@@ -27,7 +27,7 @@ public class Ina219 : IDisposable
27
27
// along with any conversions.
28
28
private static int s_readDelays ( Ina219AdcResolutionOrSamples adc )
29
29
{
30
- switch ( adc )
30
+ switch ( adc )
31
31
{
32
32
case Ina219AdcResolutionOrSamples . Adc9Bit : return 84 ;
33
33
case Ina219AdcResolutionOrSamples . Adc10Bit : return 148 ;
@@ -50,6 +50,7 @@ private static int s_readDelays(Ina219AdcResolutionOrSamples adc)
50
50
private Ina219AdcResolutionOrSamples _busAdcResSamp ;
51
51
private Ina219AdcResolutionOrSamples _shuntAdcResSamp ;
52
52
private float _currentLsb ;
53
+ private bool _mathOverflowFlag = false ;
53
54
54
55
/// <summary>
55
56
/// Construct an Ina219 device using an I2cDevice
@@ -238,21 +239,40 @@ public void Dispose()
238
239
}
239
240
}
240
241
242
+ /// <summary>
243
+ /// Get overflow result of last conversion
244
+ /// </summary>
245
+ /// <remarks>
246
+ /// The Math Overflow Flag (OVF) is set when the Power or Current calculations are out of range.
247
+ /// It indicates that current and power data may be meaningless.
248
+ /// </remarks>
249
+ [ Telemetry ]
250
+ public bool MathOverflowFlag { get => _mathOverflowFlag ; }
251
+
241
252
/// <summary>
242
253
/// Read the measured shunt voltage.
243
254
/// </summary>
244
255
/// <returns>The shunt potential difference</returns>
245
256
// read the shunt voltage. LSB = 10uV then convert to Volts
246
257
[ Telemetry ( "ShuntVoltage" ) ]
247
- public ElectricPotential ReadShuntVoltage ( ) => ElectricPotential . FromVolts ( ReadRegister ( Ina219Register . ShuntVoltage , s_readDelays ( _shuntAdcResSamp ) ) * 10 .0 / 1000000.0 ) ;
258
+ public ElectricPotential ReadShuntVoltage ( ) => ElectricPotential . FromVolts ( ConvertFrom16BitTwosComplement ( ReadRegister ( Ina219Register . ShuntVoltage , s_readDelays ( _shuntAdcResSamp ) ) ) / ( 1000000 .0 / 10.0 ) ) ;
248
259
249
260
/// <summary>
250
261
/// Read the measured Bus voltage.
251
262
/// </summary>
263
+ /// <remarks>
264
+ /// Will set _mathOverflowFlag according to result
265
+ /// _mathOverflowFlag can use to check if values in Power/Current registers are valid
266
+ /// </remarks>
252
267
/// <returns>The Bus potential (voltage)</returns>
253
268
// read the bus voltage. LSB = 4mV then convert to Volts
254
269
[ Telemetry ( "BusVoltage" ) ]
255
- public ElectricPotential ReadBusVoltage ( ) => ElectricPotential . FromVolts ( ( ( short ) ReadRegister ( Ina219Register . BusVoltage , s_readDelays ( _busAdcResSamp ) ) >> 3 ) * 4 / 1000.0 ) ;
270
+ public ElectricPotential ReadBusVoltage ( )
271
+ {
272
+ ushort busvoltage = ReadRegister ( Ina219Register . BusVoltage , s_readDelays ( _busAdcResSamp ) ) ;
273
+ _mathOverflowFlag = ( busvoltage & 1 ) != 0 ? true : false ;
274
+ return ElectricPotential . FromVolts ( ( ( short ) busvoltage >> 3 ) * 4 / 1000.0 ) ;
275
+ }
256
276
257
277
/// <summary>
258
278
/// Read the calculated current through the INA219.
@@ -270,7 +290,7 @@ public ElectricCurrent ReadCurrent()
270
290
// whenever needed.
271
291
SetCalibration ( _calibrationValue , _currentLsb ) ;
272
292
273
- return ElectricCurrent . FromAmperes ( ReadRegister ( Ina219Register . Current , s_readDelays ( _shuntAdcResSamp ) ) * _currentLsb ) ;
293
+ return ElectricCurrent . FromAmperes ( ConvertFrom16BitTwosComplement ( ReadRegister ( Ina219Register . Current , s_readDelays ( _shuntAdcResSamp ) ) ) * _currentLsb ) ;
274
294
}
275
295
276
296
/// <summary>
@@ -337,5 +357,17 @@ private void WriteRegister(Ina219Register register, ushort value)
337
357
// write the value to the register via the I2c Bus.
338
358
_i2cDevice . Write ( buffer ) ;
339
359
}
360
+
361
+ /// <summary>
362
+ /// Convert from 2's complement format
363
+ /// </summary>
364
+ /// <param name="twosComp">The value to be writtent to the register.</param>
365
+ /// <returns>Siged short integer representing decoded value.</returns>
366
+ private short ConvertFrom16BitTwosComplement ( ushort twosComp )
367
+ {
368
+ // convert from 2's cpmplement 24 bit to int (32 bit)
369
+ short normalValue = ( ( twosComp & 0x8000 ) != 0 ) ? ( short ) ( 0 - ( ( twosComp ^ 0xffff ) + 1 ) ) : ( short ) twosComp ;
370
+ return normalValue ;
371
+ }
340
372
}
341
373
}
0 commit comments