Trying to understand crc8
. This is my calculations:
poly 100110001 # OneWire
bin 00000001 # 1
1. 000000010 << 1 = 000000100
2. 000000100 << 1 = 000001000
3. 000001000 << 1 = 000010000
4. 000010000 << 1 = 000100000
5. 000100000 << 1 = 001000000
6. 001000000 << 1 = 010000000
7. 010000000 << 1 = 100000000
8. 100000000 ^ 100110001 = 000110001 << 1 = 001100010 == 00110001 # 8 digits
crc8 = 0x31 # online calc true
bin 01000001 # 41
1. 010000010 << 1 = 100000100
2. 100000100 ^ 100110001 = 000110101 << 1 = 001101010
3. 001101010 << 1 = 011010100
4. 011010100 << 1 = 110101000
5. 110101000 ^ 100110001 = 010011001 << 1 = 100110010
6. 100110010 ^ 100110001 = 000000011 << 1 = 000000110
7. 000000110 << 1 = 000001100
8. 000001100 << 1 = 000011000 == 00001100
crc8 = 0xC # online calc true
Now need crc8
of 141
The first one plus the second. Using online calculator https://ghsi.de/CRC/index.php?Polynom=100110001&Message=141 I see that crc8
of 141
must be 0xF8
. But 0x31 + 0xC
will be 3D
. Where is the error?
First off, CRCs don't add like that. You need to take the register value after running 0x01
through, which gives 0x31
, and use that as the starting register value when feeding 0x41
. You need to exclusive-or the 0x41
with the 0x31
as the first step, resulting in 0x70
. Given that, eight steps give you 0xf8
as expected.
However you have not yet arrived at the OneWire CRC-8. That CRC-8 is computed in the reversed direction, shifting down instead of up and using the polynomial (excluding the x8) reversed. Unlike both what you did and what that website does, which is shifting up. What's more, the message is fed in backwards.