Matt Rhys-Roberts wrote: > Being a typically lazy engineer, I'm hoping that somebody has already > invented this wheel for PIC use...? > > Prefer to use assembler for reasons of familiarity, tweakability and speed. The code below was written for PIC18 using Olin's application framework. It is tested production code from a real application. -- Dave Tweed ;*********************************************************************** ; ; The following three subroutines implement a standard 32-bit CRC, ; as commonly used in desktop applications (e.g., ZIP files) and ; communication systems (e.g., Ethernet). The polynomial, expressed ; as a 32-bit integer, is 0xEDB88320. See the following reference ; for background information and alternate implementations. ; ; http://www.dattalo.com/technical/software/pic/crc.php ; ; To use, call cmd_crc_init() once, then call cmd_crc_byte() for ; each byte in the stream to be checked, then call cmd_crc_final() ; once to form the final CRC32 value, which can then be compared ; with the externally-provided value. ; ; Subroutine CMD_CRC_INIT - Initialize the CRC32 checker ; locsub cmd_crc_init, noregs movlw h'FF' dbankif lbankadr movwf crc+0 movwf crc+1 movwf crc+2 movwf crc+3 leaverest ;*********************************************************************** ; ; Subroutine CMD_CRC_BYTE - Process one byte through the CRC32 checker ; ; Note that this implementation uses 20 instructions in the per-byte ; processing, and takes anywhere from 76 to 140 cycles per byte to ; execute. This compares well with Scott Dattalo's implementations, ; particularly with respect to code size, which is the key parameter ; in this application. ; ; Input: ; REG0 - byte value to be processed locsub cmd_crc_byte, regf1 movf reg0, w dbankif lbankadr xorwf crc+3, f movlw 8 movwf reg1 cmd_crc_byte_1 unbank bcf status, c dbankif lbankadr rrcf crc+0, f rrcf crc+1, f rrcf crc+2, f rrcf crc+3, f bnc cmd_crc_byte_2 movlw h'ED' xorwf crc+0, f movlw h'B8' xorwf crc+1, f movlw h'83' xorwf crc+2, f movlw h'20' xorwf crc+3, f cmd_crc_byte_2 unbank decfsz reg1 bra cmd_crc_byte_1 leaverest ;*********************************************************************** ; ; Subroutine CMD_CRC_FINAL - Finalize the CRC32 calculation ; locsub cmd_crc_final, noregs dbankif lbankadr comf crc+0, f comf crc+1, f comf crc+2, f comf crc+3, f leaverest ;*********************************************************************** -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist