資源簡介
使用INA226芯片測量電流和總線電壓,可測量uA級別電流,可自動計算功率
代碼片段和文件信息
#include?“ina226.h“
#include?“iic.h“
#include?“usart.h“
/////////////
//2毫歐方案//???
////////////
/////////////
//1毫歐方案//
////////////
//Configuration?Register?00h??===??0x43FF?===?D15-RST;D12:D14-000?;?D9:D11(AVG)-001?;?D6:D8(VBCT)-111?;?D3:D5(VSHCT)-111?;?D0-D2(MODE)-111
//Shunt?Voltage?Register?01h?(Read-Only)2.5uV/bit??===??D15:正負標志?;?D0:D14-DATA?;?負數:二進制補碼?+1
//Bus?Voltage?Register?02h?(Read-Only)1.25mV/bit??===??d15:0?;?D0:D14?-?DATA?;?不支持負電壓檢測
//Power?Register?03h?(Read-Only)?---?25*Current_LSB/bit?===?D0:D15?-?DATA
//Current?Register?04h?(Read-Only)1mA/bit?===?d15:0?;?D0:D14?-?DATA?
//Calibration?Register?05h?(Read/Write)?===?0xA00?===?d15:0?;?D0:D14?-?DATA?
//Mask/Enable?06h?(Read/Write)?===?0x0000H?===
//Bit?15?-?SOL:?Shunt?Voltage?Over-Voltage??
//Bit?14?-?SUL:?Shunt?Voltage?Under-Voltage?
//Bit?13?-?BOL:?Bus?Voltage?Over-Voltage?
//Bit?12?-?BUL:?Bus?Voltage?Under-Voltage?
//Bit?11?-?POL:?Over-Limit?Power
//Bit?10?-?CNVR:?Conversion?Read?
//Bit?4?-?AFF:?Alert?Function?Flag?
//Bit?3?-?CVRF:?Conversion?Ready?Flag?
//Bit?2?-?OVF:?Math?Overflow?Flag?
//Bit?1?-?APOL:?Alert?Polarity?bit;?sets?the?Alert?pin?polarity.
//Bit?0?-?LEN:?Alert?Latch?Enable;?configures?the?latching?feature?of?the?Alert?pin?and?Flag?bits.
//Alert?Limit?07h?(Read/Write)?===?0x0000H?===?D0:D15?-?DATA?
//INA226?ADDR?:?1000000?
/////////////
//50毫歐方案//
////////////
// INA226_SetRegPointer(INA226_ADDR1CFG_REG);//為了測試
// my_read_test?=?INA226_ReadData(INA226_ADDR1);//為了測試 默認值為4712
//配置寄存器 ??0x43FF?采樣4次?0x4dff?采樣512次???采樣時間1.1ms?1次??0x4147
//INA226_SendData(INA226_ADDR1CFG_REG0x4147);//
u16?my_read_test?=?0;
void?INA226_Init(void)
{
//INA226_SendData(INA226_ADDR1CFG_REG0x4125);/*分流電壓,持續模式,1.1MS轉化一次,?采樣?1?次*/
//INA226_SendData(INA226_ADDR1CFG_REG0x4325);/*分流電壓,持續模式,1.1MS轉化一次,?采樣?4?次*/
INA226_SendData(INA226_ADDR1CFG_REG0x4f45);//0x43FF?采樣4次?0x4dff?采樣512次?
INA226_SendData(INA226_ADDR1CAL_REG2050);//配置校準寄存器1024
}
void?INA226_SetRegPointer(u8?addru8?reg)
{
I2C_Start();
I2C_WRData(addr);
I2C_WaitACK();
I2C_WRData(reg);
I2C_WaitACK();
I2C_Stop();
}
void?INA226_SendData(u8?addru8?regu16?data)
{
u8?temp=0;
I2C_Start();
I2C_WRData(addr);
I2C_WaitACK();
I2C_WRData(reg);
I2C_WaitACK();
temp?=?(u8)(data>>8);
I2C_WRData(temp);
I2C_WaitACK();
temp?=?(u8)(data&0x00FF);
I2C_WRData(temp);
I2C_WaitACK();
I2C_Stop();
}
u16?INA226_ReadData(u8?addr)
{
u16?temp=0;
I2C_Start();
I2C_WRData(addr+1);
I2C_WaitACK();
temp?=?I2C_RDData(1);
temp<<=8;
temp?|=?I2C_RDData(0);
I2C_Stop();
return?temp;
}
u8?INA226_AlertAddr(void)
{
u8?temp;
I2C_Start();
I2C_WRData(INA226_GETALADDR);
I2C_WaitACK();
temp?=?I2C_RDData(1);
I2C_Stop();
return?temp;
}
//1mA/bit
u16?INA226_GetCurrent(u8?addr)
{
u16?temp=0;
short?tmps;
INA226_SetRegPointer(addrSV_REG);//分流電壓
temp?=?INA226_ReadData(addr);
if(t
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3912??2018-06-08?15:25??INA226\ina226.c
?????文件???????1008??2018-06-08?15:25??INA226\ina226.h
?????目錄??????????0??2018-06-08?15:25??INA226
-----------?---------??----------?-----??----
?????????????????4920????????????????????3
評論
共有 條評論