各位好:
我们在使用BQ27541-G1读取电池电压、温度等数据时发现读数一直不正常,I2C接口我们是用普通IO模拟的,在和其他设备通讯时都正常,也都加了10K的上拉。在执行Module_bq27541_G1_ReadBatteryPercent时,读不到数据,DEBUG发现,程序在运行到Module_I2C_Software_ReadBytes_1时,在“//发器件地址+1(read)”时,无法收到ACK从而报错退出了。(如果无视此错误,继续读数,则会读到0x7FFF)同样,读温度、电压时,也会出现同样的错误。请问这可能是哪里出现了问题?这部分的程序如下:
I2C_ErrorTypeDef Module_I2C_Software_ReadBytes_1(uint8_t ucIndex,uint8_t ucIcAddr,uint8_t ucRegAddr,
uint8_t *pucData, uint32_t ulNum)
{
uint16_t n = 0;
I2C_ErrorTypeDef ucError = I2C_ERR_OK;
//入口参数检查
if (ucIndex > 4) { return I2C_ERR_PARAM_INDEX; }
//发送起始位
ucError = I2C_SwStart(ucIndex);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_SEND_START; }
//发器件地址+0(write)
ucError = I2C_SwSendSlaveAddress(ucIndex,ucIcAddr, 0);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_SEND_SLAVE_ADDR; }
//发寄存器地址
ucError = I2C_SwSendByte(ucIndex,ucRegAddr);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_SEND_DATA_ADDR; }
//发送起始位
ucError = I2C_SwStart(ucIndex);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_SEND_START; }
//发器件地址+1(read)
ucError = I2C_SwSendSlaveAddress(ucIndex,ucIcAddr, 1);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_SEND_SLAVE_ADDR; }
//读ulNum个数据
for (n=0; n<ulNum; n++)
{
if (n >= ulNum-1) //产生NoACK
{
ucError = I2C_SwReceiveByte(ucIndex,pucData, 0);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_RECEIVE_DATA; }
}
else //产生ACK
{
ucError = I2C_SwReceiveByte(ucIndex,pucData, 1);
if (ucError) { I2C_SwStop(ucIndex); return I2C_ERR_RECEIVE_COMPLETE; }
}
pucData++;
}
//发停止位
I2C_SwStop(ucIndex);
return I2C_ERR_OK;
}
du dudu:
请问您的问题怎么解决的?我也遇到了同样的问题,谢谢