aboutsummaryrefslogtreecommitdiff
path: root/spaghetti-monster/fsm-adc.c
diff options
context:
space:
mode:
authorSelene ToyKeeper2020-07-31 20:53:12 -0600
committerSelene ToyKeeper2020-07-31 20:53:12 -0600
commite9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4 (patch)
tree7990da359a597e9dcfabf5af6b693fb28459c424 /spaghetti-monster/fsm-adc.c
parentfixed bug: sunset timer didn't adjust MCU clock speed at low levels, (diff)
downloadanduril-e9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4.tar.gz
anduril-e9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4.tar.bz2
anduril-e9e65c8e8ac8b94745ab1c1ed3567bf9a07e3ff4.zip
added a voltage calibration / correction function, 7C from battcheck to configure
Diffstat (limited to '')
-rw-r--r--spaghetti-monster/fsm-adc.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/spaghetti-monster/fsm-adc.c b/spaghetti-monster/fsm-adc.c
index 45a4297..96c4af3 100644
--- a/spaghetti-monster/fsm-adc.c
+++ b/spaghetti-monster/fsm-adc.c
@@ -114,7 +114,12 @@ static inline uint8_t calc_voltage_divider(uint16_t value) {
// use 9.7 fixed-point to get sufficient precision
uint16_t adc_per_volt = ((ADC_44<<5) - (ADC_22<<5)) / (44-22);
// shift incoming value into a matching position
- uint8_t result = ((value>>1) / adc_per_volt) + VOLTAGE_FUDGE_FACTOR;
+ uint8_t result = ((value>>1) / adc_per_volt)
+ + VOLTAGE_FUDGE_FACTOR
+ #ifdef USE_VOLTAGE_CORRECTION
+ + voltage_correction - 7
+ #endif
+ ;
return result;
}
#endif
@@ -262,7 +267,12 @@ static inline void ADC_voltage_handler() {
// calculate actual voltage: volts * 10
// ADC = 1.1 * 1024 / volts
// volts = 1.1 * 1024 / ADC
- voltage = ((uint16_t)(2*1.1*1024*10)/(measurement>>6) + VOLTAGE_FUDGE_FACTOR) >> 1;
+ voltage = ((uint16_t)(2*1.1*1024*10)/(measurement>>6)
+ + VOLTAGE_FUDGE_FACTOR
+ #ifdef USE_VOLTAGE_CORRECTION
+ + voltage_correction - 7
+ #endif
+ ) >> 1;
#endif
// if low, callback EV_voltage_low / EV_voltage_critical