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

Calculate 8 bit DOW CRC

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



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

Calculate 8 bit DOW CRC
PostPosted: Thu Jul 02, 2009 9:33 am     Reply with quote

I've never contributed anything and I'm not really good at this but here is something I was searching for and ended up writing myself. It works as shown but could be condensed.

Code:

////////////////////////////////////////////////////////////////////////////////
// C Code to calculate 8 bit DOW CRC on characters in a buffer.
// The chars must be terminated with a <CR> 0x0D which is not
// included in the checksum.
// Test values:  The CRC for characters "A7X2" = 0xC8
// Written and tested on CCS  PIC C Compiler.
// It's not elegent but it works.  Don Ware 7/2/09
////////////////////////////////////////////////////////////////////////////////

int8   bitcnt=0;
int8   cptr=0;
int8   crctmp=0;
int8   crcval=0;
int8   cyflag=0;
int8   tcy=0;
int8   wreg=0;
int8   zbit=0;

char    rxbuff[10];     // terminating char is  <CR>      

rxbuff[0]='A';    rxbuff[1]='7';    rxbuff[2]='X';    rxbuff[3]='2';    rxbuff[4]=13; //  test values
   

//=========================================
void crccalc(){
   
   for(cptr=0;cptr<11;cptr++){   // set for length of buffer but exit when CR found below.
      
      crctmp=rxbuff[cptr];
      if(crctmp==0x0D)   // exit routine if terminating <CR> found.
         return;
   
      wreg=crctmp;
      
      for(bitcnt=0;bitcnt<8;bitcnt++){   // 8 bits
   
         wreg ^= crcval;         // wreg = wreg xor crcval
         rrc();            // rotate right through carry
         wreg=crcval;         
   
         if(cyflag==1)
            wreg ^= 0x18;      //  xor 18H
         
         rrc();      
         
         crcval=wreg;
         wreg=crctmp;
         tcy=cyflag;
         
         rrc();
               
         crctmp=wreg;
         cyflag=tcy;
      }
   }
}
//=========================================

void rrc(){         // rotate right through carry - the hard way
   
   zbit = wreg & 0b00000001;  // copy bit 0 to temp storage

   wreg >>= 1;  // rotate right  (does not include any carry)
                           
   if(cyflag==0)   // copy original carry into bit 7
      wreg=wreg & 0b01111111;  // clear bit 7
   if(cyflag==1)
      wreg=wreg | 0b10000000;  // set bit 7

   cyflag=zbit;  // update cyflag to value shifted from bit 0 of wreg
}
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

PostPosted: Mon Dec 14, 2009 9:12 am     Reply with quote

see also
http://www.ccsinfo.com/forum/viewtopic.php?t=26264
and
http://www.ccsinfo.com/forum/viewtopic.php?t=19520

jds-pic
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