INA234 HAL Based Library 1.0.1
This library is a software library that works with the INA234 current, voltage, and power monitor chip. This library provides a convenient and efficient way to access the I2C interfaces of the chip, allowing developers to easily integrate this power meter into their systems.
Loading...
Searching...
No Matches
ina234.h File Reference
#include "main.h"
#include "i2c.h"

Go to the source code of this file.

Data Structures

struct  ina234
 Class (struct) that stores variables for interacting with INA234. More...
 
union  ina234::_reg
 
struct  ina234::_reg::_config_register
 
struct  ina234::_reg::_shunt_voltage_register
 
struct  ina234::_reg::_bus_voltage_register
 
struct  ina234::_reg::_power_register
 
struct  ina234::_reg::_current_register
 
struct  ina234::_reg::_calibration_register
 
struct  ina234::_reg::_mask_enable_register
 
struct  ina234::_reg::_alert_limit_register
 
struct  ina234::_reg::_manufacture_id_register
 
struct  ina234::_reg::_devide_id_register
 

Macros

#define MAXIMUM_EXPECTED_CURRENT   5.0
 
#define CURRENT_LSB_MINIMUM   (MAXIMUM_EXPECTED_CURRENT / 2048.0)
 
#define CURRENT_LSB   (CURRENT_LSB_MINIMUM * 1.0)
 
#define BUS_VOLTAGE_LSB   0.025
 
#define SHUNT_VOLTAGE_81_92mv_LSB   0.04
 
#define SHUNT_VOLTAGE_20_48mv_LSB   0.01
 
#define POWER_LSB   (CURRENT_LSB*0.032)
 
#define CONFIGURATION_REGISTER   0x00
 
#define SHUNT_VOLTAGE_REGISTER   0x01
 
#define BUS_VOLTAGE_REGISTER   0x02
 
#define POWER_REGISTER   0x03
 
#define CURRENT_REGISTER   0x04
 
#define CALIBRATION_REGISTER   0x05
 
#define MASK_ENABLE_REGISTER   0x06
 
#define ALERT_LIMIT_REGISTER   0x07
 
#define MANUFACTURERID_REGISTER   0x3E
 
#define DEVICEID_REGISTER   0x3F
 

Typedefs

typedef enum ADCRange ADCRange
 
typedef enum NumSamples NumSamples
 
typedef enum ConvTime ConvTime
 
typedef enum Mode Mode
 
typedef enum Status Status
 
typedef enum AlertOn AlertOn
 
typedef enum AlertPolarity AlertPolarity
 
typedef enum AlertLatch AlertLatch
 
typedef enum AlertConvReady AlertConvReady
 
typedef enum AlertSource AlertSource
 
typedef enum ErrorType ErrorType
 
typedef struct ina234 INA234
 Class (struct) that stores variables for interacting with INA234.
 

Enumerations

enum  ADCRange { RANGE_81_92mV , RANGE_20_48mV }
 
enum  NumSamples {
  NADC_1 , NADC_4 , NADC_16 , NADC_64 ,
  NADC_128 , NADC_256 , NADC_512 , NADC_1024
}
 
enum  ConvTime {
  CTIME_140us , CTIME_204us , CTIME_332us , CTIME_588us ,
  CTIME_1100us , CTIME_2116us , CTIME_4156us , CTIME_8244us
}
 
enum  Mode {
  MODE_SHUTDOWN , MODE_SINGLESHOT_SUNT , MODE_SINGLESHOT_BUS , MODE_SINGLESHOT_BOTH_SHUNT_BUS ,
  MODE_SHUTDOWN2 , MODE_CONTINUOUS_SHUNT , MODE_CONTINUOUS_BUS , MODE_CONTINUOUS_BOTH_SHUNT_BUS
}
 
enum  Status { STATUS_OK , STATUS_TimeOut }
 
enum  AlertOn {
  ALERT_NONE , ALERT_SHUNT_OVER_LIMIT , ALERT_SHUNT_UNDER_LIMIT , ALERT_BUS_OVER_LIMIT ,
  ALERT_BUS_UNDER_LIMIT , ALERT_POWER_OVER_LIMIT
}
 
