unsigned char rfid[4] = {
5, 0, 0, 0xFE
};
unsigned char test[3] = {
4, 0, 1
};
void setup()
{
uint16_t crc_out;
Serial.begin(9600);
// while(!Serial);
// delay(1000);
// See: http://forum.arduino.cc/index.php?topic=235918.0
// and see the user's manual in reply #4 for the code to
// calculate the CRC on page 6
// pass in the address of the bytes to be processed for a CRC
// and the the number of bytes. In this case I used the four
// bytes shown in the example in the thread at the URL above
// and there are 4 bytes. The code prints the correct CRC (7387)
crc_out = rfid_crc16(rfid,4);
// NOTE that this prints the result in the order MSByte LSByte
// but you must send them in the order LSByte and then MSByte
// This prints the CRC as 0x7387 but you would
// send it as 0x87 0x73
Serial.print(crc_out,HEX); //(expecting = 0x7387)
Serial.println(" rfid");
Serial.println("");
// generates 0x4BDB
crc_out = rfid_crc16(test,3);
Serial.print(crc_out,HEX);
Serial.println(" test");
}
void loop()
{
}
// This is exactly the code from page 6
#define PRESET_VALUE 0xFFFF
#define POLYNOMIAL 0x8408
unsigned int rfid_crc16(unsigned char const *pucY, unsigned char ucX)
{
unsigned char ucI,ucJ;
unsigned short int uiCrcValue = PRESET_VALUE;
for(ucI = 0; ucI < ucX; ucI++) {
uiCrcValue = uiCrcValue ^ *(pucY + ucI);
for(ucJ = 0; ucJ < 8; ucJ++) {
if(uiCrcValue & 0x0001) {
uiCrcValue = (uiCrcValue >> 1) ^ POLYNOMIAL;
}
else {
uiCrcValue = (uiCrcValue >> 1);
}
}
}
return uiCrcValue;
}
The first test uses the data from the example that you have circled in RFID.jpg which you attached to your first post. The data are the four bytes 5, 0, 0, 0xFE and the example shows the CRC as 87 73. The program prints 7387 becuase it prints the MSB (73) first whereas you must send the LSByte first to the RFID device.
The second test uses the data from the code in your first post: 4, 0, 1 and the result shows that the CRC is 0x4BDB which you would have to send as 0xDB and then 0x4B.
Pete