项目急:利用STM8L151与BQ7694003制作BMS系统,通过模拟I2C实现MCU与BQ7694003的通信,现在情况是:带CRC8校验,连续写多个是正常的;但是读的时候,发现带CRC8校验读操作时,校验不正确,即是可以读出来第一个数据,且正确,但是读出来的第二个字节就是oxFF,现在利用的是每次读一个字节,不带CRC8校验,读出来的数据是正确的,但是我想要可以实现连续读的操作,请问这是哪里的问题,谢谢!
/*****给寄存器写入多个字节**带CRC8校验****/
uint8_t BQ7694000_WriteRegs(unsigned char reg_add,unsigned char *reg_dat,unsigned char num)
{
uint8_t send_buffer[BQ769X0_REGISTER_NUM * 2 + 2];
int8_t result;
uint8_t i, *pt;
send_buffer[0] = BQMAXIMO;
send_buffer[1] = reg_add;
send_buffer[2] = reg_dat[0];
send_buffer[3] = CRC8(send_buffer, 3, CRC_KEY);
//写操作:第一个CRC8是对slave address, register address, and 第一个data的计算
pt = send_buffer + 4;
for(i = 1; i < num; i++)
{
*pt++ = reg_dat[i];
*pt++ = CRC8(reg_dat + i, 1, CRC_KEY);
}
result = HalBq769x0I2CSendBytes(send_buffer + 1, 2 * num + 1);
//2 * num + 1:2倍的num是因为一个data对应一个CRC8,1是指reg_add
//send_buffer + 1:是指send_buffer[0]不用,从send_buffer[1]开始
return result; //return 0正确 return 1不正确
}
//读字节,不带CRC8校验
uint8_t BQ7694000_ReadData(unsigned char reg_add,unsigned char*Read,unsigned char num)
{
unsigned char i;
uint8_t TargetRegister = reg_add;
HalBq769x0I2CSendBytes(&TargetRegister, 1);
return(HalBq769x0I2CReadBytes(Read, num));
}
static int8_t HalBq769x0I2CReadBytes(uint8_t *DataBuffer, uint16_t ExpectedByteNumber)
{
uint16_t i;
i2c_Start();
i2c_SendByte(BQMAXIMO | BQ_CMD_READ);
if(i2c_WaitAck())
return 0x01;
else
{
for(i = 0; i < ExpectedByteNumber; i++)
{
if(i == ExpectedByteNumber – 1)
{
DataBuffer[i] = i2c_ReadByte(0);
}
else
{
DataBuffer[i] = i2c_ReadByte(1);
}
}
i2c_Stop();
return 0;
}
}
Johnsin Tao:
Hi
建议你确认接口时序。