CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

CCITT (Kermit) 16bit CRC

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
PrinceNai



Joined: 31 Oct 2016
Posts: 478
Location: Montenegro

View user's profile Send private message

CCITT (Kermit) 16bit CRC
PostPosted: Fri Sep 23, 2022 7:12 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group