X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=blobdiff_plain;f=firmware%2Fbattery.c;h=192acf04e40502fd3097ed2425ee9c8288bc0d31;hp=d7123e95839a4af818f51c5bc1f73a8bd46a4541;hb=65c3ad96cf307c3b77b36e6f6a2af5201c213a3c;hpb=5d0f6ceb47eed2282aeb11eb57eebc390add234e diff --git a/firmware/battery.c b/firmware/battery.c index d7123e9..192acf0 100644 --- a/firmware/battery.c +++ b/firmware/battery.c @@ -5,6 +5,12 @@ #define BATTERY_ADC_SHIFT 6 #define RESISTOR_HI 1500 // kOhm #define RESISTOR_LO 100 // kOhm +/* + * The internal 1.1V reference has tolerance from 1.0 to 1.2V + * (datasheet, section 19.6). We have to measure the actual value + * of our part. + */ +#define AREF_1100MV 1060 // mV static volatile uint16_t battery_adcval; volatile unsigned char battery_critical; @@ -46,7 +52,7 @@ unsigned char battery_100mv() /* convert value in mV to value of ADC shifted by BATTERY_ADC_SHIFT */ #define MV_TO_ADC(x) \ ((uint16_t)(((uint32_t)(x) * 1024 * (1 << BATTERY_ADC_SHIFT)) \ - / ((uint32_t) 1100 \ + / ((uint32_t) AREF_1100MV \ * ((RESISTOR_HI+RESISTOR_LO)/RESISTOR_LO)))) /* convert value in mV to upper 8 bits of ADC value << BATTERY_ADC_SHIFT */