]> www.fi.muni.cz Git - heater.git/commitdiff
Battery voltage measurement and reporting
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 31 Jan 2014 12:42:47 +0000 (13:42 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 31 Jan 2014 12:42:47 +0000 (13:42 +0100)
TODO: calculate the power output based on this

firmware/main.c

index ddbd2d3cf54471fd8edf97dc0dbc87ae670f3dca..edbbf0a959ca5edc91b39b9ab0270e32d9e33f56 100644 (file)
@@ -66,8 +66,26 @@ static unsigned char intensity = 0; // selected power level
 
 /* which state (output on or output off) are we measuring now */
 static volatile unsigned char adc_type, adc_drop;
 
 /* which state (output on or output off) are we measuring now */
 static volatile unsigned char adc_type, adc_drop;
+#define ADC_RUNAVG_SHIFT 5     // running average shift on batt_on, batt_off
 static volatile uint16_t batt_on, batt_off; // measured voltage
 
 static volatile uint16_t batt_on, batt_off; // measured voltage
 
+/*
+ * The voltage divider has 1M5 and 300K resistors (i.e. it measures 1/6th of
+ * the real voltage), ADC uses 1.1V internal reference.
+ * Macro to calculate upper eight bits of the ADC running-averaged value
+ * from the voltage in milivolts.
+ */
+#define ADC_1100MV_VALUE       1071    // measured, not exactly 1100
+#define MV_TO_ADC8(mV) ((unsigned char)(((uint32_t)(1UL << ADC_RUNAVG_SHIFT) \
+                               * (1024UL * (mV)) \
+                               / (6UL * ADC_1100MV_VALUE)) >> 8))
+#define BATT_N_LEVELS  3
+static unsigned char batt_levels[BATT_N_LEVELS] = {
+       MV_TO_ADC8(3500),
+       MV_TO_ADC8(3700),
+       MV_TO_ADC8(3900),
+};
+
 /* timing by WDT */
 static volatile unsigned char jiffies, next_clock_tick;
 
 /* timing by WDT */
 static volatile unsigned char jiffies, next_clock_tick;
 
@@ -112,15 +130,15 @@ ISR(ADC_vect)
        // so don't bother with it.
        if (adc_type == 0) {
                if (batt_off) {
        // so don't bother with it.
        if (adc_type == 0) {
                if (batt_off) {
-                       batt_off += adcw - (batt_off >> 5);
+                       batt_off += adcw - (batt_off >> ADC_RUNAVG_SHIFT);
                } else {
                } else {
-                       batt_off = adcw << 5;
+                       batt_off = adcw << ADC_RUNAVG_SHIFT;
                }
        } else {
                if (batt_on) {
                }
        } else {
                if (batt_on) {
-                       batt_on += adcw - (batt_on >> 5);
+                       batt_on += adcw - (batt_on >> ADC_RUNAVG_SHIFT);
                } else {
                } else {
-                       batt_on = adcw << 5;
+                       batt_on = adcw << ADC_RUNAVG_SHIFT;
                }
        }
 }
                }
        }
 }
@@ -136,7 +154,8 @@ static void pwm_init()
        TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
        TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
-       OCR1B = steps[0];
+       // OCR1B = steps[0];
+       OCR1B = 0;
        TIMSK = _BV(OCIE1B) | _BV(TOIE1);
 }
 
        TIMSK = _BV(OCIE1B) | _BV(TOIE1);
 }
 
@@ -349,10 +368,27 @@ static void timer_check_buttons()
 static unsigned char blink_on_time, blink_off_time, n_blinks;
 static unsigned char blink_counter;
 
 static unsigned char blink_on_time, blink_off_time, n_blinks;
 static unsigned char blink_counter;
 
+static unsigned char battery_level()
+{
+       unsigned char i, adc8;
+
+       // NOTE: we use 8-bit value only, so we don't need lock to protect
+       // us against concurrently running ADC IRQ handler:
+       adc8 = batt_off >> 8;
+
+       for (i = 0; i < BATT_N_LEVELS; i++)
+               if (batt_levels[i] > adc8)
+                       break;
+
+       return i;
+}
+
 static void status_led_next_pattern()
 {
 static void status_led_next_pattern()
 {
+
        // for now, display the selected intensity
        // for now, display the selected intensity
-       n_blinks = intensity + 1;
+       // n_blinks = intensity + 1;
+       n_blinks = battery_level() + 1;
        blink_on_time = 0;
        blink_off_time = 2;
        blink_counter = 10;
        blink_on_time = 0;
        blink_off_time = 2;
        blink_counter = 10;
@@ -378,6 +414,13 @@ int main()
 {
        log_init();
 
 {
        log_init();
 
+#if 0
+       log_word(batt_levels[0]);
+       log_word(batt_levels[1]);
+       log_word(batt_levels[2]);
+       log_flush();
+#endif
+
        power_down();
 
        sei();
        power_down();
 
        sei();
@@ -397,6 +440,20 @@ int main()
                        next_clock_tick = 0;
                        timer_check_buttons();
                        timer_blink();
                        next_clock_tick = 0;
                        timer_check_buttons();
                        timer_blink();
+                       if ((jiffies & 0x0F) == 0) {
+                               unsigned char i;
+
+                               for (i = 0; i < BATT_N_LEVELS; i++)
+                                       if (batt_levels[i] > batt_off)
+                                               break;
+
+#if 0
+                               log_byte(0xcc);
+                               log_byte(i);
+                               log_byte(batt_off >> 8);
+                               log_byte(batt_on >> 8);
+#endif
+                       }
                        log_flush();
                }
        }
                        log_flush();
                }
        }