Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

CRC7 demo C code

/* This JRK CRC-7 code was written by Daniel at Point1 Seconds Inc.
   Pass in a pointer to an array of unsigned 8bit data along with the data length.
   Make sure that your array is at least one byte longer than the length to store the CRC
   It is free to use or modify as you like.

typedef unsigned char   U8;
typedef unsigned short  U16;
typedef unsigned int    U32;

typedef signed char     S8;
typedef signed short    S16;
typedef signed int      S32;
typedef signed long long S64;
typedef float           F32;
typedef double          F64;

typedef union _WORD {
    U16 u16;
    S16 s16;
    struct {
        U8 byte0;
        U8 byte1;
    U8 u8p[2];

#define JRKpoly91 0x91

void jrkCalcPoly(U8* u8p_msg, U8 u8_Len){
    U8 u8_Shifts, u8_MsgIdx = 1;
    UW uw_CRC;
    uw_CRC.u8p[0] = u8p_msg[0];
    u8p_msg[u8_Len] = 0;
    while(u8_MsgIdx <= u8_Len){
        uw_CRC.u8p[1] = u8p_msg[u8_MsgIdx];
        for(u8_Shifts = 0; u8_Shifts < 8; u8_Shifts++){
            if(uw_CRC.u8p[0] & 0x01){
                uw_CRC.u8p[0] ^= JRKpoly91;
            uw_CRC.u16 >>= 1;
    u8p_msg[u8_Len] = uw_CRC.u8p[0];

void jrkTransmit(U8* u8p_data, U8 u8_Len,  U8 u8_NumBytesReturned){

    /* add your transmit and receive code here */