CRC16 calculation is a very simple task once you understand how it is done. De math part only condenses the calculations so you dont have to replicate the "pencil and paper method" and use half your memory area to do it.
The poly is a number that you use as your base CRC number calculation.
For instance, 1 you select your start CRC, either all ZEROs 0000 0000 0000 0000 or all ONEs 1111 1111 1111 1111.
Lets start with 1.
To this point, your CRC is 1111 1111 1111 1111 in Hex its FFFF
Lets also pick the number 54 to calculate the Rev CRC16 of a single Byte of Data
In a reverse CRC16 we Sift all single BIT or your Data and at each sequence we right shift all bit of the CRC
Data = 54, being 0101 0100 becomes 0010 1010 (they shifted one bit to the right)
CRC=1111 1111 1111 1111 becomes 0111 1111 1111 1111
Lets begin:
Now we ADD the polynominal number which is #8408 = 1000 0100 0000 1000 (will explain this number later) to the CRC
Here we do the XOR which in fact is a binairy ADD with no carry:
0111 1111 1111 1111 --------- CRC
1000 0100 0000 1000 --------- Poly
1111 1011 1111 0111 --------- New CRC (see the ADD of individual Bits with no carry? its a XOR)
(0010 1010) ---- the Data right shifted once
Check the rightmost BIT of the Data and compare it to the one of the CRC
IF they are equal you right shift your CRC and Data
IF they are not you right shift AND ADD the polynominal number again.
A Byte having 8 BITs you do this 8 times and have the final CRC result.
So every single step you compare the LSB of the CRC and of the Data to select if you Right shift and then ADD or if you only rightshift.
Heres a "paper" copy of the RevCRC16 CITT calculations
The Polynominal number is such number that when used in the calculation of a CRC will reduce the possibility of error to almost nothing. It has to do with BIT being wrong at the left or the right or in the middle of your data stream. It way above me but thats what I understood.
MSB | LSB | |||||||||||||||||||||
15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | HEX | ||||||
T | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 54 | |||||||||||||
0 | 1 | 0 | 1 | 0 | 1 | 0 | RSHIFT | 2A | 1 | |||||||||||||
0 | 1 | 0 | 1 | 0 | 1 | RSHIFT | 15 | 2 | ||||||||||||||
0 | 1 | 0 | 1 | 0 | RSHIFT | A | 3 | |||||||||||||||
0 | 1 | 0 | 1 | RSHIFT | 5 | 4 | ||||||||||||||||
0 | 1 | 0 | RSHIFT | 2 | 5 | |||||||||||||||||
0 | 1 | RSHIFT | 1 | 6 | ||||||||||||||||||
0 | RSHIFT | 0 | 7 | |||||||||||||||||||
RSHIFT | 8 | |||||||||||||||||||||
CRC H#FFFF | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | FFFF | |||||
0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | RSHIFT | 7FFF | 1 | ||||
H#8408 | + | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ADD POLY | ||||
New CRC | = | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | FBF7 | 1 | |||
0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | RSHIFT | 7DFB | 2 | ||||
H#8408 | + | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ADD POLY | ||||
New CRC | = | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | F9F3 | ||||
0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | RSHIFT | 7CF9 | 3 | ||||
NO ADD | ||||||||||||||||||||||
New CRC | = | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 7CF9 | |||||
0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | RSHIFT | 3EC7 | 4 | |||||
H#8408 | + | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ADD POLY | ||||
New CRC | = | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | BA74 | ||||
1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | RSHIFT | 5D3A | 5 | |||||
H#8408 | + | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ADD POLY | ||||
New CRC | = | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | D932 | ||||
1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | RSHIFT | 6C99 | 6 | |||||
NO ADD | ||||||||||||||||||||||
New CRC | = | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 6C99 | ||||
1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | RSHIFT | 364C | 7 | ||||||
NO ADD | ||||||||||||||||||||||
New CRC | = | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 364C | ||||||
1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | RSHIFT | 1B26 | 8 | |||||||
NO ADD | ||||||||||||||||||||||
New CRC | = | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | = | 1B26 | |||
HEX | 1B | 26 | ||||||||||||||||||||
DEC | 27 | 38 | ||||||||||||||||||||
ASCII | ^[ | & | ||||||||||||||||||||
Comments:
See also:
Interested:
Questions: