PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
CCITT (Kermit) 16bit CRC |
Posted: Fri Sep 23, 2022 7:12 pm |
|
|
Below is the code to calculate 16bit Kermit flavor of CRC. I found it here and adapted for CCS: https://www.experts-exchange.com/questions/28271830/How-To-Calculate-CRC16-Using-CRC-CCITT-Kermit.html
and checked here: https://crccalc.com/
Function with a test program:
Code: |
#include <18F46K22.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#device ICD=TRUE
#use delay(internal=32000000)
char Buffer[5];
int16 Result;
/*----------------------------------------------------------------------------
* FUNCTION: KERMIT CRC16. Results were checked here: https://crccalc.com/
*---------------------------------------------------------------------------*/
int16 Kermit_CRC_16 (char *pucData, unsigned int8 ucLen) { // ucLen is the number of bytes in the buffer you want to check
//--------------------------------------------------------------------
int8 i;
char ucBit, ucCarry;
//--------------------------------------------------------------------
unsigned int16 usPoly = 0x8408; //reversed 0x1021
unsigned int16 usCRC = 0;
//--------------------------------------------------------------------
for (i = 0; i < ucLen; i++) {
usCRC ^= pucData[i];
for (ucBit = 0; ucBit < 8; ucBit++) {
ucCarry = usCRC & 1;
usCRC >>= 1;
if (ucCarry) {
usCRC ^= usPoly;
}
}
}
return usCRC;
//--------------------------------------------------------------------
}
// *******************************************************************
// *******************************************************************
void main()
{
Buffer[0] = 0x01; // test values
Buffer[1] = 0x02;
Buffer[2] = 0x03;
while(TRUE)
{
delay_cycles(1);
Result = Kermit_CRC_16(Buffer,3);
delay_cycles(1);
}
}
|
It takes 390 instruction cycles for two numbers and 573 for 3. |
|