Table of Contents

Class Ina226

Namespace
Pamoja.Sensors
Assembly
Pamoja.Sensors.dll

A Texas Instruments INA226 current, voltage, and power monitor, and a driver for one on an I2C bus.

public sealed class Ina226 : IDisposable
Inheritance
Ina226
Implements
Inherited Members

Remarks

The static members are the part's datasheet, decoded exactly as the manufacturer specifies, and Ina226.Sim, a part that is not there.

An instance drives one part over an I2cBus: it resets the part, checks its identity, programs the calibration for the shunt and the current it is sized for, and triggers one shunt and bus conversion per Measure(). Nothing is sent until Init() or the first Measure().

Constructors

Ina226(I2cBus, byte, uint, uint, uint, Ina226Config?)

Creates a driver for the part at address on bus.

public Ina226(I2cBus bus, byte address, uint shuntMilliohms = 100, uint maxMicroamps = 3200000, uint currentLsbMicroamps = 0, Ina226Config? config = null)

Parameters

bus I2cBus

The bus the part is on.

address byte

The address A1 and A0 select, which Address(AddressPin, AddressPin) works out.

shuntMilliohms uint

The shunt resistance in milliohms.

maxMicroamps uint

The largest current the shunt will carry, which sets the finest current step.

currentLsbMicroamps uint

A current step to use instead, such as a round 100, or 0 for the finest.

config Ina226Config?

The averaging, conversion times, and mode, or null for PowerOn.

Exceptions

PamojaException

The native driver could not be created.

Properties

CalibrationWord

The calibration word the driver programs.

public ushort CalibrationWord { get; }

Property Value

ushort

CurrentLsbMicroamps

The current step the driver programs, in microamps per count.

public uint CurrentLsbMicroamps { get; }

Property Value

uint

Identity

The die id read at initialization, or null before it.

public PamojaIna226DieId? Identity { get; }

Property Value

PamojaIna226DieId?

Methods

ActiveAlertFunction(PamojaIna226MaskEnable)

Returns the alert function an INA226 pin actually responds to.

public static PamojaIna226AlertFunction? ActiveAlertFunction(PamojaIna226MaskEnable mask)

Parameters

mask PamojaIna226MaskEnable

The mask.

Returns

PamojaIna226AlertFunction?

The value, or null where the part defines none.

Address(AddressPin, AddressPin)

Returns the 7-bit address the A1 and A0 pins select.

public static byte Address(Ina226.AddressPin a1, Ina226.AddressPin a0)

Parameters

a1 Ina226.AddressPin

What the A1 pin is tied to.

a0 Ina226.AddressPin

What the A0 pin is tied to.

Returns

byte

The address, 0x40 to 0x4F.

Address(byte, byte)

Returns the I2C address an INA226's A1 and A0 pin codes select.

public static byte Address(byte a1, byte a0)

Parameters

a1 byte

The a1.

a0 byte

The a0.

Returns

byte

The decoded value.

Exceptions

PamojaException

The device data is not valid.

AveragingSamples(byte)

Returns how many samples an INA226 averaging code folds into one result.

public static ushort AveragingSamples(byte code)

Parameters

code byte

The code.

Returns

ushort

The value the C ABI computes.

BusMicrovolts(ushort)

Converts a raw INA226 bus-voltage register to microvolts.

public static uint BusMicrovolts(ushort raw)

Parameters

raw ushort

The raw.

Returns

uint

The value the C ABI computes.

BusRegister(uint)

Builds the INA226 bus-voltage register a monitor reports for a bus voltage.

public static ushort BusRegister(uint microvolts)

Parameters

microvolts uint

The microvolts.

Returns

ushort

The value the C ABI computes.

BusVolts(ushort)

Converts a raw INA226 bus-voltage register to volts.

public static float BusVolts(ushort raw)

Parameters

raw ushort

The raw.

Returns

float

The value the C ABI computes.

Calibration(uint, uint)

Computes the INA226 calibration register for a shunt and current resolution.

