This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

【求助】基于C51的INA226 IIC数据通信中遇到的问题

Other Parts Discussed in Thread: INA226

最近想用INA226做个电流电压测量的工具监视铅酸电池充放电状态,遇到点问题,情况是这样的
在主函数体中只是先运行

INA226_WriteReg(INA226_CONFIG,0x4127);

然后

tmp=INA226_ReadReg(INA226_BUS);

使用串口输出tmp变量时候一直显示输出为零,搞不清是什么情况,恳请各位老师帮帮忙指导一下,不胜感激

以下是INA226.c 文件代码:
#define INA226_W	0x80
#define INA226_R	0x81

#define INA226_CONFIG	0x00
#define INA226_SHUNT	0x01
#define INA226_BUS		0x02
#define INA226_POWEER	0x03
#define INA226_CURRENT	0x04
#define INA226_CAL		0x05
#define INA226_MAEN		0x06
#define INA226_AIERT	0x07
#define INA226_DIEID	0xFF

sbit SCL=P0^0;
sbit SDA=P0^1;
void Delay1us();
void INA226_Start();
void INA226_Stop();
void INA226_GetACK();
void INA226_WriteByte(unsigned char tmp);
void INA226_SendACK();
unsigned char INA226_ReadByte();
void INA226_SetPoint(unsigned char addr);
void INA226_WriteReg(unsigned char addr,unsigned int dat);
unsigned int INA226_ReadReg(unsigned char addr);



void Delay1us()		//@33.1776MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	_nop_();
	i = 5;
	while (--i);
}

void INA226_Start(){
	SDA=1;
	Delay1us();
	SCL=1;
	Delay1us();
	SDA=0;
	Delay1us();
}
void INA226_Stop(){
	Delay1us();Delay1us();
	SCL=1;
	Delay1us();
	SDA=0;
	Delay1us();
	SDA=1;
	Delay1us();
} 
void INA226_GetACK(){
	Delay1us();
	SCL=0;
	SDA=1;
	Delay1us();Delay1us();
	SCL=1;
	while(SDA==1);
	Delay1us();
	SCL=0;
}
void INA226_WriteByte(unsigned char tmp){
	unsigned char i;
	for(i=0;i<8;i++){
		Delay1us();
		SCL=0;
		SDA=(tmp<<i)&0x80;
		Delay1us();Delay1us();
		SCL=1;	
	}
	INA226_GetACK();	
}
void INA226_SendACK(){
	Delay1us();
	SCL=0;
	SDA=0;
	Delay1us();Delay1us();
	SCL=1;
	Delay1us();
	SCL=0;
}
unsigned char INA226_ReadByte(){
	unsigned char i,tmp; 
	SDA=1;
	for(i=0;i<8;i++){
		Delay1us();
		SCL=0;
		tmp<<=1;
		Delay1us();Delay1us();
		SCL=1;
		if(SDA)tmp|=0x01;
	}
	INA226_SendACK();	
}

void INA226_SetPoint(unsigned char addr){
	INA226_Start();
	INA226_WriteByte(INA226_W);	
	INA226_WriteByte(addr);		
}
void INA226_WriteReg(unsigned char addr,unsigned int dat){
	INA226_SetPoint(addr);
	INA226_WriteByte(dat>>8);
	INA226_WriteByte(dat&0x00ff);
	INA226_Stop();		
}
unsigned int INA226_ReadReg(unsigned char addr){
	unsigned int tmp;
	INA226_SetPoint(addr);
	INA226_Stop();
	INA226_Start();
	INA226_WriteByte(INA226_R);
	tmp=INA226_ReadByte();
	tmp<<=8;
	tmp|=INA226_ReadByte();
	INA226_Stop();
	return tmp;
}


  • 好好地看了一下你的程序, 从时序上看, 看不出问题来.

    能否检查一下硬件, 或者用示波器看看通信波形有没有问题

  • 可是我在发送字节后都会等待器件的ACK返回,在函数里

    void INA226_GetACK(){
        Delay1us();
        SCL=0;
        SDA=1;
        Delay1us();Delay1us();
        SCL=1;
        while(SDA==1);
        Delay1us();
        SCL=0;
    }

    如果没有ACK返回会进入死循环的啊,之前也有因为时序问题进入死循环是没有输出的,但是问题来了,器件有返回,而且输出全零,说明通信应该没问题,读配置寄存器的值输出也是全零,就不知道为什么了

  • 你得先看一下从器件是否有返回数据,使用示波器看一下,看看有没有信号传到单片机SDA线上,这个确定了再走下一步。

    根据你的GetACK看是有 ACK信号的,你再示波器看一下ACK后是否真的全是0