21ic问答首页 - 盛思瑞CO2传感器读数据错误,百思不解。
盛思瑞CO2传感器读数据错误,百思不解。
这是CRC函数:uint8_t sensirion_i2c_generate_crc(const uint8_t* in, uint16_t count) {
uint16_t current_byte;
uint8_t crc = CRC8_INIT;
uint8_t crc_bit;
/* calculates 8-Bit checksum with given polynomial */
for (current_byte = 0; current_byte < count; ++current_byte) {
crc ^= (in[current_byte]);
for (crc_bit = 8; crc_bit > 0; --crc_bit) {
if (crc & 0x80)
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
else
crc = (crc << 1);
}
}
return crc;
} uint8_t buf[2] = {0x00, 0x11}; #define CRC8_POLYNOMIAL 0x31
#define CRC8_INIT 0xFF
#define CRC8_LEN 1 crc_test = sensirion_i2c_generate_crc((uint8_t*)&buf[0],2); 下面是发指令代码: iic_Start();
// 1. Write command phase (use write address 0x28)
iic_SendData(0x52); // Slave write address (0x29 >> 1 = 0x14? Or check datasheet for correct write address)
iic_Wait_Ack();
iic_SendData(0x36); // Command byte 3
iic_Wait_Ack();
iic_SendData(0x15); // Argument byte 1
iic_Wait_Ack();
iic_SendData(0x00); // Argument byte 2
iic_Wait_Ack();
iic_SendData(0x11); // Argument byte 3
iic_Wait_Ack();
iic_SendData(0xf3); // Argument byte 3
iic_Wait_Ack();
iic_Stop();
delay_100us(100); 出现的错误现象是:iic_SendData(0xf3);就会通讯失败,注意0xF3是CRC计算值, 但是如果任意发一个CRC字节,不是计算的0xf3结果, 任意一个。比如:0xfd, 或者0x31,这时虽然下面的读数据失败,但是通讯并没有死亡。再后面的读product id指令还能读到结果。 但是如果发的是函数计算的0xf3作为CRC字节发送,通讯就死亡。后面读product id也失败了。为什么发的是正确的CRC反而导致通讯死亡呢?
uint16_t current_byte;
uint8_t crc = CRC8_INIT;
uint8_t crc_bit;
/* calculates 8-Bit checksum with given polynomial */
for (current_byte = 0; current_byte < count; ++current_byte) {
crc ^= (in[current_byte]);
for (crc_bit = 8; crc_bit > 0; --crc_bit) {
if (crc & 0x80)
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
else
crc = (crc << 1);
}
}
return crc;
} uint8_t buf[2] = {0x00, 0x11}; #define CRC8_POLYNOMIAL 0x31
#define CRC8_INIT 0xFF
#define CRC8_LEN 1 crc_test = sensirion_i2c_generate_crc((uint8_t*)&buf[0],2); 下面是发指令代码: iic_Start();
// 1. Write command phase (use write address 0x28)
iic_SendData(0x52); // Slave write address (0x29 >> 1 = 0x14? Or check datasheet for correct write address)
iic_Wait_Ack();
iic_SendData(0x36); // Command byte 3
iic_Wait_Ack();
iic_SendData(0x15); // Argument byte 1
iic_Wait_Ack();
iic_SendData(0x00); // Argument byte 2
iic_Wait_Ack();
iic_SendData(0x11); // Argument byte 3
iic_Wait_Ack();
iic_SendData(0xf3); // Argument byte 3
iic_Wait_Ack();
iic_Stop();
delay_100us(100); 出现的错误现象是:iic_SendData(0xf3);就会通讯失败,注意0xF3是CRC计算值, 但是如果任意发一个CRC字节,不是计算的0xf3结果, 任意一个。比如:0xfd, 或者0x31,这时虽然下面的读数据失败,但是通讯并没有死亡。再后面的读product id指令还能读到结果。 但是如果发的是函数计算的0xf3作为CRC字节发送,通讯就死亡。后面读product id也失败了。为什么发的是正确的CRC反而导致通讯死亡呢?
赞0
评论
2025-09-19
赞0
评论
2025-09-18
赞0
评论
2025-09-18
赞0
评论
2025-09-18
赞0
评论
2025-09-18
赞0
评论
2025-09-17
您需要登录后才可以回复 登录 | 注册