enum  AlertPolarity { ALERT_ACTIVE_LOW , ALERT_ACTIVE_HIGH }
 
enum  AlertLatch { ALERT_TRANSPARENT , ALERT_LATCHED }
 
enum  AlertConvReady { ALERT_CONV_DISABLE , ALERT_CONV_ENABLE }
 
enum  AlertSource { ALERT_DATA_READY , ALERT_LIMIT_REACHED }
 
enum  ErrorType { ERROR_NONE , ERROR_MEMORY , ERROR_OVF , ERROR_BOTH_MEMORY_OVF }
 

Functions

Status INA234_init (INA234 *self, uint8_t I2C_ADDR, I2C_HandleTypeDef *hi2c, float ShuntResistor, ADCRange adc_range, NumSamples numer_of_adc_samples, ConvTime vbus_conversion_time, ConvTime vshunt_conversion_time, Mode mode)
 Initialize the INA234 with the given config.
 
Status INA234_alert_init (INA234 *self, AlertOn alert_on, AlertPolarity alert_polarity, AlertLatch alert_latch, AlertConvReady alert_conv_ready, float alert_limit)
 Initialize the alert functionality of INA234 with the given configurations.
 
Status __INA234_readTwoBytes (INA234 *self, uint8_t MemAddress)
 Read two bytes (a 16bit register) from INA234 and stores in the ina234::_reg::raw_data.
 
Status __INA234_writeTwoBytes (INA234 *self, uint8_t MemAddress)
 Write two bytes (a 16bit register) to INA234 from the ina234::_reg::raw_data.
 
Status INA234_setADCRange (INA234 *self, ADCRange adc_range)
 Set the ADC full scale range of INA234.
 
Status INA234_setNumberOfADCSamples (INA234 *self, NumSamples numer_of_adc_samples)
 Set the number of ADC samples to calculate the average.
 
Status INA234_setVBusConversionTime (INA234 *self, ConvTime vbus_conversion_time)
 Set the VBus convertion period.
 
Status INA234_setVShuntConversionTime (INA234 *self, ConvTime vshunt_conversion_time)
 Set the VShunt convertion period.
 
Status INA234_setMode (INA234 *self, Mode mode)
 Set the operating mode.
 
ADCRange INA234_getADCRange (INA234 *self)
 Get the ADC full scale range of INA234.
 
NumSamples INA234_getNumberOfADCSamples (INA234 *self)
 Get the number of ADC samples to calculate the average.
 
ConvTime INA234_getVBusConversionTime (INA234 *self)
 Get the VBus convertion period.
 
ConvTime INA234_getVShuntConversionTime (INA234 *self)
 Get the VShunt convertion period.
 
Mode INA234_getMode (INA234 *self)
 Set the operating mode.
 
void INA234_SoftResetAll (INA234 *self)
 Send a reset command to all of the INA234s on the bus.
 
uint16_t INA234_getManID (INA234 *self)
 Get the manufacturer ID.
 
uint16_t INA234_getDevID (INA234 *self)
 Get the device ID.
 
void INA234_readAll (INA234 *self)
 Read all of the measured values: Shunt voltage, bus voltage, power, and current. Then store the values to the ina234 object (struct) variables. Then you can read variables: ina234::ShuntVoltage, ina234::BusVoltage, ina234::Power, and ina234::Current.
 
float INA234_getCurrent (INA234 *self)
 Read the current from INA234.
 
float INA234_getBusVoltage (INA234 *self)
 Read the bus voltage from INA234.
 
float INA234_getShuntVoltage (INA234 *self)
 Read the shunt voltage from INA234.
 
float INA234_getPower (INA234 *self)
 Read the power from INA234.
 
uint8_t INA234_isDataReady (INA234 *self)
 Check if the conversion is done or not. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA234_resetAlert() function.
 
