The dspic-utility.h header file defines two functions for calculating the 16-bit CRC-CCITT value of a stream of bytes.
These functions are based on an article by Joe Geluso, originally located at http://www.joegeluso.com/software/articles/ccitt.htm. Unfortunately, it seems that that site no longer exists, but a copy of the article can be found in the Wayback Machine at http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm.
To use these functions, you can use the -I compiler option to add the dsPIC Helper Library source directory to the compiler command line, and then include the header file like so:
#include <crc-ccitt.h>
Call this function with the first byte in the stream, using an initial crc value of CRC_CCITT_INITIAL_VALUE . The function returns the crc value to use in the call for the next byte in the stream. Once all the bytes have been processed, call crc_ccitt_normalize() to retrieve the actual CRC value.
Once all the bytes have been processed, call this function with crc set to the last value returned by crc_ccitt() to retrieve the actual CRC value.
Here’s a simple example that calculates the 16-bit CRC-CCITT value for the string "123456789". A call to test() will return value 0xE5CC:
#include <crc-ccitt.h> uint16_t test(void) { const char* str = "123456789"; uint16_t crc = CRC_CCITT_INITIAL_VALUE; while (*str) crc = crc_ccitt(crc, *str++); return crc_ccitt_normalize(crc); }