Skip to content

Commit 4c238bd

Browse files
committed
Avoid ambiguity
1 parent d92f72c commit 4c238bd

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/SparkFun_ADS1219.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,17 @@ bool SfeADS1219Driver::readConversion()
126126
if (result)
127127
{
128128
// Data is 3-bytes (24-bits), big-endian (MSB first).
129-
_adcResult = rawBytes[0];
130-
_adcResult = (_adcResult << 8) | rawBytes[1];
131-
_adcResult = (_adcResult << 8) | rawBytes[2];
129+
union {
130+
int32_t i32;
131+
uint32_t u32;
132+
} iu32;
133+
iu32.u32 = rawBytes[0];
134+
iu32.u32 = (iu32.u32 << 8) | rawBytes[1];
135+
iu32.u32 = (iu32.u32 << 8) | rawBytes[2];
132136
// Preserve the 2's complement.
133-
if (_adcResult & (1 << 23))
134-
_adcResult |= 0xFF000000;
137+
if (0x00100000 == (iu32.u32 & 0x00100000))
138+
iu32.u32 = iu32.u32 | 0xFF000000;
139+
_adcResult = iu32.i32; // Store the result
135140
}
136141
return result;
137142
}

src/SparkFun_ADS1219.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class SfeADS1219Driver
230230

231231
ads1219_gain_config_t _adcGain; // Local configuration value. ADC gain - needed for conversion to mV.
232232

233-
int32_t _adcResult; // Local store for the ADC conversion result. 24-Bit, shifted left for correct 2's complement
233+
int32_t _adcResult; // Local store for the ADC conversion result. 24-Bit, 2's complement
234234
};
235235

236236
class SfeADS1219ArdI2C : public SfeADS1219Driver

0 commit comments

Comments
 (0)