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=4763f0563334f99423d99a6e2f9893cabe034a27;hp=d7123e95839a4af818f51c5bc1f73a8bd46a4541;hb=220d48069967aaae91c303f84b98f2bb36a51425;hpb=5d0f6ceb47eed2282aeb11eb57eebc390add234e diff --git a/firmware/battery.c b/firmware/battery.c index d7123e9..4763f05 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 */ @@ -106,8 +112,12 @@ unsigned char battery_gauge() rv = 10; } - if (rv == 1) - battery_critical = 1; +#if 0 + if (battery_adcval && rv == 1) + err_flags.err_battery = 1; + else + err_flags.err_battery = 0; +#endif #if 0 log_byte(0xbb);