Fix for MCP ADC not displaying correct voltage

This commit is contained in:
Levi Gillis
2021-12-17 16:21:36 +01:00
parent e411e65f34
commit c792bed763
2 changed files with 8 additions and 2 deletions

View File

@@ -42,7 +42,7 @@ void BatteryMonitor::Loop()
level = voltage_3_3 - level;
if (level < 50)
{
voltage = 4.0F;
voltage = 5.0F;
}
else
{
@@ -52,7 +52,8 @@ void BatteryMonitor::Loop()
#endif
#if BATTERY_MONITOR_EXTERNAL
last_battery_sample = now_ms;
voltage = ((float)analogRead(PIN_BATTERY_LEVEL)) * batteryADCMultiplier;
float e = ((float)analogRead(PIN_BATTERY_LEVEL)) * batteryADCMultiplier;
voltage = (voltage > 0) ? min(voltage, e) : e;
#endif
#if BATTERY_MONITOR_MCP3021
if (address > 0)
@@ -65,6 +66,7 @@ void BatteryMonitor::Loop()
if (status == 0)
{
float v = (((uint16_t)(MSB & 0x0F) << 6) | (uint16_t)(LSB >> 2));
v *= batteryADCMultiplier;
voltage = (voltage > 0) ? min(voltage, v) : v;
}
}

View File

@@ -129,5 +129,9 @@
// SlimeVR Board can handle max 5V > so analogRead of 5.0V input will result in 1023.0
#define batteryADCMultiplier 1.0 / 1023.0 * (320 + BATTERY_MONITOR_EXTERNAL) / 100
#endif
#if BATTERY_MONITOR_MCP3021
// Default recommended resistors are 9.1k and 5.1k
#define batteryADCMultiplier 3.3 / 1023.0 * 14.2 / 9.1
#endif
#endif // SLIMEVR_DEFINES_H_