File tree Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
236236class SfeADS1219ArdI2C : public SfeADS1219Driver
You can’t perform that action at this time.
0 commit comments