aboutsummaryrefslogtreecommitdiffstats
path: root/src/avr/gpio.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-04-25 14:07:39 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-04-25 19:13:44 -0400
commit46cf3ef1457d0ea63dc30ecd3f343af83e03b589 (patch)
tree0e6faf878e72b008448c3849031d207343a19d49 /src/avr/gpio.c
parentcf475a9a39068292715d4cafe8b4b3f306e5fe35 (diff)
downloadkutter-46cf3ef1457d0ea63dc30ecd3f343af83e03b589.tar.gz
kutter-46cf3ef1457d0ea63dc30ecd3f343af83e03b589.tar.xz
kutter-46cf3ef1457d0ea63dc30ecd3f343af83e03b589.zip
avr: Explicitly set ADCSRA on each conversion start
Don't logically or the ADSC bit on the start of a conversion - explicitly set the full contents of the register. Also, clear the ADIF flag on each write. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/avr/gpio.c')
-rw-r--r--src/avr/gpio.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/avr/gpio.c b/src/avr/gpio.c
index 70d849f0..d4b784f5 100644
--- a/src/avr/gpio.c
+++ b/src/avr/gpio.c
@@ -263,7 +263,8 @@ static const uint8_t adc_pins[] PROGMEM = {
#endif
};
-static const uint8_t ADMUX_DEFAULT = 0x40;
+enum { ADMUX_DEFAULT = 0x40 };
+enum { ADC_ENABLE = (1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2)|(1<<ADEN)|(1<<ADIF) };
DECL_CONSTANT(ADC_MAX, 1023);
@@ -280,7 +281,7 @@ gpio_adc_setup(uint8_t pin)
}
// Enable ADC
- ADCSRA = (1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2)|(1<<ADEN);
+ ADCSRA = ADC_ENABLE;
// Disable digital input for this pin
#ifdef DIDR2
@@ -313,16 +314,18 @@ gpio_adc_sample(struct gpio_adc g)
goto need_delay;
last_analog_read = g.chan;
+ // Set the channel to sample
#if defined(ADCSRB) && defined(MUX5)
- // the MUX5 bit of ADCSRB selects whether we're reading from channels
- // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
+ // The MUX5 bit of ADCSRB selects whether we're reading from
+ // channels 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
ADCSRB = ((g.chan >> 3) & 0x01) << MUX5;
#endif
-
ADMUX = ADMUX_DEFAULT | (g.chan & 0x07);
- // start the conversion
- ADCSRA |= 1<<ADSC;
+ // Start the sample
+ ADCSRA = ADC_ENABLE | (1<<ADSC);
+
+ // Schedule next attempt after sample is likely to be complete
need_delay:
return (13 + 1) * 128 + 200;
}