Function |
|
Calculate CRC16 (CRC-16-IBM, also known as CRC-16-ANSI) x16 + x15 + x2 + 1 |
Purpose |
|
To verify data transmitted. Used at Bisync, Modbus, USB, ANSI X3.28, SIA DC - 07, and many others |
Syntax |
|
void updateCRC16(uns8 value) |
Parameters |
|
value: next data byte to update |
Input values |
|
Crc16 16b CRC value before update. The initial value (before the first byte is updated) must be specified (the same as at the counterpart). |
Return value |
|
– |
Output values |
|
Crc16 Updated CRC value |
Preconditions |
|
– |
Remarks |
|
– |
Side effects |
|
– |
See also |
|
– |
Example |
|
// Verifying data incoming via UART
bit RxUART(); // Returns TRUE when a byte (stored in variable // RxUARTdata) from UART was successfully received static uns8 RxUARTdata; // Global variable holding the byte received by function // RxUART() … FSR1 = …; // FSR1 points to the message buffer Crc16 = 0xFFFF; // Initial CRC value (use the same value as the one used // at the transmitting counterpart) uns8 msgLength = 0; // Reset the message length while (RxUART()) // Read the message from UART, store it, count the // message length and update CRC { setINDF1(RxUARTdata); // Store the byte received UpdateCrc16 (*FSR1++); // Update CRC and increment the pointer msgLength++; // Update the message length } if (Crc16 == 0) // Is CRC valid? // Yes … |