Ok, here is the complete file: -------------CRC16.ASM---------------------- ;******************************************************************** ; ; Compiled with MAPLAB 4.12.12 ; ;******************************************************************** LIST p=16C54 ; PIC16C54 is the target processor crc_hi equ 00EH crc_lo equ 00FH STATUS equ 3 ; STATUS register F3 CARRY equ 0 ; Carry bit in status register ;******************************************************************** ; CRC Test Program ;******************************************************************** org 0h start clrf crc_hi clrf crc_lo movlw 080H ; crc_hi:crc_lo = 00 00 call CRC16 movlw 075H ; crc_hi:crc_lo = A0 01 call CRC16 movlw 08AH ; crc_hi:crc_lo = 27 A0 call CRC16 movlw 00BH ; crc_hi:crc_lo = DF A6 call CRC16 movlw 075H ; crc_hi:crc_lo = BD 1E call CRC16 movlw 0C7H ; crc_hi:crc_lo = EF FC call CRC16 movlw 0AAH ; crc_hi:crc_lo = D3 AE call CRC16 movlw 075H ; crc_hi:crc_lo = C3 D2 call CRC16 movlw 0C7H ; crc_hi:crc_lo = BA 82 call CRC16 movlw 055H ; crc_hi:crc_lo = F3 7B call CRC16 movlw 043H ; crc_hi:crc_lo = 1C 73 call CRC16 movlw 01CH ; crc_hi:crc_lo = 14 1c call CRC16 movlw 014H ; crc_hi:crc_lo = 00 14 call CRC16 goto start ; crc_hi:crc_lo = 00 00 ;******************************************************************** ; ; CRC-16 (x^16+x^15+x^2+x^0) ; No tables, No loops, No temporary registers used. ; ; Input: W = Data byte for CRC calculation ; crc_hi:crc_lo 16 bit CRC register ; ; Output: crc_hi:crc_lo updated. ; ; Notes: CARRY is trashed. ; DIGIT CARRY is trashed. ; ZERO is trashed. ; W is zero on exit. ; ; 30 instructions, 31 machine cycles per byte. ; ; Copyright (C) February 8, 2000. All Right Reserved. ; Charles Ader, PO Box 940 Pleasanton, California, USA. ; ; This code started out as an example program found in ; Dallas Semiconductor Application Note 27: ; ; Understanding and Using Cyclic Redundancy Checks ; with Dallas Semiconductor iButton(TM) Products. ; ; The application note shows an 8051 assembly language ; routine that calculates the same CRC as the hardware ; in the DS5001/2 secure micro. ; ;******************************************************************** CRC16 xorwf crc_lo,W ;W = input XOR old crc_lo xorwf crc_hi,W ;Swap old crc_hi with W xorwf crc_hi,F ; xorwf crc_hi,W ;new crc_hi = input XOR old crc_lo movwf crc_lo ;new crc_lo = old crc_hi ; ; Calculate parity of crc_hi, (input XOR old crc_lo), ; an place the result in carry. ; ; Save crc_hi in W and use crc_hi as a temp ; location so we can test bits. ; ; Note: We use INCF STATUS,F to compliment ; carry. This is safe because the XORWF ; will clear the zero flag if any of the ; INCF instructions are going to execute. ; ; The INCF can be executed a maximum of ; four times after the XORWF before the ; bits to the left of zero in status will ; be affected. ; ; We use INCF at most three time here. ; movf crc_hi,W ;Save crc_hi in W swapf crc_hi,F ;Trade nibbles xorwf crc_hi,F ;XOR high half byte with low rrf crc_hi,F ;Initialize Carry btfsc crc_hi,0 incf STATUS,F ;Compliment carry btfsc crc_hi,1 incf STATUS,F ;Compliment carry btfsc crc_hi,2 incf STATUS,F ;Compliment carry movwf crc_hi ;Restore crc_hi from W ; ; Use the parity of crc_hi, (input XOR crc_lo), ; to complete the CRC calculation. ; movlw 001H btfsc STATUS,CARRY ; If carry xorwf crc_lo,F ; flip bit 0 of crc_lo movlw 040H rrf crc_hi,F ; shift parity into crc_hi btfsc STATUS,CARRY ; if shift out is one xorwf crc_lo,F ; flip bit 6 of crc_lo rlf crc_hi,W ; unshift crc_hi into W xorwf crc_hi,F ; combine them rrf crc_hi,F ; shift parity back into crc_hi movlw 080H btfsc STATUS,CARRY ; if shift out is one xorwf crc_lo,F ; flip bit 7 of crc_lo retlw 0 ;******************************************************************** ; Power on reset ;******************************************************************** org 01FFH goto start end -------------End CRC16.ASM------------------ On Thu, 10 Feb 2000, piclist.com wrote: > Um, look maybe we need an FAQ entry on this but.... > > WE ALWAYS WANT THE CODE. > > Sorry to shout but asking the PICListers if they want to see code is > like asking an addict if he wants to shoot up. > > ; What you need is to define the polynomial. E. g. for the CCITT one: > > poly0 equ 0x21 > poly1 equ 0x10 > > ; Do not forget to initialize = before the 1st call > ; e. g. with 0xFF or with zeroes, as the particular CRC algorithm > ; requires. > > ;This subroutine calculates a 16-bit CRC > ;-------- > ; CrcUpd: update using > ; > ; > CBLOCK crcblk > crch > crcl > saved > oldcch > i > ENDC > CrcUpd movwf saved ; j = W > movlw d'8' ; W = 8 > movwf i ; i = W > _loop movfw crch > movwf oldcch ; temporary save > clrc ; clear carry for rlf > rlf crcl ; crc << 1 > rlf crch > movfw saved ; the char read > xorwf oldcch,F ; test with old high > btfss oldcch,7 ; if bit set, apply mask > b _notset ; otherwise skip > movlw poly0 > xorwf crcl,F > movlw poly1 > xorwf crch,F > _notset clrc ; for rlf > rlf saved,F ; next bit of saved > decfsz i,F > b _loop > return > ; > ; End CrcUpd > > More at: > http://www.piclist.com/faq > > James Newton > mailto:jamesnewton@geocities.com > 1-619-652-0593 phone > http://www.piclist.com > > ----- Original Message ----- > From: Charles Ader > To: > Sent: Thursday, February 10, 2000 21:03 > Subject: CRC-16 for the PIC16C5X > > > > I was searching through the PICLIST archive for a CRC-16 > > routine for a project but found nothing useful. The only > > thing I found was a Dallas app note (#27) that uses a > > linear method, no table lookup and no loops. > > > > This translated well to PIC code. If there is still > > an interest I'll post it to the list. > > > > Here is a summary: > > > > ; CRC-16 (x^16+x^15+x^2+x^0) > > ; No tables, No loops, No temporary registers used. > > ; > > ; Input: W = Data byte for CRC calculation > > ; crc_hi:crc_lo 16 bit CRC register > > ; > > ; Output: crc_hi:crc_lo updated. > > ; > > ; 30 instructions, 31 machine cycles per byte. > > > > Charles. > > >