[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 <lzy00419@outlook.com>
This commit is contained in:
Zhaoqi Xu authored and GitHub committed 2026-10-15 05:20:38 -05:00
1 parent 7b9d32b765
commit 60aa84841d
1 file changed
+2 -6
+2 -6
View File
@@ -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);