]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/adc.c
mudflap for dual rearlights
[bike-lights.git] / firmware / adc.c
index 1dbedfc2c2e36fbb4f19c369152efeb7e614d291..2bca0cf2e8cd8951acca37bbf04c6266fe787a5b 100644 (file)
@@ -158,6 +158,7 @@ void init_adc()
        unsigned char i;
        current_adc = 0;
        adc_is_on = 1;
+       slow_adcs_wanted = FIRST_1S_ADC;
 
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
@@ -203,6 +204,18 @@ static void adc1_gain20_adc(uint16_t adcsum)
 ISR(ADC_vect) { // IRQ handler
        uint16_t adcval = ADCW;
 
+       /*
+        * After the timer interrupt, drop the current reading.
+        * We may have changed the PWM outputs, so the value is
+        * probably useless anyway.
+        * FIXME: possible race condition - we should make an explicit
+        * notification inside the timer IRQ handler.
+        */
+       if (slow_adcs_wanted) {
+               start_next_adc();
+               return;
+       }
+
        if (zero_count) {
                if (zero_count > 1) {
                        ADCSRA |= _BV(ADSC);
@@ -225,6 +238,7 @@ ISR(ADC_vect) { // IRQ handler
                ADCSRA |= _BV(ADSC);
                adc_sum += adcval;
                read_count--;
+               pwm_timer();
                return;
        }