I'm trying to implement a CRC check on my Arduino and Thanks to you guys I think I've got this working:
unsigned int CRC(byte input[], int array_size)
{
unsigned int crc16 = 0xFFFF;
int pos = 0;
int i = 0;
/*Main Code*/
for (pos = 0; pos < sizeof(input); pos++) {
crc16 ^= input[pos]; // XOR byte into least sig. byte of crc
for (i = 8; i != 0; i--) { // Loop over each bit
if ((crc16 & 0x0001) != 0) { // If the LSB is set
crc16 >>= 1; // Shift right and XOR 0xA001
crc16 ^= 0xA001;
}
else // Else LSB is not set
crc16 >>= 1; // Just shift right
}
}
/*Note, this number has low and high bytes swapped,
so swap bytes*/
return crc16;
}
byte crc[]
stack-allocatescrc
, so it goes away when the function returns; ie, officially is no good as a returned result. Also, it doesn't match thebyte
type of the function;crc
is abyte[]
orbyte*
. Just compute and return anunsigned int
instead of an array – James Waldby - jwpat7 May 16 '17 at 16:56byte*
will it work as intended? – ATE-ENGE May 16 '17 at 17:06sizeof(input)
, you get the sizeof a byte pointer. You need to pass the length of the array as a parameter, – Johnny Mopp May 16 '17 at 17:30