CCS News

CCS C Compiler Makes It Easy To Calculate a CRC Using the PIC® MCU's CRC Module

Friday 26 May, 2017

Reliably sending data between two devices usually involves a checksum. Creating a checksum of data is a great way to detect if data has be successfully received; by comparing a received checksum to a mathematically determined checksum it can be determined if the data was sent and received properly. A common and easy method of performing a checksum is just summing or XORing all the bytes in the data. Unfortunately this method does accurately detect situations where data may have been received correctly. For example, the result of 0 XOR 0 is still 0. That means a checksum based on XOR would couldn't tell the difference between 0x0000 or 0x00.

The Cyclic Redundancy Check (CRC) is a more robust method for hashing data. In reference to the previous example, the CRC of 0x0000 isn't 0x00. Many new PIC® MCUs released today include a CRC module that perform this algorithm quickly in hardware, and CCS provides a library for accessing this module.

There are two basic versions of the CRC module that some Microchip PIC® MCUs have, a 16-bit module and a 32-bit module. The following getenv() statement can be used in the CCS C Compiler to determine if the device has a CRC module:

getenv("CRC") //will return 1 if it device has a HW CRC module

Both version of the CRC module has a programmable polynomial CRC equation, for the 16-bit modules the polynomial can be up to 16 bits wide and for the 32-bit modules the polynomial can be up to 32 bits wide. The setup_crc() function can be used in the CCS C Compiler for setting up the CRC polynomial. For example the following will setup the CRC module to generate a checksum using the CRC-16 CCITT polynomial, x^16 + x^12 + X^5 + 1:

setup_crc(16, 12, 5, 0);

To calculate the CRC checksum the CCS C Compiler supplies the following functions, crc_calc8(), crc_calc16 and crc_calc32(), for devices with a 32-bit CRC module only. The function that should be used depends on the polynomial size that was set with the setup_crc() function. For polynomials that are 8-bits or less the crc_calc8() function should be used, for polynomials that are greater then 8-bits and 16-bits or less the crc_calc16() function should be used and for polynomials that are greater then 16-bits and 32-bits or less the crc_calc32() function should be used. Additionally the crc_calc8(), crc_calc16() and crc_calc32() have an optional parameter that allows setting the width of the data that the CRC checksum is being calculated for, the data width defaults to the width the function, 8-bits for crc_calc8(), 16-bits for crc_calc16() and 32-bits for crc_calc32(). Finally the CCS C Compiler supplies that crc_init() function that is used to set the initialization value of the CRC generator. This function should be called before every call to one of the CRC calculation function to guarantee correct results.

The following is an example of using the CRC module to generate a 16-bit CRC for some 8-bit data that will be transmitted with the UART peripheral:

#use rs232(UART1, baud=9600, errors)
#define START 0x01
#define END 0x0D
void tx_data(unsigned int8 *Data, unsigned int8 Length)
unsigned int16 CRC;

CRC = crc_calc16(Data, Length, 8);
printf("%s%04LX", Data, CRC);


void main(void)
unsigned int8 Data[32];
unsigned int8 Length;
unsigned int16 Count = 0;

setup_crc(16, 12, 5, 0);

Length = sprintf(Data, "Count=%lu,", Count++);
tx_data(Data, Length);

Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.