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 support@ccsinfo.com

CRC
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








PostPosted: Wed Nov 02, 2005 10:16 am     Reply with quote

thank you so much! If I'd noticed your 17 cycle versin, I wouldnt have bothered with the crc-8 anyway!

Even though we are slightly pressed with regard to cycles, if we cant afford that, we need to move up on the "cpu" ladder. Smile

Superb!
Guest








PostPosted: Wed Nov 02, 2005 10:17 am     Reply with quote

eh? this forum has some "odd personality quirks" This was a reply in a differnt thread.... please close/delete this post!!
jonowoodhouse



Joined: 12 Feb 2007
Posts: 2
Location: Cape Town

View user's profile Send private message Visit poster's website

crc8 - assembler and c crc algorithm
PostPosted: Mon Feb 12, 2007 11:59 am     Reply with quote

Hi

Firstly, to Dirk & the others - thanks very much for this code. It's been really useful. Smile

Below please find a variation that will run for both the PIC16 (PCM) and PIC18 (PCH) PIC chips, and allows you to pass in the initial CRC value. (Make this zero if you don't want to build on a previously calculated value).

[Also, you may not need the first section (which sets up some #defines and #locates) if you have already done this, or are including in some inc file.]

Code:

//-----------------------------------------------------------------------------
// Calculate crc8 checksum
// This is the checksum as used in the Dallas One Wire protocol.
// Advantage of this checksum over other crc's is that because of a pecularity
// of this specific polynomial it allows bytewise processing without bit
// shifting.  It's very fast, small and uses no RAM.
//
// Based on:        Maxim Application Note 27,
//                  http://www.maxim-ic.com/appnotes.cfm/appnote_number/542
// Original C code: T. Scott Dattalo
//                  http://www.dattalo.com/technical/software/pic/crc_8bit.c
// Optimized by:    Dirk Van den Berge (dvdb)
//                  donated to the CCS-forum 12-jan-2004
//                  http://www.ccsinfo.com/forum/viewtopic.php?t=17170
// Minor tweaks by: Jono Woodhouse (jonowoodhouse) and ckielstra
//                  http://www.ccsinfo.com/forum/viewtopic.php?t=17170
//                  http://www.ccsinfo.com/forum/viewtopic.php?t=26264
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Setup FSR and INDF
#if defined(__PCM__)                     // For example (and tested on) PIC16F74, PIC16F887
   #locate FSR = 0x0004
   #locate INDF = 0x0000
#elif defined(__PCH__)                  // For example (and tested on) PIC18F4520
   #define FSR0 0x0000
   #define INDF0 0x0FEF
   #define POSTINC0  0x0FEE
   #locate FSR0L = 0x0FE9
   #locate FSR0H = 0x0FEA
   #locate STATUS = 0x0FD8
#else
   #Error You need to setup the above for your PIC Chip
#endif


//-----------------------------------------------------------------------------
// This is the crc8 function for use on the PIC16 and PIC18 etc. microprocessors
unsigned int8 crc8_pic(unsigned int8* p_data, unsigned int8 p_datalength, unsigned int8 crc) {
   #ASM
      #if defined(__PCM__)
         movf   p_data, W               // copy pointer to w reg..
         movwf  FSR                     // ..to pointer register
      #elif defined(__PCH__)
         movff   p_data, FSR0L         // copy low byte of pointer to low byte of pointer register 0..
         movff   &p_data+1, FSR0H      // copy high byte of pointer to high byte of pointer register 0..
      #else
         #Error You need to setup the above for your PIC Chip
      #endif
      loop:
         #if defined(__PCM__)
            movf   INDF, W            // load w with next databyte
            incf FSR                  // do a 8-bit increment - increment indirection register to point to next databyte - for the next time
         #elif defined(__PCH__)
            movf   POSTINC0, W      // load w with next databyte and increment indirection register to point to next databyte - for the next time
         #else
            #Error You need to setup the above for your PIC Chip
         #endif
         xorwf crc, F               // xor with accumulated crc (accumulated crc is no longer valid now)
         movlw 0                     // w will accumulate the new crc
         btfsc crc, 0
            xorlw 0x5e               // could also be iorlw
         btfsc crc, 1
            xorlw 0xbc
         btfsc crc, 2
            xorlw 0x61
         btfsc crc, 3
            xorlw 0xc2
         btfsc crc, 4
            xorlw 0x9d
         btfsc crc, 5
            xorlw 0x23
         btfsc crc, 6
            xorlw 0x46
         btfsc crc, 7
            xorlw 0x8c
         movwf crc                  // store accumulated crc
         decfsz p_datalength, F
      goto loop                     // next databyte
      movwf crc                     // done, crc is in w - now store it in result (crc)
   #ENDASM
   return(crc);
}

//-----------------------------------------------------------------------------


I've tested this on a PIC16F74, PIC16F884, PIC16F887 & PIC18F4520.

Cheers
Jono Woodhouse Cool
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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