我买了个射频收发模块,它自带的数字手册上有它的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上改了改程序,没改完,改后的程序如下:
#include
#define MSG_CRC_INIT 0xFFFF// Definitions for CRC constants
#define MSG_CCITT_CRC_POLY 0x1021// Definitions for CRC constants
typedef unsigned char u8;
typedef unsigned int u16;
typedef struct MsgObj// Message object structure for sending/receiving messages
{
u8 dataLen;
u8 opCode;
u8 db[250];
u16 crc;
}MsgObj;
void CRC_calcCrc8(u16 *crc_calc, u8 ch);// This function updates a running CRC value
u16 CRC_calcCrcMsgObj(MsgObj *hMsg);// This function calculates the CRC for an arbitrary message.
u16 CRC;
void main()
{
CRC=0;
while(1);
}
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<<=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+=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;
}
}
u16 CRC_calcCrcMsgObj(MsgObj *hMsg)
{
u16 crcReg, i;
crcReg = MSG_CRC_INIT;// Initialize the CRC register
CRC_calcCrc8(&crcReg, hMsg->dataLen);
CRC_calcCrc8(&crcReg, hMsg->opCode);
for(i=0; idataLen; i++)
{
CRC_calcCrc8(&crcReg, hMsg->db);
}
return(crcReg);
}
编译后在“if (crc_calc & 0x8000)”上出现“error C193:‘&’:bad operand type”的错误,各位大侠帮忙分析分析!!