aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-12-30 11:20:04 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-12-30 11:20:04 +0100
commit9bc0bc5fc01c5e638bc325dd2080c2b8bfa5a81f (patch)
tree8124b9574b6faa362564edbdfbd6e11699c91e89
parent96b3081127f2c89e4b2e04fa5ca6690f7cd1ea9a (diff)
parentc99ed77e3748c27ea2c4d075be90c744bdeff9bd (diff)
downloadglutte-batteries-9bc0bc5fc01c5e638bc325dd2080c2b8bfa5a81f.tar.gz
glutte-batteries-9bc0bc5fc01c5e638bc325dd2080c2b8bfa5a81f.tar.bz2
glutte-batteries-9bc0bc5fc01c5e638bc325dd2080c2b8bfa5a81f.zip
-rw-r--r--sw/main.cpp10
1 files changed, 7 insertions, 3 deletions
diff --git a/sw/main.cpp b/sw/main.cpp
index e4f0a67..69e554a 100644
--- a/sw/main.cpp
+++ b/sw/main.cpp
@@ -111,7 +111,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.
*
@@ -482,7 +482,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, adc_voltage_id::V_BAT_PLUS, time_now);
SET_ADMUX(1);
// Start ADC conversion
@@ -495,7 +498,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, adc_voltage_id::V_BAT_MINUS, time_now);