From: Jan "Yenya" Kasprzak Date: Wed, 6 Mar 2013 23:19:43 +0000 (+0100) Subject: battery.c: adjustment for 1.1V internal reference X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=commitdiff_plain;h=60d17ce8b779c966e6285f1fb9f3de3118752092 battery.c: adjustment for 1.1V internal reference --- 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 */