tweak voltage divider for my config
[digitaldcpower] / main.c
diff --git a/main.c b/main.c
index 514edf5..a1205be 100644 (file)
--- a/main.c
+++ b/main.c
@@ -9,6 +9,7 @@
 * Clock frequency     : Internal clock 8 Mhz 
 *********************************************/
 #include <avr/io.h>
+#include <avr/pgmspace.h>
 #include <inttypes.h>
 #include <avr/interrupt.h>
 #define F_CPU 8000000UL  // 8 MHz
@@ -24,7 +25,7 @@
 #include "hardware_settings.h"
 
 // change this version string when you compile:
-#define SWVERSION "ver: ddcp-0.6.3"
+#define SWVERSION "ver: ddcp-0.6.6"
 //#define DEBUGDISP 1
 
 //debug LED:
@@ -53,6 +54,52 @@ static uint8_t uartstrpos=0;
 static uint8_t uart_has_one_line=0;
 #endif
 
+// convert voltage values to adc values, disp=10 is 1.0V
+// ADC for voltage is 11bit:
+static int16_t disp_u_to_adc(int16_t disp){
+        return((int16_t)(((float)disp * 204.7) / (ADC_REF * U_DIVIDER )));
+}
+// calculate the needed adc offset for voltage drop on the
+// current measurement shunt (the shunt has about 0.75 Ohm =1/1.33 Ohm)
+// use 1/1.2 instead of 1/1.3 because cables and connectors have as well
+// a loss.
+static int16_t disp_i_to_u_adc_offset(int16_t disp){
+        return(disp_u_to_adc(disp/12));
+}
+// convert adc values to voltage values, disp=10 is 1.0V
+// disp_i_val is needed to calculate the offset for the voltage drop over
+// the current measurement shunt, voltage measurement is 11bit
+static int16_t adc_u_to_disp(int16_t adcunits,int16_t disp_i_val){
+        int16_t adcdrop;
+        adcdrop=disp_i_to_u_adc_offset(disp_i_val);
+        if (adcunits < adcdrop){
+                return(0);
+        }
+        adcunits=adcunits-adcdrop;
+        return((int16_t)((((float)adcunits /204.7)* ADC_REF * U_DIVIDER)+0.5));
+}
+// convert adc values to current values, disp=10 needed to be printed
+// by the printing function as 0.10 A, current measurement is 10bit
+static int16_t disp_i_to_adc(int16_t disp){
+        return((int16_t) (((disp * 10.23)* I_RESISTOR) / ADC_REF));
+}
+// convert adc values to current values, disp=10 needed to be printed
+// by the printing function as 0.10 A, current measurement is 10bit
+static int16_t adc_i_to_disp(int16_t adcunits){
+        return((int16_t) (((float)adcunits* ADC_REF)/(10.23 * I_RESISTOR)+0.5));
+}
+
+static void update_controlloop_targets(void){
+        // current
+        measured_val[0]=adc_i_to_disp(getanalogresult(0));
+        set_val_adcUnits[0]=disp_i_to_adc(set_val[0]);
+        set_target_adc_val(0,set_val_adcUnits[0]);
+        // voltage
+        measured_val[1]=adc_u_to_disp(getanalogresult(1),measured_val[0]);
+        set_val_adcUnits[1]=disp_u_to_adc(set_val[1])+disp_i_to_u_adc_offset(measured_val[0]);
+        set_target_adc_val(1,set_val_adcUnits[1]);
+}
+
 void delay_ms_uartcheck(uint8_t ms)
 // delay for a minimum of <ms> 
 {
@@ -100,7 +147,7 @@ void delay_ms_uartcheck(uint8_t ms)
                                 uartstrpos++;
                         }
                         if (uartstrpos>UARTSTRLEN){
-                                uart_sendstr_P("\r\nERROR\r\n");
+                                uart_sendstr_p(PSTR("\r\nERROR\r\n"));
                                 uartstrpos=0; // empty buffer
                                 uartstr[0]='\0'; // just print prompt
                                 uart_has_one_line=1; 
@@ -148,41 +195,6 @@ static void int_to_dispstr(uint16_t inum,char *outbuf,int8_t decimalpoint_pos){
         }
 }
 
