3. Shift the CRC-16 register one bit to the right (toward the LSB), zero-filling the MSB.
Extract and examine the LSB. (If the LSB was 0): Repeat Step 3 (another shift). (If the
LSB was 1): Exclusive OR the CRC-16 register with the polynomial value A001h (1010
0000 0000 0001b).
4. Repeat Steps 3 and 4 until 8 shifts have been performed. When this is done, a complete
byte will have been processed.
5. Repeat Steps 2 through 5 for the next byte of the message. Continue doing this until all
bytes have been processed.
6. The final contents of the CRC-16 register is the CRC-16 value.
When the CRC-16 (16 bytes) is transmitted in the message, the low byte will be transmitted first,
followed by the high byte.
An example of a C language function performing CRC generation is shown below.
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
crc_16 calculate the crc_16 error check field
Input parameters:
buffer: string to calculate CRC
length: bytes number of the string
This function returns the CRC value.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
unsigned int crc_16 (unsigned char *buffer, unsigned int length)
{
unsigned int i, j, temp_bit, temp_int, crc;
crc = 0xFFFF;
for ( i = 0; i < length; i++ ) {
temp_int = (unsigned char) *buffer++;
crc ^= temp_int;
for ( j = 0; j < 8; j++ ) {
temp_bit = crc & 0x0001;
crc >>= 1;
if ( temp_bit != 0 )
crc ^= 0xA001;
}
}
return (crc);
}
12.5 Function Code 3 & 4: Words Reading
These function codes are used by the master unit to read a consecutive group of words (16 bit)
which contain the value of the variable of the slave unit. The master can require a maximum of
20 words at a time.38 M odbus/Jbus Communications