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;fp=firmware%2Fbattery.c;h=472c1c47f099093c59bfe6454983276687e9dad8;hp=0000000000000000000000000000000000000000;hb=3aa04c7f0ae362c4494d272ecf249d862e3aec01;hpb=5b2ef9def765f5bfde371bff0f811c9d9cc8d682 diff --git a/firmware/battery.c b/firmware/battery.c new file mode 100644 index 0000000..472c1c4 --- /dev/null +++ b/firmware/battery.c @@ -0,0 +1,25 @@ +#include + +#include "lights.h" + +#define RESISTOR_HI 1500 // kOhm +#define RESISTOR_LO 150 // kOhm + +volatile unsigned char battery_100mv; + +void battery_adc(uint16_t adcval) +{ + /* + * This is tricky: we need to maintain precision, so we first + * multiply adcval by as big number as possible to fit uint16_t, + * then divide to get the final value, + * and finally type-cast it to unsigned char. + * We don't do running average, as the required precision + * is coarse (0.1 V). + */ + battery_100mv = (unsigned char) + ((adcval * 11 // 1.1V + * (RESISTOR_HI+RESISTOR_LO)/RESISTOR_LO // resistor ratio + / 4) >> 8); // divide by 1024 +} +