ModbusMaster  v2.0.1
Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
"util/crc16.h": CRC Computations

Functions

static uint16_t crc16_update (uint16_t crc, uint8_t a)
 Processor-independent CRC-16 calculation. More...
 

Detailed Description

#include "util/crc16.h"

This header file provides functions for calculating cyclic redundancy checks (CRC) using common polynomials. Modified by Doc Walker to be processor-independent (removed inline assembler to allow it to compile on SAM3X8E processors).

References:
Jack Crenshaw's "Implementing CRCs" article in the January 1992 issue of Embedded Systems Programming. This may be difficult to find, but it explains CRC's in very clear and concise terms. Well worth the effort to obtain a copy.

Function Documentation

§ crc16_update()

static uint16_t crc16_update ( uint16_t  crc,
uint8_t  a 
)
static

Processor-independent CRC-16 calculation.

Polynomial: x^16 + x^15 + x^2 + 1 (0xA001)
Initial value: 0xFFFF

This CRC is normally used in disk-drive controllers.

Parameters
uint16_tcrc (0x0000..0xFFFF)
uint8_ta (0x00..0xFF)
Returns
calculated CRC (0x0000..0xFFFF)
72 {
73  int i;
74 
75  crc ^= a;
76  for (i = 0; i < 8; ++i)
77  {
78  if (crc & 1)
79  crc = (crc >> 1) ^ 0xA001;
80  else
81  crc = (crc >> 1);
82  }
83 
84  return crc;
85 }