INA226驱动程序

描述

INA226是一款分流/功率监视器,具有IPCTM或SMBUS兼容接口。该器件监视分流压降和总线电源电压。可编程校准值、转换时间和取平均值功能与内部乘法器相结合,可实现电流值(单位为安培)和功率值(单位为瓦)的直接读取。

接口

INA226可在OV至36V的共模总线电压范围内感测电流,与电源电压无关。该器件由一个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 )	//结束信号
{
	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)	//读取应答信号
{
	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)	//发送应答信号
{
	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发送一位数据
{
	unsigned char i;
	IIC_SCL_L;
	for(i=0;i< 8;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读取一位数据
{
	unsigned char i,byte=0;
	INA_IIC_SDA_IN();
	for(i=0;i< 8;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);	//发送INA219地址
	INA_IIC_Send_Byte(reg);							//发送寄存器地址
	INA_IIC_Send_Byte(data_temp[0]);						//发送高8位数据
	data++;
	INA_IIC_Send_Byte(data_temp[1])	;					//发送低8位数据
	INA_IIC_Stop();
}

 
void INA226_Init(void)
{	
	INAInit();
	DELAY_Ms(10);
	INA226_SendData(INA226_ADDR1,CFG_REG,0x484f);//设置转换时间204us,求平均值次数128,采样时间204*128,设置模式为分流模式和总线连续模式
	INA226_SendData(INA226_ADDR1,CAL_REG,0x0012);//设置分流电压转转电流转换参数
}
 


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 < string.h >

#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		//校准,设定满量程范围以及电流功率测数
#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



  审核编辑:汤梓红
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分