Thursday, October 08, 2009

C CODE Error Check CRC-16

Correct transmission of the frame is assured by means of the CRC-16 cyclical redundancy check.
Both CRC-16 characters are generated as follows, based upon all of the characters included in the
frame (slave address to last data byte):


1 Presetting of a 16 bit register (CRC-16 register) with FFFFh
2 Exclusive OR linking of the low bytes in the CRC-16 register to the frame’s character,
results to CRC-16 register
3 Shift the CRC-16 register one bit to the right,
A “0“ is added and the displaced, least significant bit (LSB) is saved
4 Where LSB = 0, continue as of step 5.
Where LSB = 1, establish exclusive OR linking of the CRC-16 registers to A001h.
5 Repeat steps 3 and 4 until a total of 8 shifts to the right have occurred.
At this point, one of the frame’s characters has been processed.
6 Execute steps 2 through 5 for each of the frame’s remaining characters.
7 The content of the CRC-16 register, preceded by the low byte, is added to the frame
after all of the frame’s characters have been processed.

programming in C

unsigned int crc_16 (unsigned char *buffer, unsigned int length) {
unsigned int i, j, lsb, tmp, crc = 0xFFFF;
for ( i = 0; i < tmp =" (unsigned" j =" 0;" lsb =" crc">>= 1;
if ( lsb != 0 ) crc ^= 0xA001;
}
}
return (crc);
}

Relate Posts