battery measurements (off, on)
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 23:25:09 +0000 (00:25 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 23:25:09 +0000 (00:25 +0100)
firmware/main.c

index afaa557..fcb7426 100644 (file)
@@ -17,11 +17,28 @@ static void timer_init()
 
        DDRB |= _BV(PB4);
 
-       TCCR1 = _BV(CS10); // clk/1 = 1 MHz
-       // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
+       // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
+       TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
-       OCR1B = 0;
+       OCR1B = steps[0];
+       TIMSK = _BV(OCIE1B) | _BV(TOIE1);
+}
+
+volatile unsigned char adc_type, adc_drop;
+
+ISR(TIM1_OVF_vect)
+{
+       adc_drop = 2;
+       adc_type = 1;
+       ADCSRA |= _BV(ADSC);
+}
+
+ISR(TIM1_COMPB_vect)
+{
+       adc_drop = 2;
+       adc_type = 0;
+       ADCSRA |= _BV(ADSC);
 }
 
 static void set_pwm(unsigned char pwm)
@@ -33,11 +50,38 @@ static void adc_init()
 {
        power_adc_enable();
 
-       ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); // clk/8 = 125 kHz
+       ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0) | _BV(ADIE); // clk/8 = 125 kHz
        ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); // 1.1V ref., PB3 single-ended
        DIDR0 = _BV(ADC3D);
 }
 
+volatile uint16_t batt_on, batt_off;
+
+ISR(ADC_vect)
+{
+       uint16_t adcw = ADCW;
+
+       if (adc_drop) {
+               adc_drop--;
+               ADCSRA |= _BV(ADSC);
+               return;
+       }
+
+       if (adc_type == 0) {
+               if (batt_off) {
+                       batt_off += adcw - (batt_off >> 5);
+               } else {
+                       batt_off = adcw << 5;
+               }
+       } else {
+               if (batt_on) {
+                       batt_on += adcw - (batt_on >> 5);
+               } else {
+                       batt_on = adcw << 5;
+               }
+       }
+}
+
 static void status_led_init()
 {
        DDRB |= _BV(PB2);
@@ -236,6 +280,11 @@ static void timer_blink()
                blink_on_time = 0;
                blink_off_time = 2;
                blink_counter = 10;
+#if 0
+               log_byte(0xbb);
+               log_byte(batt_on >> 7);
+               log_byte(batt_off >> 7);
+#endif
        }
 }
 
@@ -245,17 +294,6 @@ int main()
 
        power_down();
 
-#if 0
-       ADCSRA |= _BV(ADSC);
-       while (!(ADCSRA & _BV(ADIF)))
-               ;
-       log_word(ADCW);
-       ADCSRA |= _BV(ADSC);
-       while (!(ADCSRA & _BV(ADIF)))
-               ;
-       log_word(ADCW);
-       log_flush();
-#endif
        sei();
 
        // we try to be completely IRQ-driven, so just wait for IRQs here
@@ -272,6 +310,7 @@ int main()
                        wdt_timer_fired = 0;
                        timer_check_buttons();
                        timer_blink();
+                       log_flush();
                }
        }
 }