我买了个射频收发模块,它自带的数字手册上有自己的CRC校验程序如下:
An example implementation of CRC calculation on a message is shown next
// Definitions for CRC constants
#define MSG_CRC_INIT(0xFFFF)
#define MSG_CCITT_CRC_POLY(0x1021)
// Message object structure for sending/receiving messages
typedef struct MsgObj
{
u8 dataLen;
u8 opCode;
u16 data[MSG_MAX_DATA_LENGTH];
u16 crc;
} MsgObj;
// This function updates a running CRC value
void CRC_calcCrc8(u16 *crc_calc, u8 ch)
{
u8 i, v, xor_flag;
// Align test bit with leftmost bit of the message byte.
v = 0x80;
for (i=0; i<8; i++)
{
if (crc_calc & 0x8000)
{
xor_flag = 1;
}
else
{
xor_flag = 0;
}
crc_calc = crc_calc <<1;
if (ch & v)
{
// Append next bit of msg to CRC if it a one. The zero bit placed there by the
shift above need not be changed if the next bit in msg is zero.
crc_calc= crc_calc + 1;
}
if (xor_flag)
{
crc_calc = crc_calc ^ MSG_CCITT_CRC_POLY;
}
// Align test bit with next bit of the message byte. v = v >> 1;
}
}
// This function calculates the CRC for an arbitrary message.
u16 CRC_calcCrcMsgObj(MsgObj *hMsg)
{
u16 crcReg, i;
// Initialize the CRC register
crcReg = MSG_CRC_INIT;
CRC_calcCrc8(&crcReg, MSG_CCITT_CRC_POLY, hMsg->dataLen);
CRC_calcCrc8(&crcReg, MSG_CCITT_CRC_POLY, hMsg->opCode);
for(i=0; idataLen; i++)
{
CRC_calcCrc8(&crcReg, MSG_CCITT_CRC_POLY, hMsg->data);
}
return(crcReg);
}
我用keil uvision3自己改了改上面的程序,没改完,编译时在“if (crc_calc & 0x8000)”一行出现了“error C193:'&':bad operand type”的错误,各位大侠帮忙分析分析!!