I have used this C code posted by others in the forum. But i found that i can only do CRC for 7 bytes. Is it the problem of only 16 elements in the feedback array? or why?
/**************************************************************************** * * NAME: Crc8() * PURPOSE: Generate 8-bit CRC. * * SYNOPSIS: unsigned char Crc8(unsigned char b, unsigned char initCrc); * * INTERFACE PARAMETERS: * * Name FG-IO Description * ------------------- ----- ----------------------------------------- * b F I Data byte to CRC (MSBit first). * initCrc F I Initial CRC or CRC accumulator for * continuation. * <return value> O CRC. * * DESCRIPTION: * * The function returns the 8-bit CRC after including "b" with the * initial CRC "initCRC". * ****************************************************************************/ static const UCHAR code feedback[16] = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D }; UCHAR CRC8(UCHAR b, UCHAR CRC) { CRC = (CRC << 4) ^ feedback[((CRC) ^ b) >> 4]; CRC = (CRC << 4) ^ feedback[((CRC >> 4) ^ b) & 0x0F]; return (CRC); }
By the way, i hope to do CRC for 256 byte data and i just input 0xFF for the initial CRC. Is there anything wrong?
"But i found that i can only do CRC for 7 bytes." What exactly is the problem? Examples of failure would be the things to post. "Is it the problem of only 16 elements in the feedback array?" Feedback tables have 2**n elements, where 'n' is the number of bits being processed in parallel. In this case the CRC is calculating 4 bits at a time, 2**4 = 16, so the feedback table should have 16 elements. No problem.
"By the way, i hope to do CRC for 256 byte data and i just input 0xFF for the initial CRC. Is there anything wrong? That is too much data to cover with only an 8-bit CRC. Generally speaking, an n-bit CRC's error detection properties degrade after 2**(n-1)-1 data bits. Thus, an 8-bit CRC should be avoided for data blocks over 15 bytes in length. For 256 byte data, you really should be using a 16-bit CRC.