中级会员
 
- 积分
- 230
- 金钱
- 230
- 注册时间
- 2018-2-9
- 在线时间
- 137 小时
|
/**
* @brief DS18B20专用CRC-8校验(逆序位处理)
* @param data 待校验数据指针
* @param len 数据长度
* @return CRC-8校验值(若与第9字节匹配则返回0)
*/
uint8_t DS18B20_CRC8(const uint8_t *data, uint8_t len) {
uint8_t crc = 0x00;
for (uint8_t i = 0; i < len; i++) {
crc ^= data;
for (uint8_t j = 0; j < 8; j++) {
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C; // 多项式0x8C(0x31的反转)
else crc >>= 1;
}
}
return crc;
}
------------------------------------------------------------------------
// 读取暂存器(9字节)
DS18B20_Reset(GPIOx, GPIO_Pin);
DS18B20_WriteByte(GPIOx, GPIO_Pin, 0xCC);
DS18B20_WriteByte(GPIOx, GPIO_Pin, 0xBE); // 读取暂存器
for (uint8_t i = 0; i < 9; i++) {
scratchpad = DS18B20_ReadByte(GPIOx, GPIO_Pin);
}
// eMBEnable(); // 重新开启中断
// CRC校验(前8字节计算值应与第9字节匹配)
if (DS18B20_CRC8(scratchpad, 8) != scratchpad[8]) {
return lasttemp; //
}
这是我的校验函数和如何使用的 |
|