CRC 16 generate

I have a code snippet for C to generate CRC codes, but the CRC I generate in Arduino is not correct. I think it has something to do with in = 32 bits, but I do not know how to change that in the snippet. Any help would be appreciated.

int = 32 bits, short = 16 bits, char = 8 bits

CRC function

unsigned short Stngr::crc16_lsb(unsigned char *pData, int length) {
unsigned char i;
unsigned short data, crc;
crc = 0xFFFF;
if (length == 0)
return 0;
do {
data = (unsigned int)0x00FF & *pData++;
crc = crc ^ data;
for (i = 8; i > 0; i–) {
if (crc & 0x0001) crc = (crc >> 1) ^ 0x8408;
else crc >>= 1; } }
while (–length);
crc = ~crc;
return (crc);
}

My code

byte message = {0xAA, 0x06, 0xFD, 0x21, 0x00, 0x00};
unsigned short CRC = crc16_lsb(message, sizeof(message);
message[4] = (byte)((short)CRC & (short)0xff);
message[5] = (byte)((short)CRC >> 8);

message result: byte message = {0xAA, 0x06, 0xFD, 0x21, 0x98, 0xCO};

but should have been: byte message[] = {0xAA, 0x06, 0xFD, 0x21, 0x6F, 0xA8};

Your code has a smiley with glasses... 8) and miss a closing parenthesis after sizeof()
(do you need the CRC with the 00 00?)

data = (unsigned int)0x00FF & *pData++;

Seems fishy....

Why do you think you should get that CRC? You can check online what to expect and also read the answers here

Since they are not part of the message, don't include the last two bytes in calculating the CRC. i.e. use

unsigned short CRC = crc16_lsb(message, sizeof(message)-2);

Thanks for the reply. I have changed the code to include sizeof(message)-2 ( I used a hardcoded value for the size before I published the code on this site ( eg. 8 ), sorry for that.

The code compiles ok, and there is no syntax errors as far as I can see. My only problem is that the last two CRC bytes are calculated incorrectly.

When I use the calculator and inputs "AA06FD21" as hex, none of the functions returnes : 6F A8, as far as I can see.

Why do you think you should get that CRC? You can check online what to expect

The hex values including the last two CRC codes, are received from the SOC sultion that I am using. The hex values including CRC are sent from the manufacturer of the SOC so they should be correct.

touPilsner:
Thanks for the reply. I have changed the code to include sizeof(message)-2 ( I used a hardcoded value for the size before I published the code on this site ( eg. 8 ), sorry for that.

I created a test harness out of your code before replying. I get the expected result. Why are you using 8! You have just 4 bytes of message!

The code compiles ok, and there is no syntax errors as far as I can see. My only problem is that the last two CRC bytes are calculated incorrectly.

When I use the calculator and inputs "AA06FD21" as hex, none of the functions returnes : 6F A8, as far as I can see.

If you supplied that string then it will be wrong. You need to supply the hex decoded value as 4 bytes.

The hex values including the last two CRC codes, are received from the SOC sultion that I am using. The hex values including CRC are sent from the manufacturer of the SOC so they should be correct.

Just do as I did and create a short test harness out of the code you presented but use sizeof() -2 .

What's your device and its protocol? Many are using CCITT-16 initialized to 0

I created a test harness out of your code before replying. I get the expected result. Why are you using 8! You have just 4 bytes of message!

You are absolutely correct! Thank you. It is now working. :slight_smile:

touPilsner:
You are absolutely correct! Thank you. It is now working. :slight_smile:

Good to know!

Another option is to use a library function:
http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html