#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;
/* 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 */