AlertSource INA234_getAlertSource (INA234 *self)
 Get the alert source. This function is usefull when you enabled both of the alert functions and data ready alert simultaneously. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA234_resetAlert() function.
 
ErrorType INA234_getErrors (INA234 *self)
 Get the error flags of INA234. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA234_resetAlert() function.
 
Status INA234_resetAlert (INA234 *self)
 Reset the alert pin. This function is useful when set the alert pin to latch mode.
 

Macro Definition Documentation

◆ ALERT_LIMIT_REGISTER

#define ALERT_LIMIT_REGISTER   0x07

◆ BUS_VOLTAGE_LSB

#define BUS_VOLTAGE_LSB   0.025

◆ BUS_VOLTAGE_REGISTER

#define BUS_VOLTAGE_REGISTER   0x02

◆ CALIBRATION_REGISTER

#define CALIBRATION_REGISTER   0x05

◆ CONFIGURATION_REGISTER

#define CONFIGURATION_REGISTER   0x00

◆ CURRENT_LSB

#define CURRENT_LSB   (CURRENT_LSB_MINIMUM * 1.0)

◆ CURRENT_LSB_MINIMUM

#define CURRENT_LSB_MINIMUM   (MAXIMUM_EXPECTED_CURRENT / 2048.0)

◆ CURRENT_REGISTER

#define CURRENT_REGISTER   0x04

◆ DEVICEID_REGISTER

#define DEVICEID_REGISTER   0x3F

◆ MANUFACTURERID_REGISTER

#define MANUFACTURERID_REGISTER   0x3E

◆ MASK_ENABLE_REGISTER

#define MASK_ENABLE_REGISTER   0x06

◆ MAXIMUM_EXPECTED_CURRENT

#define MAXIMUM_EXPECTED_CURRENT   5.0

◆ POWER_LSB

#define POWER_LSB   (CURRENT_LSB*0.032)

◆ POWER_REGISTER

#define POWER_REGISTER   0x03

◆ SHUNT_VOLTAGE_20_48mv_LSB

#define SHUNT_VOLTAGE_20_48mv_LSB   0.01

◆ SHUNT_VOLTAGE_81_92mv_LSB

#define SHUNT_VOLTAGE_81_92mv_LSB   0.04

◆ SHUNT_VOLTAGE_REGISTER

#define SHUNT_VOLTAGE_REGISTER   0x01

Typedef Documentation

◆ ADCRange

typedef enum ADCRange ADCRange

◆ AlertConvReady

◆ AlertLatch

typedef enum AlertLatch AlertLatch

◆ AlertOn

typedef enum AlertOn AlertOn

◆ AlertPolarity

◆ AlertSource

typedef enum AlertSource AlertSource

◆ ConvTime

typedef enum ConvTime ConvTime

◆ ErrorType

typedef enum ErrorType ErrorType

◆ INA234

typedef struct ina234 INA234

Class (struct) that stores variables for interacting with INA234.

◆ Mode

typedef enum Mode Mode

◆ NumSamples

typedef enum NumSamples NumSamples

◆ Status

typedef enum Status Status

Enumeration Type Documentation

◆ ADCRange

enum ADCRange
Enumerator
RANGE_81_92mV 
RANGE_20_48mV 

◆ AlertConvReady

Enumerator
ALERT_CONV_DISABLE 
ALERT_CONV_ENABLE 

◆ AlertLatch

enum AlertLatch
Enumerator
ALERT_TRANSPARENT 
ALERT_LATCHED 

◆ AlertOn

enum AlertOn
Enumerator
ALERT_NONE 
ALERT_SHUNT_OVER_LIMIT 
ALERT_SHUNT_UNDER_LIMIT 
ALERT_BUS_OVER_LIMIT 
ALERT_BUS_UNDER_LIMIT 
ALERT_POWER_OVER_LIMIT 

◆ AlertPolarity

Enumerator
ALERT_ACTIVE_LOW 
ALERT_ACTIVE_HIGH 

◆ AlertSource

