Skip to content

Commit

Permalink
use higher resolution for readRefVcc
Browse files Browse the repository at this point in the history
  • Loading branch information
jp112sdl committed Dec 9, 2018
1 parent 5874f2e commit 821d5f1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions BatterySensor.h
Expand Up @@ -105,7 +105,7 @@ class BatterySensor : public Alarm {
clock.add(*this);
}

virtual uint8_t voltage() {
uint16_t voltageHighRes() {
uint16_t vcc = 0;
#ifdef ARDUINO_ARCH_AVR
// Read 1.1V reference against AVcc
Expand All @@ -118,15 +118,19 @@ class BatterySensor : public Alarm {
ADCSRA |= (1 << ADSC); // start conversion
while (ADCSRA & (1 << ADSC)); // wait to finish

vcc = 1100UL * 1023 / ADC / 100;
vcc = 1100UL * 1023 / ADC;
#elif defined ARDUINO_ARCH_STM32F1
int millivolts = 1200 * 4096 / adc_read(ADC1, 17); // ADC sample to millivolts
vcc = millivolts / 100;
vcc = millivolts;
#endif
DPRINT(F("Bat: ")); DDECLN(vcc);
return (uint8_t) vcc;
return vcc;
}

virtual uint8_t voltage() {
uint8_t vcc = voltageHighRes() / 100;
DPRINT(F("Bat: ")); DDECLN(vcc);
return vcc;
}

};

Expand Down Expand Up @@ -169,7 +173,7 @@ class BatterySensorUni : public BatterySensor {
}

uint16_t readRefVcc () {
return VCC != 0 ? VCC : BatterySensor::voltage() * 100;
return VCC != 0 ? VCC : BatterySensor::voltageHighRes();
}
};

Expand Down

0 comments on commit 821d5f1

Please sign in to comment.