public static ushort Calibration(uint currentLsbMicroamps, uint shuntMilliohms)

Parameters

currentLsbMicroamps uint

The current lsb microamps.

shuntMilliohms uint

The shunt milliohms.

Returns

ushort

The value the C ABI computes.

ConfigFromRegister(ushort)

Parses an INA226 configuration register value.

public static PamojaIna226Config ConfigFromRegister(ushort raw)

Parameters

raw ushort

The raw.

Returns

PamojaIna226Config

The decoded value.

Exceptions

PamojaException

The device data is not valid.

ConfigToRegister(PamojaIna226Config)

Assembles an INA226 configuration register value.

public static ushort ConfigToRegister(PamojaIna226Config config)

Parameters

config PamojaIna226Config

The config.

Returns

ushort

The value the C ABI computes.

ConfigToRegister(Ina226Config)

Assembles the configuration register from named settings.

public static ushort ConfigToRegister(Ina226Config config)

Parameters

config Ina226Config

The settings.

Returns

ushort

The register value.

ConversionMicros(byte)

Returns the conversion time an INA226 code selects.

public static uint ConversionMicros(byte code)

Parameters

code byte

The code.

Returns

uint

The value the C ABI computes.

CurrentAmps(short, uint)

Converts a raw INA226 current register to amps.

public static float CurrentAmps(short raw, uint currentLsbMicroamps)

Parameters

raw short

The raw.

currentLsbMicroamps uint

The current lsb microamps.

Returns

float

The value the C ABI computes.

CurrentMicroamps(short, uint)

Converts a raw INA226 current register to microamps.

public static int CurrentMicroamps(short raw, uint currentLsbMicroamps)

Parameters

raw short

The raw.

currentLsbMicroamps uint

The current lsb microamps.

Returns

int

The value the C ABI computes.

CurrentRegister(int, uint)

Builds the INA226 current register a monitor reports for a current.

public static short CurrentRegister(int microamps, uint currentLsbMicroamps)

Parameters

microamps int

The microamps.

currentLsbMicroamps uint

The current lsb microamps.

Returns

short

The value the C ABI computes.

CurrentRegisterFromShunt(short, ushort)

Computes the INA226 current register the chip derives from a shunt reading.

public static short CurrentRegisterFromShunt(short shunt, ushort calibration)

Parameters

shunt short

The shunt.

calibration ushort

The calibration.

Returns

short

The value the C ABI computes.

DieId(ushort)

Splits an INA226 die-ID register into its device and revision fields.

public static PamojaIna226DieId DieId(ushort raw)

Parameters

raw ushort

The raw.

Returns

PamojaIna226DieId

The decoded value.

Exceptions

PamojaException

The device data is not valid.

Dispose()

Releases the driver and its share of the bus.

public void Dispose()

Identify(ushort, ushort)

Checks that a pair of identification registers belongs to an INA226.

public static PamojaIna226DieId Identify(ushort manufacturerId, ushort dieId)

Parameters

manufacturerId ushort

The manufacturer id.

dieId ushort

The die id.

Returns

PamojaIna226DieId

The decoded value.

Exceptions

PamojaException

The device data is not valid.

Init()

Resets the part, checks it is an INA226, and programs the configuration and calibration.

public void Init()

Exceptions

PamojaException

Nothing answered, the id registers are not an INA226's, or the calibration did not read back.

IsContinuous(byte)

Reports whether an INA226 mode code keeps converting after the first result.

public static bool IsContinuous(byte code)

Parameters

code byte

The code.

Returns

bool

The value the C ABI computes.

MaskEnableFromRegister(ushort)

Parses an INA226 Mask/Enable register value.

public static PamojaIna226MaskEnable MaskEnableFromRegister(ushort raw)

Parameters

raw ushort

The raw.

Returns

PamojaIna226MaskEnable

The decoded value.

Exceptions

PamojaException

The device data is not valid.

MaskEnableToRegister(PamojaIna226MaskEnable)

Assembles an INA226 Mask/Enable register value.