Enumerator
ALERT_DATA_READY 
ALERT_LIMIT_REACHED 

◆ ConvTime

enum ConvTime
Enumerator
CTIME_140us 
CTIME_204us 
CTIME_332us 
CTIME_588us 
CTIME_1100us 
CTIME_2116us 
CTIME_4156us 
CTIME_8244us 

◆ ErrorType

enum ErrorType
Enumerator
ERROR_NONE 
ERROR_MEMORY 
ERROR_OVF 
ERROR_BOTH_MEMORY_OVF 

◆ Mode

enum Mode
Enumerator
MODE_SHUTDOWN 
MODE_SINGLESHOT_SUNT 
MODE_SINGLESHOT_BUS 
MODE_SINGLESHOT_BOTH_SHUNT_BUS 
MODE_SHUTDOWN2 
MODE_CONTINUOUS_SHUNT 
MODE_CONTINUOUS_BUS 
MODE_CONTINUOUS_BOTH_SHUNT_BUS 

◆ NumSamples

enum NumSamples
Enumerator
NADC_1 
NADC_4 
NADC_16 
NADC_64 
NADC_128 
NADC_256 
NADC_512 
NADC_1024 

◆ Status

enum Status
Enumerator
STATUS_OK 
STATUS_TimeOut 

Function Documentation

◆ __INA234_readTwoBytes()

Status __INA234_readTwoBytes ( INA234 * self,
uint8_t MemAddress )

Read two bytes (a 16bit register) from INA234 and stores in the ina234::_reg::raw_data.

Parameters
selfA pointer to the ina234 object (struct)
MemAddressAddress of the register
Returns
Ths status of reading
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ __INA234_writeTwoBytes()

Status __INA234_writeTwoBytes ( INA234 * self,
uint8_t MemAddress )

Write two bytes (a 16bit register) to INA234 from the ina234::_reg::raw_data.

Parameters
selfA pointer to the ina234 object (struct)
MemAddressAddress of the register
Returns
Ths status of writing
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_alert_init()

Status INA234_alert_init ( INA234 * self,
AlertOn alert_on,
AlertPolarity alert_polarity,
AlertLatch alert_latch,
AlertConvReady alert_conv_ready,
float alert_limit )

Initialize the alert functionality of INA234 with the given configurations.

Parameters
selfA pointer to the ina234 object (struct)
alert_ondetermines the event that you want to assert the alert:
alert_polarityThe alert polarity determines the polarity of alert assertion:
  • ALERT_ACTIVE_LOW Alert pin goes low on alert
  • ALERT_ACTIVE_HIGH Alert pin goes HiZ on alert (alert pin is open-drain so you have to add a pull-up resistor to get high state)
alert_latchDetermine the alert pin behaviour. If the latch is enabled, you have to reset the pin by calling the INA234_resetAlert() function.
alert_conv_readyShows if you want to get alert on "conversion done"/"data ready" too or not. If enabled, you have to distinguish the alert sourece by calling INA234_getAlertSource() function.
alert_limitThis the limit value. It automatically maps to the alert function you have chosen. The unit of this limit is related to the alert_on argument:

For example if the alert_limit was 10.4 and you give ALERT_SHUNT_OVER_LIMIT for the alert_on argument, it means you will get alert if the shunt voltage reaches over the 10.4mV. If you give ALERT_BUS_OVER_LIMIT to alert_on, it means you will get alert if the bus voltage reaches the 10.4V

Returns
Ths status of initialization
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_getADCRange()

ADCRange INA234_getADCRange ( INA234 * self)

Get the ADC full scale range of INA234.

Parameters
selfA pointer to the ina234 object (struct)
Returns
The full scale range of ADC.
Return values
RANGE_81_92mVfor 81.92 mV
RANGE_20_48mVfor 20.48 mV

◆ INA234_getAlertSource()

AlertSource INA234_getAlertSource ( INA234 * self)

Get the alert source. This function is usefull when you enabled both of the alert functions and data ready alert simultaneously. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA234_resetAlert() function.

