X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=firmware%2Fadc.c;h=fa0782c4248c6ee67917901cc1340dedfefea087;hb=ee55592455c9628ca21142449d4b5918a59151fe;hp=d0ee1c40f3377f17e40bd1f7662d662f72f8034f;hpb=5090774fd8ca593d3b247b099b3b6a40e837475e;p=bike-lights.git diff --git a/firmware/adc.c b/firmware/adc.c index d0ee1c4..fa0782c 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -12,12 +12,11 @@ volatile static unsigned char current_adc; static uint16_t adc_sum; static unsigned char sum_shift; static unsigned char adc_vals; -static uint16_t adc1_gain20_offset_x16; +#define ADC1_GAIN20_OFFSET_SHIFT 6 +static uint16_t adc1_gain20_offset; static void inline setup_mux(unsigned char n) { - ADCSRA |= _BV(ADEN); // enable ADC - /* ADC numbering: PWM LEDs first, then ambient light sensor, battery sensor */ switch (n) { case 0: // pwmled 1: 1.1V, ADC0,1 (PA0,1), gain 20 @@ -30,7 +29,7 @@ static void inline setup_mux(unsigned char n) break; case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended ADMUX = _BV(REFS1) | _BV(MUX2); - sum_shift = 2; // 4 measurements + sum_shift = 3; // 8 measurements break; case 3: // ambient light: 1.1V, ADC5 (PA6), single-ended ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0); @@ -42,7 +41,7 @@ static void inline setup_mux(unsigned char n) break; case 5: // gain stage offset: 1.1V, ADC1,1, gain 20 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0); - sum_shift = 3; // 8 measurements + sum_shift = 0; // 1 measurement break; } @@ -87,21 +86,20 @@ void init_adc() ; ADCSRA |= _BV(ADIF); // clear the IRQ flag - adc1_gain20_offset_x16 = 0; + adc1_gain20_offset = 0; - for (i = 0; i < 16; i++) { + for (i = 0; i < (1 << ADC1_GAIN20_OFFSET_SHIFT); i++) { ADCSRA |= _BV(ADSC); while ((ADCSRA & _BV(ADIF)) == 0) ; - adc1_gain20_offset_x16 += ADCW; + adc1_gain20_offset += ADCW + - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT); ADCSRA |= _BV(ADIF); // clear the IRQ flag } ADCSRA |= _BV(ADIE); // enable IRQ - - ADCSRA &= ~_BV(ADEN); // disable until needed } void susp_adc() @@ -116,8 +114,6 @@ ISR(ADC_vect) { // IRQ handler if (adc_vals) // start the next conversion immediately ADCSRA |= _BV(ADSC); - else - ADCSRA &= ~_BV(ADEN); // the last one, disable ADC if (adc_vals < (1 << sum_shift)) // drop the first conversion, use all others @@ -134,18 +130,19 @@ ISR(ADC_vect) { // IRQ handler if (current_adc == ADC1_GAIN20) { // running average - adc1_gain20_offset_x16 += adcval - - (adc1_gain20_offset_x16 >> 4); + adc1_gain20_offset += adcval + - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT); } else if (current_adc == 0 || current_adc == 1) { - uint16_t offset = adc1_gain20_offset_x16 >> 4; - if (adcval >= offset) - adcval -= offset; + uint16_t offset = adc1_gain20_offset + >> (ADC1_GAIN20_OFFSET_SHIFT - sum_shift); + if (adc_sum > offset) + adc_sum -= offset; else - adcval = 0; + adc_sum = 0; } if (current_adc < N_PWMLEDS) - pwmled_adc(current_adc, adcval); + pwmled_adc(current_adc, adc_sum); if (current_adc == AMBIENT_ADC) ambient_adc(adcval); if (current_adc == BATTERY_ADC) @@ -158,5 +155,9 @@ void timer_start_adcs() { if (current_adc == NUM_ADCS) // Don't start if in progress start_next_adc(); +#if 0 + else + log_byte(0x99); +#endif }