Class Ina219
A Texas Instruments INA219 current, voltage, and power monitor, and a driver for one on an I2C bus.
public sealed class Ina219 : IDisposable
- Inheritance
-
Ina219
- Implements
- Inherited Members
Remarks
The static members are the part's datasheet, decoded exactly as the manufacturer specifies, and Ina219.Sim, a part that is not there.
An instance drives one part over an I2cBus: it resets the part, writes the configuration, programs the calibration for the shunt and the current it is sized for, and reads the calibration back, the identity check a part with no id register allows. Each Measure() triggers one shunt and bus conversion. Nothing is sent until Init() or the first Measure().
Constructors
Ina219(I2cBus, byte, uint, uint, uint, Ina219Config?)
Creates a driver for the part at address on bus.
public Ina219(I2cBus bus, byte address, uint shuntMilliohms = 100, uint maxMicroamps = 3200000, uint currentLsbMicroamps = 0, Ina219Config? config = null)
Parameters
busI2cBusThe bus the part is on.
addressbyteBaseAddress plus what A1 and A0 add.
shuntMilliohmsuintThe shunt resistance in milliohms; the common breakout's is 100.
maxMicroampsuintThe largest current the shunt will carry, which sets the finest current step.
currentLsbMicroampsuintA current step to use instead, such as a round 100, or 0 for the finest.
configIna219Config?The range, gain, and converter settings, or null for PowerOn.
Exceptions
- PamojaException
The native driver could not be created.
Fields
BaseAddress
The address with A1 and A0 tied to ground; the pins add to it.
public const byte BaseAddress = 64
Field Value
ConfigReset
The configuration register's power-on value.
public const ushort ConfigReset = 14751
Field Value
Properties
CalibrationWord
The calibration word the driver programs.
public ushort CalibrationWord { get; }
Property Value
CurrentLsbMicroamps
The current step the driver programs, in microamps per count.
public uint CurrentLsbMicroamps { get; }
Property Value
Methods
AdcConversionMicros(Adc)
Returns how long one conversion takes at a converter setting.
public static uint AdcConversionMicros(Ina219.Adc adc)
Parameters
Returns
- uint
The time in microseconds, from the datasheet's table.
Address(AddressPin, AddressPin)
Returns the 7-bit address the A1 and A0 pins select, from Table 1 of the datasheet.
public static byte Address(Ina219.AddressPin a1, Ina219.AddressPin a0)
Parameters
a1Ina219.AddressPinWhat the A1 pin is tied to.
a0Ina219.AddressPinWhat the A0 pin is tied to.
Returns
- byte
The address, 0x40 to 0x4F.
Exceptions
- PamojaException
A pin code is not one of the four levels.
BusMillivolts(ushort)
Converts a raw bus-voltage register to millivolts.
public static uint BusMillivolts(ushort raw)
Parameters
rawushortThe register value.
Returns
- uint
The bus voltage.
BusRegister(uint)
Builds the bus-voltage register a monitor reports for a bus voltage.
public static ushort BusRegister(uint millivolts)
Parameters
millivoltsuintThe bus voltage in millivolts.
Returns
- ushort
The register value, with the conversion-ready flag set.
Calibration(uint, uint)
Computes the calibration register for a shunt and resolution.
public static ushort Calibration(uint currentLsbMicroamps, uint shuntMilliohms)
Parameters
currentLsbMicroampsuintThe microamps per count wanted.
shuntMilliohmsuintThe shunt resistor value.
Returns
- ushort
The register value to write.
ConfigBits(Ina219Config)
Assembles the configuration register value.
public static ushort ConfigBits(Ina219Config config)
Parameters
configIna219ConfigThe settings.
Returns
- ushort
The register value to write.
ConfigFromBits(ushort)
Parses a configuration register value.
public static Ina219Config ConfigFromBits(ushort bits)
Parameters
bitsushortThe register value, as read from the part.
Returns
- Ina219Config
The settings. Every value decodes.
ConversionMicros(Ina219Config)
Returns how long one conversion cycle takes: the shunt and bus conversions the mode runs.
public static uint ConversionMicros(Ina219Config config)
Parameters
configIna219ConfigThe settings.
Returns
- uint
The time in microseconds.
ConversionReady(ushort)
Reports whether a bus-voltage register says a conversion is ready.
public static bool ConversionReady(ushort raw)
Parameters
rawushortThe register value.
Returns
- bool
Whether the conversion-ready flag is set.
CurrentMicroamps(short, uint)
Converts a raw current register to microamps.
public static int CurrentMicroamps(short raw, uint currentLsbMicroamps)
Parameters
rawshortThe signed register value.
currentLsbMicroampsuintThe resolution the calibration selected.
Returns
- int
The current.
CurrentRegister(int, uint)
Builds the current register a monitor reports for a current.
public static short CurrentRegister(int microamps, uint currentLsbMicroamps)
Parameters
microampsintThe current in microamps.
currentLsbMicroampsuintThe current LSB the calibration was set for.
Returns
- short
The signed register value.
Dispose()
Releases the driver and its share of the bus.
public void Dispose()
GainRangeMillivolts(Gain)
Returns the shunt-voltage range a gain selects.
public static ushort GainRangeMillivolts(Ina219.Gain gain)
Parameters
Returns
- ushort
The range in millivolts either side of zero.
Init()
Resets the part and programs the configuration and calibration.
public void Init()
Exceptions
- PamojaException
Nothing answered, or the calibration did not read back.
MathOverflow(ushort)
Reports whether a bus-voltage register flags a math overflow.
public static bool MathOverflow(ushort raw)
Parameters
rawushortThe register value.
Returns
- bool
Whether the current and power readings are meaningless, which means the calibration needs revisiting.
Measure()
Triggers one shunt and bus conversion and reads every result, initializing the part first if needed.
public Ina219Reading Measure()
Returns
- Ina219Reading
The registers as read, and what they mean.
Exceptions
- PamojaException
As Init(), and when the conversion-ready flag never sets.
MinimumCurrentLsbMicroamps(uint)
Returns the smallest resolution that still covers a maximum.
public static uint MinimumCurrentLsbMicroamps(uint maxExpectedMicroamps)
Parameters
maxExpectedMicroampsuintThe largest current measured.
Returns
- uint
The minimum current LSB in microamps.
PowerMicrowatts(ushort, uint)
Converts a raw power register to microwatts.
public static uint PowerMicrowatts(ushort raw, uint currentLsbMicroamps)
Parameters
Returns
- uint
The power. The power LSB is fixed at twenty times the current LSB.
PowerRegister(uint, uint)
Builds the power register a monitor reports for a power.
public static ushort PowerRegister(uint microwatts, uint currentLsbMicroamps)
Parameters
microwattsuintThe power in microwatts.
currentLsbMicroampsuintThe current LSB the calibration was set for.
Returns
- ushort
The register value.
ShuntMicrovolts(short)
Converts a raw shunt-voltage register to microvolts.
public static int ShuntMicrovolts(short raw)
Parameters
rawshortThe signed register value.
Returns
- int
The shunt voltage.
ShuntRegister(int)
Builds the shunt-voltage register a monitor reports for a shunt voltage.
public static short ShuntRegister(int microvolts)
Parameters
microvoltsintThe shunt voltage in microvolts.
Returns
- short
The signed register value, at 10 uV per count.
Remarks
The inverse of ShuntMicrovolts(short), so a node can be written and tested against what a monitor sends without one attached.