PCB Heaven

Books => Learning the PIC micro-controller @ PCB Heaven - On line book discussion => Topic started by: fredf on November 12, 2012, 19:18:05 PM

Title: RRF instruction 18F2550
Post by: fredf on November 12, 2012, 19:18:05 PM
Dear Gurus   I don't get consistent div by 2 results using RRCF. or should it be RRNCF?
I attach  the assembler code snippet


The example msb 43 3B 05 lsb  div/2 should give an output of  hex 21 9D 82 in the
temp3, temp2, temp1   registers.
This number does but others with F seem to produce rubbish  div /2 results.

Obviously the code is precarious and I would appreciate  comment on chaining registers to get consistent  div/2 results.

Bye and thanks
Fred

In case the jpeg doesn't get through the code is:-

RRFtest:

    clrf temp4
    clrf temp3
    clrf temp2
    clrf temp1

    movlw 0x43  ; Dummy infoMSB 0x43,0x3B,0x05
        movwf temp3 ; to  try and isolate the problem
    movlw 0x3B
       movwf temp2
       movlw 0x05
       movwf temp1

 
Divby2startshere:

    rrcf   temp3,f,1 
    btfsc STATUS,C
    nop
    nop
   
    rrcf   temp2,f,1 
    btfsc STATUS,C
    nop
    nop

    rrcf   temp1,f,1
   nop
   nop
Title: Re: RRF instruction 18F2550
Post by: kam on November 13, 2012, 08:01:01 AM
So, you have a 3x8bit number temp3:temp2:temp1 and you want to divide this by 2:

bcf carry (make sure the carry is clear)
rrf temp3,f
rrf temp2,f
rrf temp1,f

.end
Title: Re: RRF instruction 18F2550
Post by: _pike on November 15, 2012, 09:23:43 AM
Fist of all this numbers are not integers so i think that numbers won't be the expected.Would be more or less.Second try kam's solution.Also i cant understand how you use the code...

rrcf   temp3,f,1     -----rotate temp3 register right place the result on temp3 reg and the BSR is used to select the GPR bank
btfsc STATUS,C    -----If carry is 0 then jump next instruction...  (why?)What happens if a number has a 0 on it???Stop rotating perhaps?
nop                     -----Do nothing
nop                     -----Do nothing
   
rrcf   temp2,f,1 
btfsc STATUS,C
nop
nop

rrcf   temp1,f,1
nop
nop
Title: Re: RRF instruction 18F2550
Post by: kam on November 15, 2012, 23:08:37 PM
The code:
btfsc STATUS,C    -----If carry is 0 then jump next instruction...  (why?)What happens if a number has a 0 on it???Stop rotating perhaps?
nop                     -----Do nothing
nop                     -----Do nothing

does nothing at all...