From 48111bda207153b058d67f188ef2adc257f952ac Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 29 Dec 2019 22:34:32 +0100 Subject: Set proper ADC reference selection --- sw/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sw') diff --git a/sw/main.cpp b/sw/main.cpp index 94a444a..c9ecd04 100644 --- a/sw/main.cpp +++ b/sw/main.cpp @@ -109,7 +109,7 @@ const uint32_t V_REF_mV = 5000; #define ADC_VALUE_TO_MILLIVOLT(val) ((uint32_t)val * V_REF_mV) / (uint32_t)(1<<10) // Use the LDO on Vref as ADC reference, set REFS1..REFS0 = 0b00, and ADC input 0 -#define SET_ADMUX(input) ADMUX = _BV(REFS0) | _BV(REFS1) | input +#define SET_ADMUX(input) ADMUX = (input & 0x0F) /* Timer at approximately 100ms. * -- cgit v1.2.3 From c99ed77e3748c27ea2c4d075be90c744bdeff9bd Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 29 Dec 2019 22:34:59 +0100 Subject: Read ADCL before ADCH --- sw/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'sw') diff --git a/sw/main.cpp b/sw/main.cpp index c9ecd04..894693a 100644 --- a/sw/main.cpp +++ b/sw/main.cpp @@ -475,7 +475,10 @@ int main() // ADSC is cleared when the conversion finishes if ((ADCSRA & ADSC) == 0) { // BAT+ - const uint16_t adc_value_0 = ((uint16_t)ADCH << 8) | ADCL; + + // Datasheet 24.9.3 says ADCL must be read first + const uint8_t adcl = ADCL; + const uint16_t adc_value_0 = ((uint16_t)ADCH << 8) | adcl; send_voltage(ADC_VALUE_TO_MILLIVOLT(adc_value_0) * 4, true, time_now); SET_ADMUX(1); // Start ADC conversion @@ -488,7 +491,8 @@ int main() // ADSC is cleared when the conversion finishes if ((ADCSRA & ADSC) == 0) { // BAT- - const uint16_t adc_value_1 = ((uint16_t)ADCH << 8) | ADCL; + const uint8_t adcl = ADCL; + const uint16_t adc_value_1 = ((uint16_t)ADCH << 8) | adcl; adc_state = adc_state_t::IDLE; send_voltage(ADC_VALUE_TO_MILLIVOLT(adc_value_1) * 4, false, time_now); -- cgit v1.2.3