-// convert voltage values to adc values, disp=10 is 1.0V
-// ADC for voltage is 11bit:
-static int16_t disp_u_to_adc(int16_t disp){
-        return((int16_t)(((float)disp * 204.7) / (ADC_REF * U_DIVIDER )));
-}
-// calculate the needed adc offset for voltage drop on the
-// current measurement shunt (the shunt has about 0.75 Ohm =1/1.33 Ohm)
-// use 1/1.2 instead of 1/1.3 because cables and connectors have as well
-// a loss.
-static int16_t disp_i_to_u_adc_offset(int16_t disp){
-        return(disp_u_to_adc(disp/12));
-}
-// convert adc values to voltage values, disp=10 is 1.0V
-// disp_i_val is needed to calculate the offset for the voltage drop over
-// the current measurement shunt, voltage measurement is 11bit
-static int16_t adc_u_to_disp(int16_t adcunits,int16_t disp_i_val){
-        int16_t adcdrop;
-        adcdrop=disp_i_to_u_adc_offset(disp_i_val);
-        if (adcunits < adcdrop){
-                return(0);
-        }
-        adcunits=adcunits-adcdrop;
-        return((int16_t)((((float)adcunits /204.7)* ADC_REF * U_DIVIDER)+0.5));
-}
-// convert adc values to current values, disp=10 needed to be printed
-// by the printing function as 0.10 A, current measurement is 10bit
-static int16_t disp_i_to_adc(int16_t disp){
-        return((int16_t) (((disp * 10.23)* I_RESISTOR) / ADC_REF));
-}
-// convert adc values to current values, disp=10 needed to be printed
-// by the printing function as 0.10 A, current measurement is 10bit
-static int16_t adc_i_to_disp(int16_t adcunits){
-        return((int16_t) (((float)adcunits* ADC_REF)/(10.23 * I_RESISTOR)+0.5));
-}
-
 static void store_permanent(void){
         int16_t tmp;
         uint8_t changeflag=1;
@@ -201,18 +213,18 @@ static void store_permanent(void){
         }
         delay_ms_uartcheck(1); // check for uart without delay
         if (changeflag){
-                lcd_puts_P("setting stored");
+                lcd_puts_p(PSTR("setting stored"));
                 eeprom_write_byte((uint8_t *)0x0,19); // magic number
                 eeprom_write_word((uint16_t *)0x02,set_val[0]);
                 eeprom_write_word((uint16_t *)0x04,set_val[1]);
         }else{
                 if (bpress> 2){
                         // display software version after long press
-                        lcd_puts_P(SWVERSION);
+                        lcd_puts_p(PSTR(SWVERSION));
                         lcd_gotoxy(0,1);
-                        lcd_puts_P("tuxgraphics.org");
+                        lcd_puts_p(PSTR("tuxgraphics.org"));
                 }else{
-                        lcd_puts_P("already stored");
+                        lcd_puts_p(PSTR("already stored"));
                 }
         }
         delay_ms_uartcheck(200);
@@ -242,9 +254,9 @@ static uint8_t check_buttons(void){
                         }
                         // version
                         if (uartstr[0]=='v' && uartstr[1]=='e'){
-                                uart_sendstr_p(P("  "));
-                                uart_sendstr_p(P(SWVERSION));
-                                uart_sendstr_p(P("\r\n"));
+                                uart_sendstr_p(PSTR("  "));
+                                uart_sendstr_p(PSTR(SWVERSION));
+                                uart_sendstr_p(PSTR("\r\n"));
                                 cmdok=1;
                         }
                         // store
@@ -264,18 +276,18 @@ static uint8_t check_buttons(void){
                         }
                         // help
                         if (uartstr[0]=='h' || uartstr[0]=='H'){
-                                uart_sendstr_p(P("  Usage: u=V*10|i=mA/10|store|help|version\r\n"));
-                                uart_sendstr_p(P("  Examples:\r\n"));
-                                uart_sendstr_p(P("  set 6V: u=60\r\n"));
-                                uart_sendstr_p(P("  max 200mA: i=20\r\n"));
+                                uart_sendstr_p(PSTR("  Usage: u=V*10|i=mA/10|store|help|version\r\n"));
+                                uart_sendstr_p(PSTR("  Examples:\r\n"));
+                                uart_sendstr_p(PSTR("  set 6V: u=60\r\n"));
+                                uart_sendstr_p(PSTR("  max 200mA: i=20\r\n"));
                                 cmdok=1;
                         }
                         if (uartprint_ok){
                                 cmdok=1;
-                                uart_sendstr_p(P("  ok\r\n"));
+                                uart_sendstr_p(PSTR("  ok\r\n"));
                         }
                         if (uartstr[0]!='\0' && cmdok==0){
-                                uart_sendstr_p(P("  command unknown\r\n"));
+                                uart_sendstr_p(PSTR("  command unknown\r\n"));
                         }
                         uart_sendchar('#'); // marking char for script interface
                         int_to_dispstr(measured_val[1],buf,1);
@@ -364,17 +376,9 @@ int main(void)
                         i=0;
                 }
                 lcd_home();
-                // current
-                measured_val[0]=adc_i_to_disp(getanalogresult(0));
-                set_val_adcUnits[0]=disp_i_to_adc(set_val[0]);
-                set_target_adc_val(0,set_val_adcUnits[0]);
-                // voltage
-                measured_val[1]=adc_u_to_disp(getanalogresult(1),measured_val[0]);
-                set_val_adcUnits[1]=disp_u_to_adc(set_val[1])+disp_i_to_u_adc_offset(measured_val[0]);
-                set_target_adc_val(1,set_val_adcUnits[1]);
+                update_controlloop_targets();
                 ilimit=is_current_limit();
 
-                        
                 // voltage
 #ifdef DEBUGDISP
                 itoa(getanalogresult(1),out_buf,10);
@@ -421,6 +425,8 @@ int main(void)
                         lcd_puts("   ");
                 }
 
+                update_controlloop_targets();
+
                 // the buttons must be responsive but they must not 
                 // scroll too fast if pressed permanently
                 if (check_buttons()==0){
@@ -432,10 +438,10 @@ int main(void)
                                 delay_ms_uartcheck(80);
                         }else{
                                 bpress++;
-                                delay_ms_uartcheck(180);
-                                delay_ms_uartcheck(180);
-                                delay_ms_uartcheck(180);
-                                delay_ms_uartcheck(180);
+                                delay_ms_uartcheck(160);
+                                delay_ms_uartcheck(160);
+                                delay_ms_uartcheck(160);
+                                delay_ms_uartcheck(160);
                         }
                 }else{
                         // button press