Hi, All
Some i2C sensor chips use such CRC check.
How to calculate such CRC ?
Some i2C sensor chips use such CRC check.
How to calculate such CRC ?
Source code::
- initial value=0xFF, polynomial=(x8 + x5 + x4 + 1) ie 0x31 CRC [7:0] = 1+X4+X5+X8
*/
/**************************************************************************/
bool AHTxx::_checkCRC8()
{
{
uint8_t crc = 0xFF; //initial value
for (uint8_t byteIndex = 0; byteIndex < 6; byteIndex ++) //6-bytes in data, {status, RH, RH, RH+T, T, T, CRC}
{
crc ^= _rawData[byteIndex];
for(uint8_t bitIndex = 8; bitIndex > 0; --bitIndex) //8-bits in byte
{
if (crc & 0x80) {crc = (crc << 1) ^ 0x31;} //0x31=CRC seed/polynomial
else {crc = (crc << 1);}
}
}
return (crc == _rawData[6]);
}