]> www.fi.muni.cz Git - bike-lights.git/commitdiff
ADC: ignore the first reading after MUX switch
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 Aug 2012 19:57:41 +0000 (21:57 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 Aug 2012 19:57:41 +0000 (21:57 +0200)
adc.c

diff --git a/adc.c b/adc.c
index b758721d73e4c298ff3561b0be773b7336879894..9a5e31438834a7408acfd7e086935a14781da37f 100644 (file)
--- a/adc.c
+++ b/adc.c
@@ -18,8 +18,9 @@ static unsigned char adc_mux[] = { // pwmleds should be first
 
 #define AMBIENT_ADC N_PWMLEDS
 
-#define LAST_ADC (sizeof(adc_mux)/sizeof(char))
-volatile static unsigned char current_adc = LAST_ADC;
+#define LAST_ADC (sizeof(adc_mux)/sizeof(adc_mux[0]))
+volatile static unsigned char current_adc;
+static unsigned char adc_ignore;
 
 static void start_next_adc()
 {
@@ -40,11 +41,15 @@ static void start_next_adc()
 found:
        // ADCSRB |= _BV(GSEL); // gain 8 or 32
        ADMUX = adc_mux[current_adc]; // set up mux, start one-shot conversion
+       adc_ignore = 1; // ignore first reading after mux change
        ADCSRA |= _BV(ADSC);
 }
 
 void init_adc()
 {
+       current_adc = LAST_ADC;
+       adc_ignore = 1;
+
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
                // | _BV(ADPS2)                 // CLK/16 = 62.5 kHz
@@ -68,6 +73,18 @@ void init_adc()
 ISR(ADC_vect) { // IRQ handler
        uint16_t adcval = ADCW;
 
+#if 0
+       log_byte(0xF3);
+       log_byte(current_adc);
+       log_word(adcval);
+#endif
+
+       if (adc_ignore) {
+               ADCSRA |= _BV(ADSC);
+               adc_ignore = 0;
+               return;
+       }
+
        if (current_adc < N_PWMLEDS)
                pwmled_adc(current_adc, adcval);
        if (current_adc == AMBIENT_ADC)