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.

单片机无法读取INA226数据问题

Other Parts Discussed in Thread: INA226

最近采用INA226读取负载工作电压、电流、电压,使用单片机进行配置和读取数据。

以下是我写的程序,请教各位大神是什么原因。

void Delay6us() ;
void delay2us();
void sent_Byte(uchar); //INA226配置与读取子函数说明
void config_INA226(); //设置配置寄存器
void cal_INA226(); //设置校准寄存器
uint receive_Word(uint); //读取寄存器数值
uint read_Data(uchar,uint); //读取电流/电压/功率的值

//////////////////////INA226子函数/////////////////////////////////
//==============================================================================
//子程序:Delay6us
//==============================================================================
void Delay6us() //@11.0592MHz
{
uchar i;

i = 14;
while (--i);

// _nop_(); //5us
// i = 11;
// while (--i);
}

void delay2us() //@11.0592MHz
{
uchar i;

i = 3;
while (--i);
}

//------------------------------------------------------------------------------
//子程序IIC_Start
//------------------------------------------------------------------------------
void IIC_Start(void)
{
INA_SDA=1;
INA_SCL=1;
Delay6us();
INA_SDA=0;
Delay6us();
}

//------------------------------------------------------------------------------
//子程序IIC_Stop(void)
//------------------------------------------------------------------------------
void IIC_Stop(void)
{
INA_SDA=0;
INA_SCL=1;
Delay6us();
INA_SDA=1;
Delay6us();
}

//------------------------------------------------------------------------------
//子程序INA_SentByte
//------------------------------------------------------------------------------
void INA_SendByte(uchar info)
{
uchar i;
bit ACK;

INA_SCL=0;
Delay6us();

for(i=0;i<7;i++) //发送8bit数据
{
if((info<<i)&0x80)
INA_SDA = 1;
else
INA_SDA = 0;
_nop_();
_nop_();
INA_SCL = 1;
Delay6us();
INA_SCL=0;
_nop_();
_nop_();
INA_SDA=0;
Delay6us();
}

i++;
if((info<<i)&0x80)
INA_SDA = 1;
else
INA_SDA = 0;
_nop_();
_nop_();
INA_SCL = 1;
Delay6us();
INA_SCL=0;
_nop_();
_nop_();
INA_SDA=1;
Delay6us();

// INA_SDA = 1;
// Delay6us();
INA_SCL = 1; //在第9个脉冲,等待从器件确认
Delay6us();
ACK=INA_SDA;
while(ACK)
ACK=INA_SDA;
Delay6us();
INA_SCL = 0;
_nop_();
_nop_();
INA_SDA = 0;
Delay6us();
}

//------------------------------------------------------------------------------
//子程序INA_ReceiveWord
//------------------------------------------------------------------------------
uint INA_ReceiveWord(uint info)
{
uchar i;

// INA_SCL=1;
// Delay6us();

for(i=0;i<8;i++)
{
INA_SCL=1;
Delay6us();

info<<=1;
info=info|INA_SDA;
// Delay6us();

INA_SCL=0;
Delay6us();
}
INA_SDA = 1;
Delay6us();
INA_SCL = 1; //在第9个脉冲,等待主器件确认
Delay6us();
INA_SDA=0;
Delay6us();
INA_SCL = 0;
Delay6us();

// INA_SCL=1;
// Delay6us();

for(i=0;i<8;i++)
{
INA_SCL=1;
Delay6us();

info<<=1;
info=info|INA_SDA;
// Delay6us();

INA_SCL=0;
Delay6us();
}
INA_SDA=1; //在第18个脉冲,等待主器件非确定位(高电平)
Delay6us();
INA_SCL = 1;
Delay6us();
Delay6us();
INA_SCL = 0;
_nop_();
_nop_();
INA_SDA = 0;
Delay6us();

return info;
}

//------------------------------------------------------------------------------
//子程序config_INA226
//------------------------------------------------------------------------------
void config_INA226()
{
IIC_Start();
INA_SendByte(0x80); //写命令。从器件地址为1000,0000(最后一位为R/W),A1 A0:GND GND
INA_SendByte(0x00); //写地址。配置寄存器地址。
INA_SendByte(0x45); //配置寄存器。0100'010,1'01,11'0,111(16个平均数,总线2.116ms,分流4.156,共100.352ms)
INA_SendByte(0x77);
IIC_Stop();
}

//------------------------------------------------------------------------------
//子程序cal_INA226
//------------------------------------------------------------------------------
void cal_INA226()
{
IIC_Start();
INA_SendByte(0x80); //写命令。从器件地址为1000,0000(最后一位为R/W),A1 A0:GND GND
INA_SendByte(0x05); //写地址。校准寄存器地址。
INA_SendByte(0x04); //最大电流取6A,Current_LSB=6/(2^15)=0.00018,取0.0002A,R_shut=0.025,CAL=1024(0x0400)
INA_SendByte(0x00);
IIC_Stop();
}

//------------------------------------------------------------------------------
//子程序read_data
//------------------------------------------------------------------------------
uint read_Data(uchar type,uint info)
{
IIC_Start();
INA_SendByte(0x80); //写命令。从器件地址为1000,0000(最后一位为R/W),A1 A0:GND GND
INA_SendByte(type);
IIC_Stop();
IIC_Start();
INA_SendByte(0x81); //读命令。从器件地址为1000,0001(最后一位为R/W),A1 A0:GND GND
INA_ReceiveWord(info);
IIC_Stop();
return info;
}