aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-12-29 22:34:59 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-12-29 22:34:59 +0100
commitc99ed77e3748c27ea2c4d075be90c744bdeff9bd (patch)
tree4d49258b03edaef256a44e8dd47d6f108c3e5658
parent48111bda207153b058d67f188ef2adc257f952ac (diff)
downloadglutte-batteries-c99ed77e3748c27ea2c4d075be90c744bdeff9bd.tar.gz
glutte-batteries-c99ed77e3748c27ea2c4d075be90c744bdeff9bd.tar.bz2
glutte-batteries-c99ed77e3748c27ea2c4d075be90c744bdeff9bd.zip
Read ADCL before ADCH
-rw-r--r--sw/main.cpp8
1 files changed, 6 insertions, 2 deletions
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);