1 /*********************************************
2 * vim: set sw=8 ts=8 si :
3 * Author: Guido Socher, Copyright: GPL v3
4 * This is the main program for the digital dc power supply
6 * See http://www.tuxgraphics.org/electronics/
9 * Clock frequency : Internal clock 8 Mhz
10 *********************************************/
13 #include <avr/interrupt.h>
14 #define F_CPU 8000000UL // 8 MHz
15 #include <util/delay.h>
18 #include <avr/eeprom.h>
24 #include "hardware_settings.h"
26 // change this when you compile:
27 #define SWVERSION "ver: ddcp-0.6.1"
31 // set output to VCC, red LED off
32 #define LEDOFF PORTD|=(1<<PORTD0)
33 // set output to GND, red LED on
34 #define LEDON PORTD&=~(1<<PORTD0)
35 // to test the state of the LED
36 #define LEDISOFF PORTD&(1<<PORTD0)
38 // the units are display units and work as follows: 100mA=10 5V=50
39 // The function int_to_dispstr is used to convert the intenal values
40 // into strings for the display
41 static int16_t measured_val[2]={0,0};
42 static int16_t set_val[2];
43 // the set values but converted to ADC steps
44 static int16_t set_val_adcUnits[2];
45 static uint8_t bpress=0;
47 static char uartstr[UARTSTRLEN+1];
48 static uint8_t uartstrpos=0;
49 static uint8_t uart_has_one_line=0;
51 void delay_ms_uartcheck(uint8_t ms)
52 // delay for a minimum of <ms>
56 if(uart_has_one_line==0 && uart_getchar_noblock(&uartstr[uartstrpos])){
57 uart_sendchar(uartstr[uartstrpos]); // echo back
58 if (uartstr[uartstrpos]=='\n'||uartstr[uartstrpos]=='\r'){
59 uartstr[uartstrpos]='\0';
61 uart_sendchar('\n'); // the echo back puts a \r
64 if (uartstrpos>UARTSTRLEN){
65 uart_sendstr_P("\r\nERROR\r\n");
66 uartstrpos=0; // empty buffer
67 uartstr[uartstrpos]='\0'; // just print prompt
75 // Convert an integer which is representing a float into a string.
76 // Our display is always 4 digits long (including one
77 // decimal point position). decimalpoint_pos defines
78 // after how many positions from the right we set the decimal point.
79 // The resulting string is fixed width and padded with leading space.
81 // decimalpoint_pos=2 sets the decimal point after 2 pos from the right:
82 // e.g 74 becomes "0.74"
83 // The integer should not be larger than 999.
84 // The integer must be a positive number.
85 // decimalpoint_pos can be 0, 1 or 2
86 static void int_to_dispstr(uint16_t inum,char *outbuf,int8_t decimalpoint_pos){
89 itoa(inum,chbuf,10); // convert integer to string
91 if (i>3) i=3; //overflow protection
92 strcpy(outbuf," 0"); //decimalpoint_pos==0
93 if (decimalpoint_pos==1) strcpy(outbuf," 0.0");
94 if (decimalpoint_pos==2) strcpy(outbuf,"0.00");
97 outbuf[j-1]=chbuf[i-1];
100 if (j==4-decimalpoint_pos){
101 // jump over the pre-set dot
107 // convert voltage values to adc values, disp=10 is 1.0V
108 // ADC for voltage is 11bit:
109 static int16_t disp_u_to_adc(int16_t disp){
110 return((int16_t)(((float)disp * 204.7) / (ADC_REF * U_DIVIDER )));
112 // calculate the needed adc offset for voltage drop on the
113 // current measurement shunt (the shunt has about 0.75 Ohm =1/1.33 Ohm)
114 // use 1/1.2 instead of 1/1.3 because cables and connectors have as well
116 static int16_t disp_i_to_u_adc_offset(int16_t disp){
117 return(disp_u_to_adc(disp/12));
119 // convert adc values to voltage values, disp=10 is 1.0V
120 // disp_i_val is needed to calculate the offset for the voltage drop over
121 // the current measurement shunt, voltage measurement is 11bit
122 static int16_t adc_u_to_disp(int16_t adcunits,int16_t disp_i_val){
124 adcdrop=disp_i_to_u_adc_offset(disp_i_val);
125 if (adcunits < adcdrop){
128 adcunits=adcunits-adcdrop;
129 return((int16_t)((((float)adcunits /204.7)* ADC_REF * U_DIVIDER)+0.5));
131 // convert adc values to current values, disp=10 needed to be printed
132 // by the printing function as 0.10 A, current measurement is 10bit
133 static int16_t disp_i_to_adc(int16_t disp){
134 return((int16_t) (((disp * 10.23)* I_RESISTOR) / ADC_REF));
136 // convert adc values to current values, disp=10 needed to be printed
137 // by the printing function as 0.10 A, current measurement is 10bit
138 static int16_t adc_i_to_disp(int16_t adcunits){
139 return((int16_t) (((float)adcunits* ADC_REF)/(10.23 * I_RESISTOR)+0.5));
142 static void store_permanent(void){
144 uint8_t changeflag=1;
146 if (eeprom_read_byte((uint8_t *)0x0) == 19){
148 // ok magic number matches accept values
149 tmp=eeprom_read_word((uint16_t *)0x04);
150 if (tmp != set_val[1]){
153 tmp=eeprom_read_word((uint16_t *)0x02);
154 if (tmp != set_val[0]){
159 lcd_puts_P("setting stored");
160 eeprom_write_byte((uint8_t *)0x0,19); // magic number
161 eeprom_write_word((uint16_t *)0x02,set_val[0]);
162 eeprom_write_word((uint16_t *)0x04,set_val[1]);
165 // display software version after long press
166 lcd_puts_P(SWVERSION);
168 lcd_puts_P("tuxgraphics.org");
170 lcd_puts_P("already stored");
173 delay_ms_uartcheck(200);
176 // check the keyboard
177 static uint8_t check_buttons(void){
179 if (uart_has_one_line){
180 // if (strncmp("?",uartstr,1)==0){
181 int_to_dispstr(measured_val[1],buf,1);
186 int_to_dispstr(set_val[1],buf,1);
191 int_to_dispstr(measured_val[0],buf,2);
196 int_to_dispstr(set_val[0],buf,2);
200 if (is_current_limit()){
210 if (check_u_button(&(set_val[1]))){
211 if(set_val[1]>U_MAX){
216 if (check_i_button(&(set_val[0]))){
217 if(set_val[0]>I_MAX){
222 if (check_store_button()){
234 // debug led, you can not have an LED if you use the uart
235 //DDRD|= (1<<DDD0); // LED, enable PD0, LED as output
241 set_val[0]=15;set_val[1]=50; // 150mA and 5V
242 if (eeprom_read_byte((uint8_t *)0x0) == 19){
243 // ok magic number matches accept values
244 set_val[1]=eeprom_read_word((uint16_t *)0x04);
245 set_val[0]=eeprom_read_word((uint16_t *)0x02);
252 // due to electrical interference we can get some
253 // garbage onto the display especially if the power supply
254 // source is not stable enough. We can remedy it a bit in
255 // software with an ocasional reset:
256 if (i==50){ // not every round to avoid flicker
262 measured_val[0]=adc_i_to_disp(getanalogresult(0));
263 set_val_adcUnits[0]=disp_i_to_adc(set_val[0]);
264 set_target_adc_val(0,set_val_adcUnits[0]);
266 measured_val[1]=adc_u_to_disp(getanalogresult(1),measured_val[0]);
267 set_val_adcUnits[1]=disp_u_to_adc(set_val[1])+disp_i_to_u_adc_offset(measured_val[0]);
268 set_target_adc_val(1,set_val_adcUnits[1]);
269 ilimit=is_current_limit();
274 itoa(getanalogresult(1),out_buf,10);
276 int_to_dispstr(measured_val[1],out_buf,1);
281 itoa(set_val_adcUnits[1],out_buf,10);
283 int_to_dispstr(set_val[1],out_buf,1);
288 // put a marker to show which value is currenlty limiting
297 itoa(getanalogresult(0),out_buf,10);
299 int_to_dispstr(measured_val[0],out_buf,2);
304 itoa(set_val_adcUnits[0],out_buf,10);
306 int_to_dispstr(set_val[0],out_buf,2);
311 // put a marker to show which value is currenlty limiting
317 // the buttons must be responsive but they must not
318 // scroll too fast if pressed permanently
319 if (check_buttons()==0){
320 // no buttons pressed
321 delay_ms_uartcheck(20);
323 if (check_buttons()==0){
324 // no buttons pressed
325 delay_ms_uartcheck(20);
328 delay_ms_uartcheck(180);
333 // somebody pressed permanetly the button=>scroll fast
334 delay_ms_uartcheck(30);
337 delay_ms_uartcheck(180);