I want to use the CRC module of MC56F84462 to calculate CRC-modbus 。
I have tried so many times ,but still have not get the right answer。
for example,the input data are:0xF4,0x02,0x06,0x12,and the right CRC(modbus) is 0x7910。
how can i get it?
my code is here:
CRC init:
void CRC_Init(void) { /* CRC_CTRL: TOT=1,TOTR=1,??=0,FXOR=0,WAS=0,TCRC=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */ setReg32(CRC_CTRL, 0x50000000UL); /* CRC_GPOLY: HIGH=0,LOW=0x8005 */ setReg32(CRC_GPOLY, 0x8005UL); /* CRC_CTRL: WAS=1 */ setReg32Bits(CRC_CTRL, 0x02000000UL); /* CRC_CRC: HU=0xFF,HL=0xFF,LU=0xEA,LL=0xA8 */ setReg32(CRC_CRC, 0xFFFFFFFFUL); /* CRC_CTRL: WAS=0 */ clrReg32Bits(CRC_CTRL, 0x02000000UL); }
CRC calculate code is here:
{ unsigned char temp_size=0; unsigned char temp_i=0; unsigned char temp_o = 0; unsigned char *temp_p; unsigned short temp_in; setReg32Bits(CRC_CTRL, 0x02000000UL); setReg32(CRC_CRC, 0xFFFFFFFFUL); clrReg32Bits(CRC_CTRL, 0x02000000UL); //setReg32(CRC_GPOLY,0x8005); temp_size = data_size/2; temp_o=0; for(temp_i=0;temp_i<temp_size;temp_i++) { temp_in = (((unsigned short)(*data_buf)<<8)|*(data_buf+1)); setReg32(CRC_CRC,temp_in); temp_o +=2; data_buf +=2; } if(temp_o<data_size) { temp_p = (unsigned char *)&CRC_CRC; *temp_p++ = *data_buf; } temp_sum = (unsigned short)(0xFFFF&getReg32(CRC_CRC)); temp_sum1 = (unsigned short)(getReg32(CRC_CRC)>>16); }
if there is some mistake ,please help me,thanks so much。