C.TY TNHH TỰ ĐỘNG HÓA VIỆT TRUNG 02413.281.181-0989.984.666
Website:www.viet-trung.com.vn Đ/c:194-Nguyễn Trãi-Võ Cường-TP.Bắc Ninh
133
Serial
Communications
User
Ma
nual
(2)
CRC
Generation
Generating a CRC
Step 1
Load a 16-bit register with FFFF hex (all 1's). Call this the CRC register.
Step 2
Exclusive OR the first eight-bit byte of the message with the low order byte
of the 16-bit CRC register, putting the result in the CRC register.
Step 3
Shift the CRC register one bit to the right (toward the LSB), zero filling the
MSB. Extract and examine the LSB.
Step 4
If the LSB is 0, repeat Step 3 (another shift). If the LSB is 1, Exclusive OR
the CRC register with the polynomial value A001 hex (1010 0000 0000
0001).
Step 5
Repeat Steps 3 and 4 until eight shifts have been performed. When this is
done, a complete eight-bit byte will have been processed.
Step 6
Repeat Steps 2 ... 5 for the next eight-bit byte of the message. Continue
doing this until all bytes have been processed.
The final contents of the CRC register is the CRC value.
Step 7
When the CRC is placed into the message, its upper and lower bytes must
be swapped as described below.
Pseudo code for generating a CRC-16:
CONST ARRAY BUFFER /* data, ex: F0h, 06h, 00h, 80h, 17h, 70h */
CONST WORD POLYNOMIAL = 0A001h /* X16 = X15 + X2 + X1 */
/* SUBROTINUE OF CRC CACULATE START */
CRC_CAL(LENGTH)
VAR INTEGER LENGTH;
{ VAR WORD CRC16 = 0FFFFH; /* CRC16 initial */
VAR INTEGER = i,j; /* LOOP COUNTER */
VAR BYTE DATA; /* DATA BUFFER */
FOR (i=1;i=LENGTH;i++) /* BYTE LOOP */
{ DATA == BUFFER[i];
CRC16 == CRC16 XOR DATA
FOR (j=1;j=8;J++) /* BIT LOOP */
{ IF (CRC16 AND 0001H) = 1 THEN
CRC16 == (CRC16 SHR 1) XOR POLYNOMIAL;
ELSE
CRC16 == CRC16 SHR 1;
DATA == DATA SHR 1;
};