Parameters
selfA pointer to the ina234 object (struct)
Returns
The alert source
Return values
ALERT_DATA_READYThe alert source is convertion ready
ALERT_LIMIT_REACHEDThe alert source is limit reach

◆ INA234_getBusVoltage()

float INA234_getBusVoltage ( INA234 * self)

Read the bus voltage from INA234.

Parameters
selfA pointer to the ina234 object (struct)
Returns
a float value in Volts representing the bus voltage

◆ INA234_getCurrent()

float INA234_getCurrent ( INA234 * self)

Read the current from INA234.

Parameters
selfA pointer to the ina234 object (struct)
Returns
a float value in Amps representing the current

◆ INA234_getDevID()

uint16_t INA234_getDevID ( INA234 * self)

Get the device ID.

Parameters
selfA pointer to the ina234 object (struct)
Returns
a 12bit device ID

◆ INA234_getErrors()

ErrorType INA234_getErrors ( INA234 * self)

Get the error flags of INA234. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA234_resetAlert() function.

Parameters
selfA pointer to the ina234 object (struct)
Returns
Error type
Return values
ERROR_NONENo error
ERROR_MEMORYMemory error (CRC or ECC)
ERROR_OVFMath overflow error
ERROR_BOTH_MEMORY_OVFBoth memory error (CRC or ECC) and math overflow error

◆ INA234_getManID()

uint16_t INA234_getManID ( INA234 * self)

Get the manufacturer ID.

Parameters
selfA pointer to the ina234 object (struct)
Returns
a 16bit manufacturer ID

◆ INA234_getMode()

Mode INA234_getMode ( INA234 * self)

Set the operating mode.

Parameters
selfA pointer to the ina234 object (struct)
Returns
Operating mode
Return values
MODE_SHUTDOWNshutdown mode
MODE_SINGLESHOT_SUNTonly measure the shunt voltage once
MODE_SINGLESHOT_BUSonly measure the bus voltage once
MODE_SINGLESHOT_BOTH_SHUNT_BUSmeasure the bus and shunt voltage once
MODE_CONTINUOUS_SHUNTonly measure the shunt voltage continuously
MODE_CONTINUOUS_BUSonly measure the bus voltage continuously
MODE_CONTINUOUS_BOTH_SHUNT_BUSmeasure the bus and shunt voltage continuously

◆ INA234_getNumberOfADCSamples()

NumSamples INA234_getNumberOfADCSamples ( INA234 * self)

Get the number of ADC samples to calculate the average.

Parameters
selfA pointer to the ina234 object (struct)
Returns
Numer of ADC samples to calculate the average. The higher ADC samples leads to lower noises and higher latency.
Return values
NADC_1for one sample (no average)
NADC_44 samples
NADC_1616 samples
NADC_6464 samples
NADC_128128 samples
NADC_256256 samples
NADC_512512 samples
NADC_10241024 samples

◆ INA234_getPower()

float INA234_getPower ( INA234 * self)

Read the power from INA234.

Parameters
selfA pointer to the ina234 object (struct)
Returns
a float value in Watt representing the power

◆ INA234_getShuntVoltage()

float INA234_getShuntVoltage ( INA234 * self)

Read the shunt voltage from INA234.

Parameters
selfA pointer to the ina234 object (struct)
Returns
a float value in miliVolts representing the shunt voltage

◆ INA234_getVBusConversionTime()

ConvTime INA234_getVBusConversionTime ( INA234 * self)

Get the VBus convertion period.

Parameters
selfA pointer to the ina234 object (struct)
Returns
The conversion time of VBus measurment.
Return values
CTIME_140us
CTIME_204us
CTIME_332us
CTIME_588us
CTIME_1100us
CTIME_2116us
CTIME_4156us
CTIME_8244us

◆ INA234_getVShuntConversionTime()

ConvTime INA234_getVShuntConversionTime ( INA234 * self)

Get the VShunt convertion period.

