|
INA236 HAL Based Library 1.0.1
This library is a software library that works with the INA236 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.
|
#include "main.h"#include "i2c.h"Go to the source code of this file.
Data Structures | |
| struct | INA236 |
| Class (struct) that stores variables for interacting with INA236. More... | |
| union | INA236::_reg |
| struct | INA236::_reg::_config_register |
| struct | INA236::_reg::_shunt_voltage_register |
| struct | INA236::_reg::_bus_voltage_register |
| struct | INA236::_reg::_power_register |
| struct | INA236::_reg::_current_register |
| struct | INA236::_reg::_calibration_register |
| struct | INA236::_reg::_mask_enable_register |
| struct | INA236::_reg::_alert_limit_register |
| struct | INA236::_reg::_manufacture_id_register |
| struct | INA236::_reg::_devide_id_register |
Macros | |
| #define | MAXIMUM_EXPECTED_CURRENT 5.0 |
| #define | CURRENT_LSB_MINIMUM (MAXIMUM_EXPECTED_CURRENT / ((float)(1<<15))) |
| #define | CURRENT_LSB (CURRENT_LSB_MINIMUM * 1.0) |
| #define | BUS_VOLTAGE_LSB 0.0016 |
| #define | SHUNT_VOLTAGE_81_92mv_LSB 0.0025 |
| #define | SHUNT_VOLTAGE_20_48mv_LSB 0.000625 |
| #define | POWER_LSB (CURRENT_LSB*32) |
| #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 INA236_Status | INA236_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 INA236 | INA236 |
| Class (struct) that stores variables for interacting with INA236. | |
Functions | |
| INA236_Status | INA236_init (INA236 *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 INA236 with the given config. | |
| INA236_Status | INA236_alert_init (INA236 *self, AlertOn alert_on, AlertPolarity alert_polarity, AlertLatch alert_latch, AlertConvReady alert_conv_ready, float alert_limit) |
| Initialize the alert functionality of INA236 with the given configurations. | |
| INA236_Status | __INA236_readTwoBytes (INA236 *self, uint8_t MemAddress) |
| Read two bytes (a 16bit register) from INA236 and stores in the INA236::_reg::raw_data. | |
| INA236_Status | __INA236_writeTwoBytes (INA236 *self, uint8_t MemAddress) |
| Write two bytes (a 16bit register) to INA236 from the INA236::_reg::raw_data. | |
| INA236_Status | INA236_setADCRange (INA236 *self, ADCRange adc_range) |
| Set the ADC full scale range of INA236. | |
| INA236_Status | INA236_setNumberOfADCSamples (INA236 *self, NumSamples numer_of_adc_samples) |
| Set the number of ADC samples to calculate the average. | |
| INA236_Status | INA236_setVBusConversionTime (INA236 *self, ConvTime vbus_conversion_time) |
| Set the VBus convertion period. | |
| INA236_Status | INA236_setVShuntConversionTime (INA236 *self, ConvTime vshunt_conversion_time) |
| Set the VShunt convertion period. | |
| INA236_Status | INA236_setMode (INA236 *self, Mode mode) |
| Set the operating mode. | |
| ADCRange | INA236_getADCRange (INA236 *self) |
| Get the ADC full scale range of INA236. | |
| NumSamples | INA236_getNumberOfADCSamples (INA236 *self) |
| Get the number of ADC samples to calculate the average. | |
| ConvTime | INA236_getVBusConversionTime (INA236 *self) |
| Get the VBus convertion period. | |
| ConvTime | INA236_getVShuntConversionTime (INA236 *self) |
| Get the VShunt convertion period. | |
| Mode | INA236_getMode (INA236 *self) |
| Set the operating mode. | |
| void | INA236_SoftResetAll (INA236 *self) |
| Send a reset command to all of the INA236s on the bus. | |
| uint16_t | INA236_getManID (INA236 *self) |
| Get the manufacturer ID. | |
| uint16_t | INA236_getDevID (INA236 *self) |
| Get the device ID. | |
| void | INA236_readAll (INA236 *self) |
| Read all of the measured values: Shunt voltage, bus voltage, power, and current. Then store the values to the INA236 object (struct) variables. Then you can read variables: INA236::ShuntVoltage, INA236::BusVoltage, INA236::Power, and INA236::Current. | |
| float | INA236_getCurrent (INA236 *self) |
| Read the current from INA236. | |
| float | INA236_getBusVoltage (INA236 *self) |
| Read the bus voltage from INA236. | |
| float | INA236_getShuntVoltage (INA236 *self) |
| Read the shunt voltage from INA236. | |
| float | INA236_getPower (INA236 *self) |
| Read the power from INA236. | |
| uint8_t | INA236_isDataReady (INA236 *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 INA236_resetAlert() function. | |
| AlertSource | INA236_getAlertSource (INA236 *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 INA236_resetAlert() function. | |
| ErrorType | INA236_getErrors (INA236 *self) |
| Get the error flags of INA236. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA236_resetAlert() function. | |
| INA236_Status | INA236_resetAlert (INA236 *self) |
| Reset the alert pin. This function is useful when set the alert pin to latch mode. | |
| #define ALERT_LIMIT_REGISTER 0x07 |
| #define BUS_VOLTAGE_LSB 0.0016 |
| #define BUS_VOLTAGE_REGISTER 0x02 |
| #define CALIBRATION_REGISTER 0x05 |
| #define CONFIGURATION_REGISTER 0x00 |
| #define CURRENT_LSB (CURRENT_LSB_MINIMUM * 1.0) |
| #define CURRENT_LSB_MINIMUM (MAXIMUM_EXPECTED_CURRENT / ((float)(1<<15))) |
| #define CURRENT_REGISTER 0x04 |
| #define DEVICEID_REGISTER 0x3F |
| #define MANUFACTURERID_REGISTER 0x3E |
| #define MASK_ENABLE_REGISTER 0x06 |
| #define MAXIMUM_EXPECTED_CURRENT 5.0 |
| #define POWER_LSB (CURRENT_LSB*32) |
| #define POWER_REGISTER 0x03 |
| #define SHUNT_VOLTAGE_20_48mv_LSB 0.000625 |
| #define SHUNT_VOLTAGE_81_92mv_LSB 0.0025 |
| #define SHUNT_VOLTAGE_REGISTER 0x01 |
| typedef enum AlertConvReady AlertConvReady |
| typedef enum AlertLatch AlertLatch |
| typedef enum AlertPolarity AlertPolarity |
| typedef enum AlertSource AlertSource |
| typedef struct INA236 INA236 |
Class (struct) that stores variables for interacting with INA236.
| typedef enum INA236_Status INA236_Status |
| typedef enum NumSamples NumSamples |
| enum ADCRange |
| enum AlertConvReady |
| enum AlertLatch |
| enum AlertOn |
| enum AlertPolarity |
| enum AlertSource |
| enum ConvTime |
| enum ErrorType |
| enum INA236_Status |
| enum Mode |
| enum NumSamples |
| INA236_Status __INA236_readTwoBytes | ( | INA236 * | self, |
| uint8_t | MemAddress ) |
Read two bytes (a 16bit register) from INA236 and stores in the INA236::_reg::raw_data.
| self | A pointer to the INA236 object (struct) |
| MemAddress | Address of the register |
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status __INA236_writeTwoBytes | ( | INA236 * | self, |
| uint8_t | MemAddress ) |
Write two bytes (a 16bit register) to INA236 from the INA236::_reg::raw_data.
| self | A pointer to the INA236 object (struct) |
| MemAddress | Address of the register |
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status INA236_alert_init | ( | INA236 * | self, |
| AlertOn | alert_on, | ||
| AlertPolarity | alert_polarity, | ||
| AlertLatch | alert_latch, | ||
| AlertConvReady | alert_conv_ready, | ||
| float | alert_limit ) |
Initialize the alert functionality of INA236 with the given configurations.
| self | A pointer to the INA236 object (struct) |
| alert_on | determines the event that you want to assert the alert:
|
| alert_polarity | The alert polarity determines the polarity of alert assertion:
|
| alert_latch | Determine the alert pin behaviour. If the latch is enabled, you have to reset the pin by calling the INA236_resetAlert() function.
|
| alert_conv_ready | Shows 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 INA236_getAlertSource() function.
|
| alert_limit | This 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
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
Get the ADC full scale range of INA236.
| self | A pointer to the INA236 object (struct) |
| RANGE_81_92mV | for 81.92 mV |
| RANGE_20_48mV | for 20.48 mV |
| AlertSource INA236_getAlertSource | ( | INA236 * | 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 INA236_resetAlert() function.
| self | A pointer to the INA236 object (struct) |
| ALERT_DATA_READY | The alert source is convertion ready |
| ALERT_LIMIT_REACHED | The alert source is limit reach |
| float INA236_getBusVoltage | ( | INA236 * | self | ) |
| float INA236_getCurrent | ( | INA236 * | self | ) |
| uint16_t INA236_getDevID | ( | INA236 * | self | ) |
Get the error flags of INA236. NOTE: This function will reset the alert pin if it was in the latch mode. Exactly like calling the INA236_resetAlert() function.
| self | A pointer to the INA236 object (struct) |
| ERROR_NONE | No error |
| ERROR_MEMORY | Memory error (CRC or ECC) |
| ERROR_OVF | Math overflow error |
| ERROR_BOTH_MEMORY_OVF | Both memory error (CRC or ECC) and math overflow error |
| uint16_t INA236_getManID | ( | INA236 * | self | ) |
Get the manufacturer ID.
| self | A pointer to the INA236 object (struct) |
Set the operating mode.
| self | A pointer to the INA236 object (struct) |
| MODE_SHUTDOWN | shutdown mode |
| MODE_SINGLESHOT_SUNT | only measure the shunt voltage once |
| MODE_SINGLESHOT_BUS | only measure the bus voltage once |
| MODE_SINGLESHOT_BOTH_SHUNT_BUS | measure the bus and shunt voltage once |
| MODE_CONTINUOUS_SHUNT | only measure the shunt voltage continuously |
| MODE_CONTINUOUS_BUS | only measure the bus voltage continuously |
| MODE_CONTINUOUS_BOTH_SHUNT_BUS | measure the bus and shunt voltage continuously |
| NumSamples INA236_getNumberOfADCSamples | ( | INA236 * | self | ) |
Get the number of ADC samples to calculate the average.
| self | A pointer to the INA236 object (struct) |
| float INA236_getPower | ( | INA236 * | self | ) |
| float INA236_getShuntVoltage | ( | INA236 * | self | ) |
Get the VBus convertion period.
| self | A pointer to the INA236 object (struct) |
Get the VShunt convertion period.
| self | A pointer to the INA236 object (struct) |
| INA236_Status INA236_init | ( | INA236 * | 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 INA236 with the given config.
| self | A pointer to the INA236 object (struct) |
| I2C_ADDR | The I2C address of the INA236. It depends on the state of A0 pin of the chip. |
| hi2c | A pointer to the I2C handler that is connected to INA236 |
| ShuntResistor | The resistance of your shunt resistor (in mOhm) connected to IN+ and IN- of the INA236 |
| adc_range | The full scale range of ADC. It can be one of these values:
|
| numer_of_adc_samples | Numer of ADC samples to calculate the average. The higher ADC samples leads to lower noises and higher latency. |
| vbus_conversion_time | The conversion time of VBus measurment. |
| vshunt_conversion_time | The conversion time of VShunt measurment. |
| mode | Operating mode:
|
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| uint8_t INA236_isDataReady | ( | INA236 * | 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 INA236_resetAlert() function.
| self | A pointer to the INA236 object (struct) |
| True | |
| False |
| void INA236_readAll | ( | INA236 * | self | ) |
Read all of the measured values: Shunt voltage, bus voltage, power, and current. Then store the values to the INA236 object (struct) variables. Then you can read variables: INA236::ShuntVoltage, INA236::BusVoltage, INA236::Power, and INA236::Current.
| self | A pointer to the INA236 object (struct) |
| INA236_Status INA236_resetAlert | ( | INA236 * | self | ) |
Reset the alert pin. This function is useful when set the alert pin to latch mode.
| self | A pointer to the INA236 object (struct) |
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status INA236_setADCRange | ( | INA236 * | self, |
| ADCRange | adc_range ) |
Set the ADC full scale range of INA236.
| self | A pointer to the INA236 object (struct) |
| adc_range | The full scale range of ADC. It can be one of these values:
|
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status INA236_setMode | ( | INA236 * | self, |
| Mode | mode ) |
Set the operating mode.
| self | A pointer to the INA236 object (struct) |
| mode | Operating mode:
|
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status INA236_setNumberOfADCSamples | ( | INA236 * | self, |
| NumSamples | numer_of_adc_samples ) |
Set the number of ADC samples to calculate the average.
| self | A pointer to the INA236 object (struct) |
| numer_of_adc_samples | Numer of ADC samples to calculate the average. The higher ADC samples leads to lower noises and higher latency. |
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status INA236_setVBusConversionTime | ( | INA236 * | self, |
| ConvTime | vbus_conversion_time ) |
Set the VBus convertion period.
| self | A pointer to the INA236 object (struct) |
| vbus_conversion_time | The conversion time of VBus measurment. |
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |
| INA236_Status INA236_setVShuntConversionTime | ( | INA236 * | self, |
| ConvTime | vshunt_conversion_time ) |
Set the VShunt convertion period.
| self | A pointer to the INA236 object (struct) |
| vshunt_conversion_time | The conversion time of VShunt measurment. |
| INA236_OK | in case of success |
| INA236_TimeOut | in case of failure |