大神帮我看看这个串口CRC校验程序怎么理解,CRC算法跟网上的一样的crc16吧
//the following is MCU code for CRC16 ,please refer.
//-------------------------------------------------------------------------------------------
#define RxCountMax 18
unsigned short RxBuf[RxCountMax];
unsigned short TxBuf[10];
unsigned short RxCnt;
unsigned short TxCnt;
unsigned short Rx50msCnt;
unsigned long pAddr1,pAddr2,pAddr3,pAddr4;
//Receive interrupt routine
void voRxIsr(void)
{
unsigned short i,CRC_RX,CRC_Tmp;
RxBuf[RxCnt] = Rx.data; //acquire data
RxCnt++;
if(RxCnt == RxCountMax) {
CRC_Tmp = CRC_CHECK(RxBuf,16); //CRC Calculation
CRC_RX = ((unsigned short)RxBuf[RxCountMax-1]<<8) + RxBuf[RxCountMax-2];
if(CRC_Tmp == CRC_RX){
pAddr1 = ((ULONG)(RxBuf[0x3])<<24)|((ULONG)(RxBuf[0x2])<<16)|((ULONG)(RxBuf[0x1])<<8)|RxBuf[0x0];
pAddr2 = ((ULONG)(RxBuf[0x7])<<24)|((ULONG)(RxBuf[0x6])<<16)|((ULONG)(RxBuf[0x5])<<8)|RxBuf[0x4];
pAddr3 = ((ULONG)(RxBuf[0xB])<<24)|((ULONG)(RxBuf[0xA])<<16)|((ULONG)(RxBuf[0x9])<<8)|RxBuf[0x8];
pAddr4 = ((ULONG)(RxBuf[0xF])<<24)|((ULONG)(RxBuf[0xE])<<16)|((ULONG)(RxBuf[0xD])<<8)|RxBuf[0xC];
}
RxCnt = 0;
}
Rx50msCnt = 0;
//to add--Clear Receive Data Register Fll flag;
}
//-------------------------------------------------------------------------------------------
//Transfer interrupt routine
void voTxIsr(void)
{
if(TxCnt <= 9){
Tx.Register = TxBuf[TxCnt];
//Clear Tx interrupt flag
TxCnt++;
if(TxCnt >= 10){
//send interrupt disable
}
}
}
//-------------------------------------------------------------------------------------------
//Monitor routine Execute every T Period time
void voMonitor(void)
{
unsigned short ChxData[4],CRC_Tmp,i;
Rx50msCnt++;
if(Rx50msCnt >=500) {
RxCnt = 0;
Rx50msCnt = 0;
}
ChxData[0].sw = *(unsigned short *)pAddr1;
ChxData[1].sw = *(unsigned short *)pAddr2;
ChxData[2].sw = *(unsigned short *)pAddr3;
ChxData[3].sw = *(unsigned short *)pAddr4;
for(i=0;i<4;i++){
TxBuf[2*i+0] = ChxData[i].sb.low; // ch[i] low byte
TxBuf[2*i+1] = ChxData[i].sb.hi; // ch[i] hi byte
}
CRC_Tmp = CRC_CHECK(TxBuf,8);
TxBuf[8] = CRC_Tmp&0xff;
TxBuf[9] = CRC_Tmp>>8;
//send first data
Tx.Register = TxBuf[0]; //send begin
//Enalbe TXD interrupt
TxCnt = 1;
} |