Parameters
selfA pointer to the ina234 object (struct)
Returns
The conversion time of VShunt measurment.
Return values
CTIME_140us
CTIME_204us
CTIME_332us
CTIME_588us
CTIME_1100us
CTIME_2116us
CTIME_4156us
CTIME_8244us

◆ INA234_init()

Status INA234_init ( INA234 * self,
uint8_t I2C_ADDR,
I2C_HandleTypeDef * hi2c,
float ShuntResistor,
ADCRange adc_range,
NumSamples numer_of_adc_samples,
ConvTime vbus_conversion_time,
ConvTime vshunt_conversion_time,
Mode mode )

Initialize the INA234 with the given config.

Parameters
selfA pointer to the ina234 object (struct)
I2C_ADDRThe I2C address of the INA234. It depends on the state of A0 pin of the chip.
hi2cA pointer to the I2C handler that is connected to INA234
ShuntResistorThe resistance of your shunt resistor (in mOhm) connected to IN+ and IN- of the INA234
adc_rangeThe full scale range of ADC. It can be one of these values:
numer_of_adc_samplesNumer of ADC samples to calculate the average. The higher ADC samples leads to lower noises and higher latency.
vbus_conversion_timeThe conversion time of VBus measurment.
vshunt_conversion_timeThe conversion time of VShunt measurment.
modeOperating mode:
Returns
Ths status of initialization
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_isDataReady()

uint8_t INA234_isDataReady ( INA234 * self)

Check if the conversion is done or not. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA234_resetAlert() function.

Parameters
selfA pointer to the ina234 object (struct)
Return values
True
False

◆ INA234_readAll()

void INA234_readAll ( INA234 * self)

Read all of the measured values: Shunt voltage, bus voltage, power, and current. Then store the values to the ina234 object (struct) variables. Then you can read variables: ina234::ShuntVoltage, ina234::BusVoltage, ina234::Power, and ina234::Current.

Parameters
selfA pointer to the ina234 object (struct)

◆ INA234_resetAlert()

Status INA234_resetAlert ( INA234 * self)

Reset the alert pin. This function is useful when set the alert pin to latch mode.

Parameters
selfA pointer to the ina234 object (struct)
Returns
Ths status of reset
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_setADCRange()

Status INA234_setADCRange ( INA234 * self,
ADCRange adc_range )

Set the ADC full scale range of INA234.

Parameters
selfA pointer to the ina234 object (struct)
adc_rangeThe full scale range of ADC. It can be one of these values:
Returns
Ths status of config
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_setMode()

Status INA234_setMode ( INA234 * self,
Mode mode )

Set the operating mode.

Parameters
selfA pointer to the ina234 object (struct)
modeOperating mode:
Returns
Ths status of config
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_setNumberOfADCSamples()

Status INA234_setNumberOfADCSamples ( INA234 * self,
NumSamples numer_of_adc_samples )

Set the number of ADC samples to calculate the average.

Parameters
selfA pointer to the ina234 object (struct)
numer_of_adc_samplesNumer of ADC samples to calculate the average. The higher ADC samples leads to lower noises and higher latency.
Returns
Ths status of config
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_setVBusConversionTime()

Status INA234_setVBusConversionTime ( INA234 * self,
ConvTime vbus_conversion_time )

Set the VBus convertion period.

Parameters
selfA pointer to the ina234 object (struct)
vbus_conversion_timeThe conversion time of VBus measurment.
Returns
Ths status of config
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_setVShuntConversionTime()

Status INA234_setVShuntConversionTime ( INA234 * self,
ConvTime vshunt_conversion_time )

Set the VShunt convertion period.

Parameters
selfA pointer to the ina234 object (struct)
vshunt_conversion_timeThe conversion time of VShunt measurment.
Returns
Ths status of config
Return values
STATUS_OKin case of success
STATUS_TimeOutin case of failure

◆ INA234_SoftResetAll()

void INA234_SoftResetAll ( INA234 * self)

Send a reset command to all of the INA234s on the bus.

Parameters
selfA pointer to the ina234 object (struct)