Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC ASM Gurus: Small Routine Help

Status
Not open for further replies.

Mike - K8LH

Well-Known Member
A puzzle to share with members, if you please. The following routine is used in the tail end of a readbit() routine for DS18B20 1-wire temperature sensors. It's basically my implementation of a "zero overhead" bit level CRC method. As you can see, the routine is limited to use in 18F and "enhanced mid-range" 16F devices which provide access to WREG. I'd like an alternative routine that will work for normal 16F devices and I'm looking for ideas. Note that the Carry bit must be preserved.

Thanks for taking a peek.

Cheerful regards, Mike

Code:
     clrf   wreg           ;
     rlf    wreg,W         ; save C (wreg = 0x00 or 0x01)
     xorwf  crc,F          ; xor b0 bits, result in 'crc'
     rrf    crc,F          ; is xor result 0 (C=0)?
     skpnc                 ; yes, skip, else
     iorlw  0x18           ; wreg = x8+x5+x4+1 poly and b0 bit
     rrf    wreg,W         ; restore C (wreg = 0x8C or 0x00)
     xorwf  crc,F          ; apply the polynomial, or not

<added>
Oops! I suppose I could just use a variable to replace WREG. Anyone see a better way to do it?

Code:
     clrf   work           ;
     rlf    work,F         ; save C (work = 0x00 or 0x01)
     movf   work,W         ;
     xorwf  crc,F          ; xor b0 bits, result in 'crc'
     rrf    crc,F          ; put xor result in C
     movlw  0              ;
     skpnc                 ; C=0? yes, skip, else
     movlw  0x8C           ; x8+x5+x4+1 polynomial
     xorwf  crc,F          ; apply the polynomial, or not
     rrf    work,W         ; restore C
 
Last edited:
Sure. The best way is to calculate CRC as you receive bits. Takes just a xor and a shift for every bit. If you do it this way, you do not need to spend any extra time at the end.
 
Sure. The best way is to calculate CRC as you receive bits. Takes just a xor and a shift for every bit. If you do it this way, you do not need to spend any extra time at the end.

Duh! That is what I'm doing! I'm just doing it in the read bit routine while waiting for the end of the 60-usec read/write slot (no overhead)...
 
Sorry. Totally my mistake. :oops:

Instead of

Code:
movlw 0
skpnc
movlw 0x8C
xorwf crc,F

You can do

Code:
movlw  0x8C
skpnc
xorwf crc,F
 
Status
Not open for further replies.
Back
Top