X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=blobdiff_plain;f=firmware%2Fadc.c;h=3fdbe3262f3d6467364cd1a73063abd0d3be75e7;hp=1dbedfc2c2e36fbb4f19c369152efeb7e614d291;hb=HEAD;hpb=918f7a82545a1fb7c5a505abe797d7849e3f3d71 diff --git a/firmware/adc.c b/firmware/adc.c index 1dbedfc..2bca0cf 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -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; }