The MRBus 16-bit CRC Algorithm
// MRBus C Reference Implementation
// Created by the pycrc library
// Input parameters:
// python pycrc.py --poly 0xa001 --width 16 --xor-in 0 --xor-out 0 --reflect-out 0 --reflect-in 0 --generate c --algorithm bit-by-bit-fast

typedef unsigned short crc_t;

crc_t crc_update(crc_t crc, const unsigned char *data, size_t data_len)
{
    unsigned int i;
    unsigned int bit;
    unsigned char c;

    while (data_len--) {
        c = *data++;
        for (i = 0x80; i > 0; i >>= 1) {
            bit = crc & 0x8000;
            if (c & i) {
                bit = !bit;
            }
            crc <<= 1;
            if (bit) {
                crc ^= 0xa001;
            }
        }
        crc &= 0xffff;
    }
    return crc & 0xffff;
}

int main(void)
{
   const unsigned char pktData[] = { 1,2,3,4,5,6,7,8,9 };
   int i;
   crc_t crc = 0;

   crc = crc_update(crc, pkt, sizeof(pktData));
   printf("CRC = %04X\n", crc);
   return(0);
}
Copyright 2012 by the MRBus Group.
Licensed under a Creative Commons Attribution-ShareAlike 3.0 License.
Questions? Comments? Please email us at support@iascaled.com

Last modified on February 19, 2012, at 01:36 PM