public static ushort MaskEnableToRegister(PamojaIna226MaskEnable mask)

Parameters

mask PamojaIna226MaskEnable

The mask.

Returns

ushort

The value the C ABI computes.

Measure()

Triggers one shunt and bus conversion and reads every result, initializing the part first if needed.

public Ina226Reading Measure()

Returns

Ina226Reading

The registers as read, and what they mean.

Exceptions

PamojaException

As Init(), and when the conversion-ready flag never sets.

MeasuresBus(byte)

Reports whether an INA226 mode code converts the bus voltage.

public static bool MeasuresBus(byte code)

Parameters

code byte

The code.

Returns

bool

The value the C ABI computes.

MeasuresShunt(byte)

Reports whether an INA226 mode code converts the shunt voltage.

public static bool MeasuresShunt(byte code)

Parameters

code byte

The code.

Returns

bool

The value the C ABI computes.

MinimumCurrentLsbMicroamps(uint)

Returns the smallest current resolution that still covers an expected maximum.

public static uint MinimumCurrentLsbMicroamps(uint maxExpectedMicroamps)

Parameters

maxExpectedMicroamps uint

The max expected microamps.

Returns

uint

The value the C ABI computes.

PowerMicrowatts(ushort, uint)

Converts a raw INA226 power register to microwatts.

public static uint PowerMicrowatts(ushort raw, uint currentLsbMicroamps)

Parameters

raw ushort

The raw.

currentLsbMicroamps uint

The current lsb microamps.

Returns

uint

The value the C ABI computes.

PowerRegister(uint, uint)

Builds the INA226 power register a monitor reports for a power.

public static ushort PowerRegister(uint microwatts, uint currentLsbMicroamps)

Parameters

microwatts uint

The microwatts.

currentLsbMicroamps uint

The current lsb microamps.

Returns

ushort

The value the C ABI computes.

PowerRegisterFromCurrent(short, ushort)

Computes the INA226 power register the chip derives from a current reading.

public static ushort PowerRegisterFromCurrent(short current, ushort bus)

Parameters

current short

The current.

bus ushort

The bus.

Returns

ushort

The value the C ABI computes.

PowerWatts(ushort, uint)

Converts a raw INA226 power register to watts.

public static float PowerWatts(ushort raw, uint currentLsbMicroamps)

Parameters

raw ushort

The raw.

currentLsbMicroamps uint

The current lsb microamps.

Returns

float

The value the C ABI computes.

SetAlert(PamojaIna226MaskEnable, ushort)

Programs the alert pin: which limit it watches, and the limit.

public void SetAlert(PamojaIna226MaskEnable mask, ushort limit)

Parameters

mask PamojaIna226MaskEnable

The mask/enable settings, one alert function at a time.

limit ushort

The alert-limit register, in the units of the register the function watches.

Exceptions

PamojaException

The transfer failed.

ShuntMillivolts(short)

Converts a raw INA226 shunt-voltage register to millivolts.

public static float ShuntMillivolts(short raw)

Parameters

raw short

The raw.

Returns

float

The value the C ABI computes.

ShuntNanovolts(short)

Converts a raw INA226 shunt-voltage register to nanovolts.

public static int ShuntNanovolts(short raw)

Parameters

raw short

The raw.

Returns

int

The value the C ABI computes.

ShuntRegister(int)

Builds the INA226 shunt-voltage register a monitor reports for a shunt voltage.

public static short ShuntRegister(int nanovolts)

Parameters

nanovolts int

The nanovolts.

Returns

short

The value the C ABI computes.

UpdateMicros(PamojaIna226Config)

Returns how often an INA226 in a configuration updates its results.

public static uint UpdateMicros(PamojaIna226Config config)

Parameters

config PamojaIna226Config

The config.

Returns

uint

The value the C ABI computes.

UpdateMicros(Ina226Config)

Returns how often an INA226 with named settings updates its results.

public static uint UpdateMicros(Ina226Config config)

Parameters

config Ina226Config

The settings.

Returns

uint

The update interval in microseconds.