5 #define RESISTOR_HI 1500 // kOhm
6 #define RESISTOR_LO 100 // kOhm
8 volatile unsigned char battery_100mv = 0;
15 void battery_adc(uint16_t adcval)
18 * This is tricky: we need to maintain precision, so we first
19 * multiply adcval by as big number as possible to fit uint16_t,
20 * then divide to get the final value,
21 * and finally type-cast it to unsigned char.
22 * We don't do running average, as the required precision
25 battery_100mv = (unsigned char)
26 ((uint16_t)(adcval * 11 // 1.1V
27 * (RESISTOR_HI+RESISTOR_LO)/RESISTOR_LO // resistor ratio
28 / 4) >> 8); // divide by 1024