From 60aa84841d0fb476b56237371fe5a24d7b3ec77e Mon Sep 17 00:00:00 2001 From: Zhaoqi Xu Date: Thu, 15 Oct 2026 18:20:38 +0800 Subject: [PATCH] [INA226] Read the shunt voltage as a signed value (#2410) The shunt voltage register is a 16-bit two's complement value. The old conversion tested `shuntvolt && 0x8000` (always true for a non-zero reading) and then negated the complement of the zero-extended value, which gives back the raw register value. Positive readings came out right by accident, but a reverse current was published as a large positive current (0xFFF0 -> 1.638 A instead of -0.4 mA with the default 0.1 ohm shunt). Cast the register to int16_t instead. Fixes #2395 Signed-off-by: Zhaoqi Xu --- main/sensorINA226.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/main/sensorINA226.cpp b/main/sensorINA226.cpp index b63a5d9..85924d0 100644 --- a/main/sensorINA226.cpp +++ b/main/sensorINA226.cpp @@ -66,12 +66,8 @@ void MeasureINA226() { // Bus Spannung, read-only, 16Bit, 0...40.96V max., LSB 1.25mV float volt = readRegister(0x02) * 0.00125; // Seite 24: Shunt Spannung +- 81,92mV mit 16 Bit, LSB 2,5uV - int shuntvolt = readRegister(0x01); - if (shuntvolt && 0x8000) { // eine negative Zahl? Dann 2er Komplement bilden - shuntvolt = ~shuntvolt; // alle Bits invertieren - shuntvolt += 1; // 1 dazuzählen - shuntvolt *= -1; // negativ machen - } + // The register holds a signed two's complement value, so a reverse current reads as negative + int16_t shuntvolt = (int16_t)readRegister(0x01); float current = shuntvolt * 0.0000025 / rShunt; // * LSB / R float power = abs(volt * current);