INA226是一款分流/功率監(jiān)視器,具有IPCTM或SMBUS兼容接口。該器件監(jiān)視分流壓降和總線電源電壓。可編程校準(zhǔn)值、轉(zhuǎn)換時間和取平均值功能與內(nèi)部乘法器相結(jié)合,可實現(xiàn)電流值(單位為安培)和功率值(單位為瓦)的直接讀取。
INA226可在OV至36V的共模總線電壓范圍內(nèi)感測電流,與電源電壓無關(guān)。該器件由一個2.7V至5.5V的單電源供電,電源電流典型值為330uA。該器件的額定工作溫度范圍為-40°C至125°C,PC兼容接口上具有多達16個可編程地址。
C文件
#define _LED_C_
#include "led.h"
#include "delay.h"
#include "system.h"
#define IIC_PORT GPIOA //IIC IO端口
#define IIC_RCC RCC_APB2Periph_GPIOA //端口時鐘
#define IIC_SDA GPIO_Pin_5 //SDA引腳
#define IIC_SDA_H GPIO_SetBits(IIC_PORT,IIC_SDA) //拉高SDA
#define IIC_SDA_L GPIO_ResetBits(IIC_PORT,IIC_SDA) //拉低SDA
#define IIC_SDA_READ() GPIO_ReadInputDataBit(GPIOA,IIC_SDA) //讀取SDA
#define IIC_SCL GPIO_Pin_4 //SCL引腳
#define IIC_SCL_H GPIO_SetBits(IIC_PORT,IIC_SCL) //拉高SCL
#define IIC_SCL_L GPIO_ResetBits(IIC_PORT,IIC_SCL) //拉低SCL
char time=5;
void INA_IIC_Delay()
{unsigned char x;
for(x=1;x>0;x--)
{
__NOP();__NOP();__NOP();__NOP();__NOP();
}
}
void INAInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = IIC_SDA | IIC_SCL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
IIC_SDA_H;
IIC_SCL_H;
}
void INA_IIC_SDA_OUT(void) //SDA配置為輸出
{
GPIO_InitTypeDef GPIO_INIT;
GPIO_INIT.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_INIT.GPIO_Pin=IIC_SDA ;
GPIO_INIT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(IIC_PORT,&GPIO_INIT);
}
void INA_IIC_SDA_IN(void) //SDA配置為輸入
{
GPIO_InitTypeDef GPIO_INIT;
GPIO_INIT.GPIO_Mode=GPIO_Mode_IPU;
GPIO_INIT.GPIO_Pin=IIC_SDA ;
GPIO_INIT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(IIC_PORT,&GPIO_INIT);
}
void INA_IIC_Start(void) //開始信號
{
IIC_SDA_H;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SDA_L;
INA_IIC_Delay();
IIC_SDA_L;
}
void INA_IIC_Stop(void ) //結(jié)束信號
{
IIC_SDA_L;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SDA_H;
INA_IIC_Delay();
IIC_SCL_L;
IIC_SCL_H;
IIC_SDA_H;
}
bool INA_IIC_ACK_Read(void) //讀取應(yīng)答信號
{
bool ack;
IIC_SDA_H;
INA_IIC_SDA_IN();
IIC_SCL_H;
INA_IIC_Delay();
if(IIC_SDA_READ() == SET) ack=false;
else ack=true;
IIC_SCL_L;
INA_IIC_SDA_OUT();
return ack;
}
void INA_IIC_ACK_Send(bool ack) //發(fā)送應(yīng)答信號
{
IIC_SCL_L;
if(ack == true)
IIC_SDA_L;
else IIC_SDA_H;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SCL_L;
}
void INA_IIC_Send_Byte(unsigned char byte) //IIC發(fā)送一位數(shù)據(jù)
{
unsigned char i;
IIC_SCL_L;
for(i=0;i8;i++)
{
if(byte & 0x80 ) IIC_SDA_H;
else IIC_SDA_L;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SCL_L;
INA_IIC_Delay();
byte<=1;
}
INA_IIC_ACK_Read();
}
unsigned char INA_IIC_Read_Byte(void) //IIC讀取一位數(shù)據(jù)
{
unsigned char i,byte=0;
INA_IIC_SDA_IN();
for(i=0;i8;i++)
{
IIC_SCL_H;
byte<=1;
if(IIC_SDA_READ() == SET) byte |= 0x01;
else byte &= 0xFE;
IIC_SCL_L;
INA_IIC_Delay();
}
INA_IIC_SDA_OUT();
return byte;
}
void INA226_SetRegPointer(u8 addr,u8 reg)
{
INA_IIC_Start();
INA_IIC_Send_Byte(addr);
INA_IIC_Send_Byte(reg);
INA_IIC_Stop();
}
void INA226_SendData(u8 addr,u8 reg,u16 data) //寫寄存器 測試成功
{
unsigned char data_temp[2];
data_temp[0]=(unsigned char )(data?>>8);
data_temp[1]=(unsigned char )(data & 0xFF);
INA_IIC_Start();
INA_IIC_Send_Byte(addr); //發(fā)送INA219地址
INA_IIC_Send_Byte(reg); //發(fā)送寄存器地址
INA_IIC_Send_Byte(data_temp[0]); //發(fā)送高8位數(shù)據(jù)
data++;
INA_IIC_Send_Byte(data_temp[1]) ; //發(fā)送低8位數(shù)據(jù)
INA_IIC_Stop();
}
void INA226_Init(void)
{
INAInit();
DELAY_Ms(10);
INA226_SendData(INA226_ADDR1,CFG_REG,0x484f);//設(shè)置轉(zhuǎn)換時間204us,求平均值次數(shù)128,采樣時間204*128,設(shè)置模式為分流模式和總線連續(xù)模式
INA226_SendData(INA226_ADDR1,CAL_REG,0x0012);//設(shè)置分流電壓轉(zhuǎn)轉(zhuǎn)電流轉(zhuǎn)換參數(shù)
}
u16 INA226_ReadData(u8 addr)
{
u16 temp=0;
INA_IIC_Start();
INA_IIC_Send_Byte(addr+1);
temp = INA_IIC_Read_Byte();
temp<=8;
INA_IIC_ACK_Send(1);
temp |= INA_IIC_Read_Byte();
INA_IIC_ACK_Send(0);
INA_IIC_Stop();
return temp;
}
/*
u8 INA226_AlertAddr()
{
u8 temp;
IIC_Start();
IIC_Send_Byte(INA226_GETALADDR);
IIC_Wait_Ack();
temp = IIC_Read_Byte(1);
IIC_Stop();
return temp;
}
*/
//1mA/bit
u16 INA226_GetShunt_Current(u8 addr)
{
u16 temp=0;
INA226_SetRegPointer(addr,CUR_REG);
temp = INA226_ReadData(addr);
if(temp&0x8000) temp = ~(temp - 1);
return temp;
}
//?? id
u16 INA226_Get_ID(u8 addr)
{
u32 temp=0;
INA226_SetRegPointer(addr,INA226_GET_ADDR);
temp = INA226_ReadData(addr);
return (u16)temp;
}
//?????
u16 INA226_GET_CAL_REG(u8 addr)
{
u32 temp=0;
INA226_SetRegPointer(addr,CAL_REG);
temp = INA226_ReadData(addr);
return (u16)temp;
}
//1.25mV/bit
u16 INA226_GetVoltage(u8 addr)
{
u32 temp=0;
INA226_SetRegPointer(addr,BV_REG);
temp = INA226_ReadData(addr);
return (u16)temp;
}
//1.25mV/bit
u16 INA226_GeTCCC(u8 addr)
{
u32 temp=0;
INA226_SetRegPointer(addr,BV_REG);
temp = INA226_ReadData(addr);
return (u16)temp;
}
//2.5uV/bit,??????????,??????2.2?
u16 INA226_GetShuntVoltage(u8 addr)
{
int16_t temp=0;
INA226_SetRegPointer(addr,SV_REG);
temp = INA226_ReadData(addr);
if(temp&0x8000) temp = ~(temp - 1);
return (u16)temp;
}
u16 INA226_Get_Power(u8 addr)
{
int16_t temp=0;
INA226_SetRegPointer(addr,PWR_REG);
temp = INA226_ReadData(addr);
return (u16)temp;
}
void GetVoltage(float *Voltage)//mV
{
Voltage[0] = INA226_GetVoltage(INA226_ADDR1)*1.25f;
}
void Get_Shunt_voltage(float *Voltage)//uV
{
Voltage[0] = (INA226_GetShuntVoltage(INA226_ADDR1)*2.2);//?????????2.5
}
void Get_Shunt_Current(float *Current)//mA
{
Current[0] = (INA226_GetShunt_Current(INA226_ADDR1)* 2.5f);
}
void GetPower()//W
{
GetVoltage(&INA226_data.voltageVal); //mV
Get_Shunt_voltage(&INA226_data.Shunt_voltage); //uV
Get_Shunt_Current(&INA226_data.Shunt_Current); //mA
INA226_data.powerVal=INA226_data.voltageVal*0.001f * INA226_data.Shunt_Current*0.001f;
}
H文件
#ifndef __LED_H
#define __LED_H
// Files includes
#include
#include "mm32_device.h"
#include "hal_conf.h"
#define CFG_REG 0x00 //
#define SV_REG 0x01 //分流電壓,分流電阻為0.1歐姆
#define BV_REG 0x02 //總線電壓
#define PWR_REG 0x03 //電源功率
#define CUR_REG 0x04 //電流
#define CAL_REG 0x05 //校準(zhǔn),設(shè)定滿量程范圍以及電流功率測數(shù)
#define ONFF_REG 0x06 //屏蔽 使能
#define AL_REG 0x07 //警報功能的閾值
#define INA226_GET_ADDR 0XFF//唯一芯片識別號
#define INA226_ADDR1 0x80 //芯片地址
#define INA226_ADDR2 0x88 //芯片地址
#define CAL 456 //0.00512/(Current_LSB*R_SHUNT) = 470 //電流偏大改小
typedef struct
{
float voltageVal; //mV
float Shunt_voltage; //uV
float Shunt_Current; //mA
float powerVal;
}INA226;
void INA226_Init(void);
void INA226_SetRegPointer(u8 addr,u8 reg);
void INA226_SendData(u8 addr,u8 reg,u16 data);
u16 INA226_ReadData(u8 addr);
u16 INA226_Get_ID(u8 addr); //獲取ID
u16 INA226_GetVoltage( u8 addr); //獲取總線電壓
u16 INA226_GetShunt_Current(u8 addr); //獲取分流電壓
u16 INA226_GetShuntVoltage(u8 addr); //分流電壓
u16 INA226_Get_Power(u8 addr); //獲取功率
u16 INA226_GET_CAL_REG(u8 addr);
void GetVoltage(float *Voltage);
void Get_Shunt_voltage(float *Current);
void Get_Shunt_Current(float *Current);
//void Get_Power(float *Current);
void GetPower();//W
extern INA226 INA226_data;
#define Key4 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) //RST
#define Key3 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) //DOWN
#define Key2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) //UP
#define Key1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) //MODE
#define IN3 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define IN4 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)
#define IN5 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5)
#define IN6 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
#define IN7 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_9)
#define KEY8 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_10)
#define KEY9 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)
#define KEY10 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)
#define KEY11 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)
#define KEY12 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_11)
#endif
審核編輯:湯梓紅
-
接口
+關(guān)注
關(guān)注
33文章
8596瀏覽量
151145 -
監(jiān)視器
+關(guān)注
關(guān)注
1文章
780瀏覽量
33139 -
驅(qū)動程序
+關(guān)注
關(guān)注
19文章
836瀏覽量
48038 -
乘法器
+關(guān)注
關(guān)注
8文章
205瀏覽量
37057
發(fā)布評論請先 登錄
相關(guān)推薦
評論