1 /*********************************************
2 * vim: set sw=8 ts=8 si et :
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 version string when you compile:
27 #define SWVERSION "ver: ddcp-0.6.4"
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;
46 // comment this out to use a debug LED on PD0 (RXD):
51 static char uartstr[UARTSTRLEN+1];
52 static uint8_t uartstrpos=0;
53 static uint8_t uart_has_one_line=0;
56 // convert voltage values to adc values, disp=10 is 1.0V
57 // ADC for voltage is 11bit:
58 static int16_t disp_u_to_adc(int16_t disp){
59 return((int16_t)(((float)disp * 204.7) / (ADC_REF * U_DIVIDER )));
61 // calculate the needed adc offset for voltage drop on the
62 // current measurement shunt (the shunt has about 0.75 Ohm =1/1.33 Ohm)
63 // use 1/1.2 instead of 1/1.3 because cables and connectors have as well
65 static int16_t disp_i_to_u_adc_offset(int16_t disp){
66 return(disp_u_to_adc(disp/12));
68 // convert adc values to voltage values, disp=10 is 1.0V
69 // disp_i_val is needed to calculate the offset for the voltage drop over
70 // the current measurement shunt, voltage measurement is 11bit
71 static int16_t adc_u_to_disp(int16_t adcunits,int16_t disp_i_val){
73 adcdrop=disp_i_to_u_adc_offset(disp_i_val);
74 if (adcunits < adcdrop){
77 adcunits=adcunits-adcdrop;
78 return((int16_t)((((float)adcunits /204.7)* ADC_REF * U_DIVIDER)+0.5));
80 // convert adc values to current values, disp=10 needed to be printed
81 // by the printing function as 0.10 A, current measurement is 10bit
82 static int16_t disp_i_to_adc(int16_t disp){
83 return((int16_t) (((disp * 10.23)* I_RESISTOR) / ADC_REF));
85 // convert adc values to current values, disp=10 needed to be printed
86 // by the printing function as 0.10 A, current measurement is 10bit
87 static int16_t adc_i_to_disp(int16_t adcunits){
88 return((int16_t) (((float)adcunits* ADC_REF)/(10.23 * I_RESISTOR)+0.5));
91 static void update_controlloop_targets(void){
93 measured_val[0]=adc_i_to_disp(getanalogresult(0));
94 set_val_adcUnits[0]=disp_i_to_adc(set_val[0]);
95 set_target_adc_val(0,set_val_adcUnits[0]);
97 measured_val[1]=adc_u_to_disp(getanalogresult(1),measured_val[0]);
98 set_val_adcUnits[1]=disp_u_to_adc(set_val[1])+disp_i_to_u_adc_offset(measured_val[0]);
99 set_target_adc_val(1,set_val_adcUnits[1]);
102 void delay_ms_uartcheck(uint8_t ms)
103 // delay for a minimum of <ms>
109 if(uart_has_one_line==0 && uart_getchar_isr_noblock(&c)){
110 if (c=='\n') c='\r'; // Make unix scripting easier. A terminal, even under unix, does not send \n
111 // ignore any white space and characters we do not use:
112 if (!(c=='\b'||(c>='0'&&c<='z')||c==0x7f||c=='\r')){
116 uartstr[uartstrpos]='\0';
117 uart_sendchar('\r'); // the echo line end
118 uart_sendchar('\n'); // the echo line end
125 uart_sendchar('\r'); // the echo line end
126 uart_sendchar('\n'); // the echo line end
127 uart_sendchar('|');uart_sendstr(buf);uart_sendchar('|');
128 uart_sendchar('\r'); // the echo line end
129 uart_sendchar('\n'); // the echo line end
131 if (c=='\b'){ // backspace
134 uart_sendchar(c); // echo back
135 uart_sendchar(' '); // clear char on screen
138 }else if (c==0x7f){ // del
141 uart_sendchar(c); // echo back
144 uart_sendchar(c); // echo back
145 uartstr[uartstrpos]=c;
148 if (uartstrpos>UARTSTRLEN){
149 uart_sendstr_P("\r\nERROR\r\n");
150 uartstrpos=0; // empty buffer
151 uartstr[0]='\0'; // just print prompt
165 // Convert an integer which is representing a float into a string.
166 // Our display is always 4 digits long (including one
167 // decimal point position). decimalpoint_pos defines
168 // after how many positions from the right we set the decimal point.
169 // The resulting string is fixed width and padded with leading space.
171 // decimalpoint_pos=2 sets the decimal point after 2 pos from the right:
172 // e.g 74 becomes "0.74"
173 // The integer should not be larger than 999.
174 // The integer must be a positive number.
175 // decimalpoint_pos can be 0, 1 or 2
176 static void int_to_dispstr(uint16_t inum,char *outbuf,int8_t decimalpoint_pos){
179 itoa(inum,chbuf,10); // convert integer to string
181 if (i>3) i=3; //overflow protection
182 strcpy(outbuf," 0"); //decimalpoint_pos==0
183 if (decimalpoint_pos==1) strcpy(outbuf," 0.0");
184 if (decimalpoint_pos==2) strcpy(outbuf,"0.00");
187 outbuf[j-1]=chbuf[i-1];
190 if (j==4-decimalpoint_pos){
191 // jump over the pre-set dot
197 static void store_permanent(void){
199 uint8_t changeflag=1;
201 if (eeprom_read_byte((uint8_t *)0x0) == 19){
203 // ok magic number matches accept values
204 tmp=eeprom_read_word((uint16_t *)0x04);
205 if (tmp != set_val[1]){
208 tmp=eeprom_read_word((uint16_t *)0x02);
209 if (tmp != set_val[0]){
213 delay_ms_uartcheck(1); // check for uart without delay
215 lcd_puts_P("setting stored");
216 eeprom_write_byte((uint8_t *)0x0,19); // magic number
217 eeprom_write_word((uint16_t *)0x02,set_val[0]);
218 eeprom_write_word((uint16_t *)0x04,set_val[1]);
221 // display software version after long press
222 lcd_puts_P(SWVERSION);
224 lcd_puts_P("tuxgraphics.org");
226 lcd_puts_P("already stored");
229 delay_ms_uartcheck(200);
230 delay_ms_uartcheck(200);
231 delay_ms_uartcheck(200);
234 // check the keyboard
235 static uint8_t check_buttons(void){
236 uint8_t uartprint_ok=0;
243 if (uart_has_one_line){
244 if (uartstr[0]=='i' && uartstr[1]=='=' && uartstr[2]!='\0'){
245 set_val[0]=atoi(&uartstr[2]);
246 if(set_val[0]>I_MAX){
255 if (uartstr[0]=='v' && uartstr[1]=='e'){
256 uart_sendstr_p(P(" "));
257 uart_sendstr_p(P(SWVERSION));
258 uart_sendstr_p(P("\r\n"));
262 if (uartstr[0]=='s' && uartstr[1]=='t'){
266 if (uartstr[0]=='u' && uartstr[1]=='=' && uartstr[2]!='\0'){
267 set_val[1]=atoi(&uartstr[2]);
268 if(set_val[1]>U_MAX){
277 if (uartstr[0]=='h' || uartstr[0]=='H'){
278 uart_sendstr_p(P(" Usage: u=V*10|i=mA/10|store|help|version\r\n"));
279 uart_sendstr_p(P(" Examples:\r\n"));
280 uart_sendstr_p(P(" set 6V: u=60\r\n"));
281 uart_sendstr_p(P(" max 200mA: i=20\r\n"));
286 uart_sendstr_p(P(" ok\r\n"));
288 if (uartstr[0]!='\0' && cmdok==0){
289 uart_sendstr_p(P(" command unknown\r\n"));
291 uart_sendchar('#'); // marking char for script interface
292 int_to_dispstr(measured_val[1],buf,1);
297 int_to_dispstr(set_val[1],buf,1);
301 int_to_dispstr(measured_val[0],buf,2);
306 int_to_dispstr(set_val[0],buf,2);
309 if (is_current_limit()){
319 if (check_u_button(&(set_val[1]))){
320 if(set_val[1]>U_MAX){
325 if (check_i_button(&(set_val[0]))){
326 if(set_val[0]>I_MAX){
331 if (check_store_button()){
345 // debug led, you can not have an LED if you use the uart
346 DDRD|= (1<<DDD0); // LED, enable PD0, LED as output
353 set_val[0]=15;set_val[1]=50; // 150mA and 5V
354 if (eeprom_read_byte((uint8_t *)0x0) == 19){
355 // ok magic number matches accept values
356 set_val[1]=eeprom_read_word((uint16_t *)0x04);
357 set_val[0]=eeprom_read_word((uint16_t *)0x02);
359 if (set_val[0]<0) set_val[0]=0;
360 if (set_val[1]<0) set_val[1]=0;
369 // due to electrical interference we can get some
370 // garbage onto the display especially if the power supply
371 // source is not stable enough. We can remedy it a bit in
372 // software with an ocasional reset:
373 if (i==50){ // not every round to avoid flicker
378 update_controlloop_targets();
379 ilimit=is_current_limit();
383 itoa(getanalogresult(1),out_buf,10);
385 int_to_dispstr(measured_val[1],out_buf,1);
390 itoa(set_val_adcUnits[1],out_buf,10);
392 int_to_dispstr(set_val[1],out_buf,1);
396 delay_ms_uartcheck(1); // check for uart without delay
398 // put a marker to show which value is currenlty limiting
407 itoa(getanalogresult(0),out_buf,10);
409 int_to_dispstr(measured_val[0],out_buf,2);
414 itoa(set_val_adcUnits[0],out_buf,10);
416 int_to_dispstr(set_val[0],out_buf,2);
421 // put a marker to show which value is currenlty limiting
427 update_controlloop_targets();
429 // the buttons must be responsive but they must not
430 // scroll too fast if pressed permanently
431 if (check_buttons()==0){
432 // no buttons pressed
433 delay_ms_uartcheck(80);
435 if (check_buttons()==0){
436 // no buttons pressed
437 delay_ms_uartcheck(80);
440 delay_ms_uartcheck(160);
441 delay_ms_uartcheck(160);
442 delay_ms_uartcheck(160);
443 delay_ms_uartcheck(160);
448 // somebody pressed permanetly the button=>scroll fast
449 delay_ms_uartcheck(120);
452 delay_ms_uartcheck(180);
453 delay_ms_uartcheck(180);
454 delay_ms_uartcheck(180);
455 delay_ms_uartcheck(180);