#ifndef INA226_CFG_H_
#define INA226_CFG_H_
//
#ifdef __cplusplus
extern "C" {
#endif
//
#include"complier_lib.h"
#include "i2c_task.h"
#include "gpio_task.h"
//
//===配置寄存区--[0x4127]
#define CONFIGURATION_REG 0x00
//===采样电阻上的压降---[0x00000],满量程是81.92mV,最小单位是2.5uV
#define SHUNT_VOLTAGE_REG 0x01
//===总线电压---[0x00000],满量程是40.96V,最小单位是1.25mV
#define BUS_VOLTAGE_REG 0x02
//===功率寄存器---[0x00000]
#define POWER_REG 0x03
//===电流寄存器---[0x00000]
#define CURRENT_REG 0x04
//===校准寄存器---[0x00000]
#define CALIBRATION_REG 0x05
//===功能控制寄存器---[0x00000]
#define MASK_ENABLE_REG 0x06
//===设置的报警阈值---[0x00000]
#define ALERT_LIMIT_REG 0x07
//===制造商ID---[0x5449]
#define MANUFACTURER_ID_REG 0xFE
//===设备ID---[0x2260]
#define DIE_ID_REG 0xFF
//===使能过压
#define SHUNT_OVER_VOLTAGE_ENABLE (1<<15)
//===不使能过压
#define SHUNT_OVER_VOLTAGE_DISABLE ~(1<<15)
//===使能欠压
#define SHUNT_UNDER_VOLTAGE_ENABLE (1<<14)
//===不使能欠压
#define SHUNT_UNDER_VOLTAGE_DISABLE ~(1<<14)
//===使能过压
#define BUS_OVER_VOLTAGE_ENABLE (1<<13)
//===不使能过压
#define BUS_OVER_VOLTAGE_DISABLE ~(1<<13)
//===使能欠压
#define BUS_UNDER_VOLTAGE_ENABLE (1<<12)
//===不使能欠压
#define BUS_UNDER_VOLTAGE_DISABLE ~(1<<12)
//===使能功率阈值
#define POWER_OVER_LIMIT_ENABLE (1<<11)
//===不使能功率阈值
#define POWER_OVER_LIMIT_DISABLE (1<<11)
//===使能转换准备好标致
#define CONVERSION_READY_ENABLE (1<<10)
//===不使能转换准备好标致
#define CONVERSION_READY_DISABLE (1<<10)
//===报警端口低有效
#define ALERT_POLARITY_ACTIVE_LOW ~(1<<1)
//===报警端口搞有效
#define ALERT_POLARITY_ACTIVE_HIGH (1<<1)
//===报警端口锁存使能
#define ALERT_LATCH_ENABLE (1)
//===报警端口锁存不使能
#define ALERT_LATCH_DISABLE 0xFFFE
//===配置平均转换的次数
#define AVERAGING_MODE_MASK ~(7<<9)
#define AVERAGING_MODE_1 (0<<9)
#define AVERAGING_MODE_4 (1<<9)
#define AVERAGING_MODE_16 (2<<9)
#define AVERAGING_MODE_64 (3<<9)
#define AVERAGING_MODE_128 (4<<9)
#define AVERAGING_MODE_256 (5<<9)
#define AVERAGING_MODE_512 (6<<9)
#define AVERAGING_MODE_1024 (7<<9)
//===配置总线电压转换的时间
#define BUS_VOLTAGE_CONVERSIOM_TIME_MASK ~(7<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_140_US (0<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_204_US (1<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_332_US (2<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_588_US (3<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_1100_US (4<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_2116_US (5<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_4156_US (6<<6)
#define BUS_VOLTAGE_CONVERSIOM_TIME_8244_US (7<<6)
//===配置采样电压转换的时间
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_MASK ~(7<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_140_US (0<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_204_US (1<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_332_US (2<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_588_US (3<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_1100_US (4<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_2116_US (5<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_4156_US (6<<3)
#define SHUNT_VOLTAGE_CONVERSIOM_TIME_8244_US (7<<3)
//===配置操作模式
#define OPERATING_MODE_MASK ~(7<<0)
#define OPERATING_MODE_POWER_DOWN_1 (0<<0)
#define OPERATING_MODE_SHUNT_VOLTAGE_TRIG (1<<0)
#define OPERATING_MODE_BUS_VOLTAGE_TRIG (2<<0)
#define OPERATING_MODE_SHUNT_BUS_VOLTAGE_TRIG (3<<0)
#define OPERATING_MODE_POWER_DOWN_2 (4<<0)
#define OPERATING_MODE_SHUNT_VOLTAGE_CONT (5<<0)
#define OPERATING_MODE_BUS_VOLTAGE_CONT (6<<0)
#define OPERATING_MODE_SHUNT_BUS_VOLTAGE_CONT (7<<0)
//===总线电压量程每BIT对应的电压值,单位是毫伏
#define INA226_RANG_BUS_VOLTAGE_MV_BIT 1.25f
//===采样电阻上电压量程每BIT对应的电压值,单位是微伏
#define INA226_RANG_SHUNT_VOLTAGE_UV_BIT 2.5f
//===采样电阻的大小,单位是毫欧
#define INA226_SAMPLE_RES_MR 1
//===INA226的电流最大采集量程,这个和校准寄存器有关
#define INA226_RANG_CURRENT_MA_MAX 15000
//===INA226的电流量程每BIT对应电流值,单位是微安安
#define INA226_RANG_CURRENT_UA_BIT_X1 (UINT16_T)( INA226_RANG_CURRENT_MA_MAX*1000/(1<<15))
//===校准寄存器的值
#define INA226_CALIB_REG_DEFAULT_X1 (UINT16_T)( 5120*1000/(INA226_RANG_CURRENT_UA_BIT_X1*INA226_SAMPLE_RES_MR) )
//===
#define INA226_RANG_CURRENT_UA_BIT_X2 (UINT16_T)( INA226_RANG_CURRENT_UA_BIT_X1*2 )
//===
#define INA226_CALIB_REG_DEFAULT_X2 (UINT16_T)( INA226_CALIB_REG_DEFAULT_X1*2 )
//===结构体定义
typedef struct _INA226_HandlerType INA226_HandlerType;
//===指针结构体定义
typedef struct _INA226_HandlerType * pINA226_HandlerType;
//===AT24Cxx的数据结构体
struct _INA226_HandlerType
{
UINT16_T msgCFGReg; //---配置寄存器
UINT16_T msgMaskReg; //---功能配置寄存器
UINT16_T msgShuntReg; //---采样电压寄存器
UINT16_T msgBusVReg; //---总线电压寄存器
UINT16_T msgPowerReg; //---功率寄存器
UINT16_T msgCurrentReg; //---采样电流寄存器
UINT16_T msgCalibReg; //---校准寄存器
float msgBusmV; //---总线电压,单位毫伏
float msgShuntuV; //---采样电阻电压,单位微伏
float msgShuntuA; //---采样电阻电流,单位微安
float msgPowermW; //---负载的功耗,单位微瓦
GPIO_HandlerType msgAlert; //---报警端口的配置,INA226是开漏模式,需要外接上拉电阻
I2C_HandlerType msgI2C; //---使用的I2C
};
//===定义的任务函数
#define INA226_TASK_ONE pINA226Device0
#define INA226_TASK_TWO 0
#define INA226_TASK_THREE 0
//===外部调用
extern INA226_HandlerType g_INA226Device0 ;
extern pINA226_HandlerType pINA226Device0 ;
//===函数定义
UINT8_T INA226_I2C_Device0_Init(INA226_HandlerType* INA226x);
UINT8_T INA226_I2C_Device1_Init(INA226_HandlerType* INA226x);
UINT8_T INA226_I2C_Device2_Init(INA226_HandlerType* INA226x);
UINT8_T INA226_I2C_Init(INA226_HandlerType* INA226x, void(*pFuncDelayus)(UINT32_T delay), UINT8_T isHWI2C);
UINT8_T INA226_ConfigInit(INA226_HandlerType* INA226x);
UINT8_T INA226_GetDieID(INA226_HandlerType* INA226x);
UINT8_T INA226_GetManuID(INA226_HandlerType* INA226x);
UINT8_T INA226_GetBusVoltage(INA226_HandlerType* INA226x);
//
#ifdef __cplusplus
}
#endif
//
#endif /*INA226_CFG_H_ */
#include "ina226_cfg.h"
//===全局变量定义
INA226_HandlerType g_INA226Device0 = { 0 };
pINA226_HandlerType pINA226Device0 = &g_INA226Device0;
///
//函 数:
//功 能:初始化
//输入参数:isHWI2C---1 硬件,0 软件
//输出参数:
//说 明:
//
UINT8_T INA226_I2C_Init(INA226_HandlerType* INA226x, void(*pFuncDelayus)(UINT32_T delay), UINT8_T isHWI2C)
{
UINT8_T _return = OK_0;
//---指定设备的初始化
if ((INA226x != NULL) && (INA226x == INA226_TASK_ONE))
{
INA226_I2C_Device0_Init(INA226x);
}
else if ((INA226x != NULL) && (INA226x == INA226_TASK_TWO))
{
INA226_I2C_Device1_Init(INA226x);
}
else if ((INA226x != NULL) && (INA226x == INA226_TASK_THREE))
{
INA226_I2C_Device2_Init(INA226x);
}
else
{
return ERROR_1;
}
//---判断是硬件I2C还是软件I2C
if (isHWI2C)
{
//---初始化硬件I2C
_return = I2CTask_MHW_Init(&(INA226x->msgI2C));
//---设置为硬件模式
INA226x->msgI2C.msgModelIsHW = 1;
}
else
{
//---初始化软件模拟I2C
_return = I2CTask_MSW_Init(&(INA226x->msgI2C), pFuncDelayus);
//---设置为软件件模式
INA226x->msgI2C.msgModelIsHW = 0;
}
//---配置初始化
_return = INA226_ConfigInit(INA226x);
return _return;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_I2C_Device0_Init(INA226_HandlerType* INA226x)
{
INA226x->msgI2C.msgI2Cx = NULL;
INA226x->msgI2C.msgSCL.msgGPIOPort = GPIOB;
INA226x->msgI2C.msgSCL.msgGPIOBit = LL_GPIO_PIN_6;
INA226x->msgI2C.msgSDA.msgGPIOPort = GPIOB;
INA226x->msgI2C.msgSDA.msgGPIOBit = LL_GPIO_PIN_7;
INA226x->msgI2C.msgModelIsHW = 0;
INA226x->msgI2C.msgPluseWidth = 2;
INA226x->msgI2C.msgFuncDelayus = NULL;
INA226x->msgI2C.msgAddr = 0x80;
INA226x->msgI2C.msgClockSpeed = 0;
return OK_0;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_I2C_Device1_Init(INA226_HandlerType* INA226x)
{
return OK_0;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_I2C_Device2_Init(INA226_HandlerType* INA226x)
{
return OK_0;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_SWI2C_WriteReg(INA226_HandlerType* INA226x, UINT8_T addr, UINT16_T val)
{
UINT8_T _return = OK_0;
//---启动IIC并发送器件地址,写数据
_return = I2CTask_MSW_START(&(INA226x->msgI2C), 1);
if (_return != OK_0)
{
//---启动写数据失败
_return = ERROR_1;
goto GoToExit;
}
//---发送寄存器地址
I2CTask_MSW_SendByte(&(INA226x->msgI2C), addr);
//---读取ACK
_return = I2CTask_MSW_ReadACK(&(INA226x->msgI2C));
if (_return != OK_0)
{
//---发送寄存器地址错误
_return = ERROR_2;
goto GoToExit;
}
//---发送高位数据
I2CTask_MSW_SendByte(&(INA226x->msgI2C), (UINT8_T)(val>>16));
//---读取ACK
_return = I2CTask_MSW_ReadACK(&(INA226x->msgI2C));
if (_return != OK_0)
{
//---发送数据错误
_return = ERROR_3;
goto GoToExit;
}
//---发送低位数据
I2CTask_MSW_SendByte(&(INA226x->msgI2C), (UINT8_T)(val&0xFF));
//---读取ACK
_return = I2CTask_MSW_ReadACK(&(INA226x->msgI2C));
if (_return != OK_0)
{
//---发送数据错误
_return = ERROR_4;
goto GoToExit;
}
//---退出操作入口
GoToExit:
//---发送停止信号
I2CTask_MSW_STOP(&(INA226x->msgI2C));
return _return;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_HWI2C_WriteReg(INA226_HandlerType* INA226x, UINT8_T addr, UINT16_T val)
{
return ERROR_1;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_I2C_WriteReg(INA226_HandlerType* INA226x, UINT8_T addr, UINT16_T val)
{
if (INA226x->msgI2C.msgModelIsHW == 1)
{
return INA226_HWI2C_WriteReg(INA226x, addr, val);
}
else
{
return INA226_SWI2C_WriteReg(INA226x, addr, val);
}
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_SWI2C_ReadReg(INA226_HandlerType* INA226x, UINT8_T addr, UINT16_T *pVal)
{
UINT8_T _return = OK_0;
//---启动IIC并发送器件地址,写数据
_return = I2CTask_MSW_START(&(INA226x->msgI2C), 1);
if (_return != OK_0)
{
//---启动写数据失败
_return = ERROR_1;
goto GoToExit;
}
//---发送寄存器地址
I2CTask_MSW_SendByte(&(INA226x->msgI2C), addr);
//---读取ACK
_return = I2CTask_MSW_ReadACK(&(INA226x->msgI2C));
if (_return != OK_0)
{
//---发送数据失败
_return = ERROR_2;
goto GoToExit;
}
//---启动IIC并发送器件地址,读数据
_return = I2CTask_MSW_START(&(INA226x->msgI2C), 0);
if (_return != OK_0)
{
//---启动读数据失败
_return = ERROR_3;
goto GoToExit;
}
UINT16_T readTemp = 0;
//---读取高位数据
readTemp= I2CTask_MSW_ReadByte(&(INA226x->msgI2C));
//---发送应答信号
_return = I2CTask_MSW_SendACK(&(INA226x->msgI2C), 0);
readTemp <<= 8;
//---读取低位数据
readTemp |= I2CTask_MSW_ReadByte(&(INA226x->msgI2C));
//---发送不应答信号
_return = I2CTask_MSW_SendACK(&(INA226x->msgI2C), 1);
*pVal = readTemp;
GoToExit:
//---发送停止信号
I2CTask_MSW_STOP(&(INA226x->msgI2C));
return _return;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_HWI2C_ReadReg(INA226_HandlerType* INA226x, UINT8_T addr, UINT16_T* pVal)
{
return ERROR_1;
}
///
//函 数:
//功 能:
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_I2C_ReadReg(INA226_HandlerType* INA226x, UINT8_T addr, UINT16_T* pVal)
{
if (INA226x->msgI2C.msgModelIsHW == 1)
{
return INA226_HWI2C_ReadReg(INA226x, addr, pVal);
}
else
{
return INA226_SWI2C_ReadReg(INA226x, addr, pVal);
}
}
///
//函 数:
//功 能:设备ID
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetDieID(INA226_HandlerType* INA226x)
{
UINT16_T deviceID = 0;
UINT8_T _return = INA226_I2C_ReadReg(INA226x, DIE_ID_REG, &deviceID);
if (_return==OK_0)
{
if (deviceID != 0x2260)
{
_return = ERROR_1;
}
}
return _return;
}
///
//函 数:
//功 能:制造商ID
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetManuID(INA226_HandlerType* INA226x)
{
UINT16_T deviceID = 0;
UINT8_T _return = INA226_I2C_ReadReg(INA226x, MANUFACTURER_ID_REG, &deviceID);
if (_return == OK_0)
{
if (deviceID != 0x5449)
{
_return = ERROR_1;
}
}
return _return;
}
///
//函 数:
//功 能:读取配置寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetCFGReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, CONFIGURATION_REG, &(INA226x->msgCFGReg));
}
///
//函 数:
//功 能:设置配置寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_SetCFGReg(INA226_HandlerType* INA226x, UINT16_T val)
{
INA226x->msgCFGReg = val;
return INA226_I2C_WriteReg(INA226x, CONFIGURATION_REG, INA226x->msgCFGReg);
}
///
//函 数:
//功 能:复位设备
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_Reset(INA226_HandlerType* INA226x)
{
INA226x->msgCFGReg = 0x4000;
return INA226_SetCFGReg(INA226x,0xC000);
}
///
//函 数:
//功 能:读取校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetCalibReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, CALIBRATION_REG, &(INA226x->msgCalibReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_SetCalibReg(INA226_HandlerType* INA226x,UINT16_T val)
{
INA226x->msgCalibReg = val;
return INA226_I2C_WriteReg(INA226x, CALIBRATION_REG, INA226x->msgCalibReg);
}
///
//函 数:
//功 能:读取校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetMaskReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, MASK_ENABLE_REG, &(INA226x->msgMaskReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_SetMaskReg(INA226_HandlerType* INA226x, UINT16_T val)
{
INA226x->msgMaskReg=val;
return INA226_I2C_WriteReg(INA226x, MASK_ENABLE_REG, INA226x->msgMaskReg);
}
///
//函 数:
//功 能:清楚转换就绪标志位
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_ClearConversionReadyFlag(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, MASK_ENABLE_REG, &(INA226x->msgMaskReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetShuntReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, SHUNT_VOLTAGE_REG, &(INA226x->msgShuntReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetCurrentReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, CURRENT_REG, &(INA226x->msgCurrentReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetBusVReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, BUS_VOLTAGE_REG, &(INA226x->msgBusVReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetPowerReg(INA226_HandlerType* INA226x)
{
return INA226_I2C_ReadReg(INA226x, POWER_REG, &(INA226x->msgPowerReg));
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetBusVoltage(INA226_HandlerType* INA226x)
{
UINT8_T _return = INA226_GetBusVReg(INA226x);
INA226x->msgBusmV = 0.0f;
if (_return==OK_0)
{
INA226x->msgBusmV = INA226x->msgBusVReg;
INA226x->msgBusmV *= INA226_RANG_BUS_VOLTAGE_MV_BIT;
}
return _return;
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetShuntVoltage(INA226_HandlerType* INA226x)
{
UINT8_T _return = INA226_GetShuntReg(INA226x);
INA226x->msgShuntuV = 0.0f;
if (_return == OK_0)
{
if ((INA226x->msgShuntReg&0x8000)!=0)
{
INA226x->msgShuntuV =(-1.0)*(0x10000 - INA226x->msgShuntReg);
}
else
{
INA226x->msgShuntuV = INA226x->msgShuntReg;
}
INA226x->msgShuntuV *= INA226_RANG_SHUNT_VOLTAGE_UV_BIT;
}
return _return;
}
///
//函 数:
//功 能:设置校准寄存器的值
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetShuntCurrent(INA226_HandlerType* INA226x)
{
UINT8_T _return = INA226_GetCurrentReg(INA226x);
INA226x->msgShuntuA = 0.0f;
if (_return == OK_0)
{
if (INA226x->msgCalibReg == 0)
{
if ((INA226x->msgCurrentReg & 0x8000) != 0)
{
INA226x->msgShuntuA =(-1.0)*(0x10000 - INA226x->msgCurrentReg);
}
else
{
INA226x->msgShuntuA = INA226x->msgCurrentReg;
}
if (INA226x->msgCalibReg == INA226_CALIB_REG_DEFAULT_X1)
{
INA226x->msgShuntuA *= INA226_RANG_CURRENT_UA_BIT_X1;
}
else if (INA226x->msgCalibReg == INA226_CALIB_REG_DEFAULT_X2)
{
INA226x->msgShuntuA *= INA226_RANG_CURRENT_UA_BIT_X2;
}
else
{
_return = INA226_GetShuntVoltage(INA226x);
if (_return == OK_0)
{
if ((INA226x->msgShuntReg & 0x8000) != 0)
{
INA226x->msgShuntuA = (-1000.0) * (INA226x->msgShuntuV / INA226_SAMPLE_RES_MR);
}
else
{
INA226x->msgShuntuA = (1000.0) * (INA226x->msgShuntuV / INA226_SAMPLE_RES_MR);
}
}
}
}
else
{
_return = INA226_GetShuntVoltage(INA226x);
if (_return == OK_0)
{
if ((INA226x->msgShuntReg & 0x8000) != 0)
{
INA226x->msgShuntuA = (-1.0)*(0x10000 - INA226x->msgShuntReg);;
}
else
{
INA226x->msgShuntuA = INA226x->msgShuntReg;
}
INA226x->msgShuntuA = (1000.0) * (INA226x->msgShuntuA * INA226x->msgCalibReg / 2048);
}
}
}
return _return;
}
///
//函 数:
//功 能:获取负载的功耗
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_GetPower(INA226_HandlerType* INA226x)
{
UINT8_T _return = INA226_GetShuntVoltage(INA226x);
INA226x->msgPowermW = 0.0f;
if (_return == OK_0)
{
_return = INA226_GetShuntCurrent(INA226x);
if (_return==OK_0)
{
INA226x->msgPowermW = INA226x->msgBusmV * INA226x->msgShuntuA / 1000000.0;
}
}
return _return;
}
///
//函 数:
//功 能:配置初始化
//输入参数:
//输出参数:
//说 明:
//
UINT8_T INA226_ConfigInit(INA226_HandlerType* INA226x)
{
UINT8_T _return = OK_0;
INA226x->msgCFGReg = 0x4000;
//---平局64,采集时间204us
_return= INA226_SetCFGReg(INA226x, 0x4000| AVERAGING_MODE_64|
BUS_VOLTAGE_CONVERSIOM_TIME_204_US|
SHUNT_VOLTAGE_CONVERSIOM_TIME_204_US|
OPERATING_MODE_SHUNT_BUS_VOLTAGE_CONT
);
if (_return==OK_0)
{
//---设置校准寄存器
_return = INA226_SetCalibReg(INA226x, INA226_RANG_CURRENT_UA_BIT_X2);
}
if (_return==OK_0)
{
//---将报警端口设置为装换完成,标志低有效,高电平无效
_return = INA226_SetMaskReg(INA226x, CONVERSION_READY_ENABLE);
}
return _return;
}
|
|