convert a binbcd sub from 16f to 18f

Status
Not open for further replies.

jkmadsci

New Member
I seem to be stumped on how to convert this code to work for the pic18f series.
i guess i am not familiar with the fsr registers in the 18cf
any help or pointers would be appreciated.



BINBCD bcf STATUS,0 ; clear the carry bit
movlw .16 ;HBYTE,LOWBYTE INTO BCD R0,R1,R2 R0 = HIGH BYTE
movwf count
clrf R0
clrf R1
clrf R2
ACCBLOop16 rlf L_byte, 1
rlf H_byte, 1
rlf R2, 1 ;should be rlcf for 18f
rlf R1, 1
rlf R0, 1
decfsz count, 1
goto adjDEC
RETLW 0
adjDEC movlw R2
movwf FSR
call adjBCD
movlw R1
movwf FSR
call adjBCD
movlw R0
movwf FSR
call adjBCD
goto ACCBLOop16
adjBCD movlw 3
addwf 0,W
movwf temp
btfsc temp,3 ; test if result > 7
movwf 0
movlw 30
addwf 0,W
movwf temp
btfsc temp,7 ; test if result > 7
movwf 0 ; save as MSD
RETLW 0
 
I seem to be stumped on how to convert this code to work for the pic18f series.
Please use Code tags when posting code. Just click the # icon above the posting window just before pasting your source. Alternately you can type [code] before your source and [/code] after it. This preserves the formatting, making your source readable, instead of the mess above.

I noticed you did this line correctly, using w to store the result in W
Code:
	addwf	0,w
but on these lines
Code:
	rlf	L_byte,f
	rlf	H_byte,f
	rlf	R2,f		;should be rlcf for 18f
	rlf	R1,f
	rlf	R0,f
you were using 1 instead of f. Use f to store the result back in the register. It just makes what you're doing clearer.

BTW: The above has nothing to do with why your code won't work.

With code tags your code would look like this:
Code:
BINBCD	bcf	STATUS,0	;clear the carry bit
	movlw	.16		;HBYTE,LOWBYTE INTO BCD R0,R1,R2 R0 = HIGH BYTE
	movwf	count
	clrf	R0
	clrf	R1
	clrf	R2
ACCBLOop16
	rlf	L_byte,f
	rlf	H_byte,f
	rlf	R2,f		;should be rlcf for 18f
	rlf	R1,f
	rlf	R0,f
	decfsz	count,f
	goto	adjDEC
	retlw	0
adjDEC	movlw	R2
	movwf	FSR
	call	adjBCD
	movlw	R1
	movwf	FSR
	call	adjBCD
	movlw	R0
	movwf	FSR
	call	adjBCD
	goto	ACCBLOop16
adjBCD	movlw	3
	addwf	0,w
	movwf	temp
	btfsc	temp,3		;test if result > 7
	movwf	0
	movlw	30
	addwf	0,w
	movwf	temp
	btfsc	temp,7		;test if result > 7
	movwf	0		;save as MSD
	retlw	0
 
i guess i am not familiar with the fsr registers in the 18f
You load the FSR reg with the address you're pointing to. You access that address indirectly by using the INDF pseudo-register.

For instance, to iterate through a series of byte variables, you'd load FSR with the address of the first variable (R2)
Code:
	movlw	R2
	movwf	FSR
and then access that byte by using INDF.
Code:
	movf	INDF,w
In the above line we load the contents of R2 into W.

To move to the next byte, increment FSR. Now INDF is pointing to the next byte. And so on...
 
Last edited:
That 16F' code doesn't look quite right -- no access to INDF...

Anyway, for 18F' devices you might want to consider something that takes advantage of the 18F' instruction set (much faster)...

Code:
;******************************************************************
;
;  Bin2Bcd -- 24 bit binary to 8 digit packed BCD (16 bit core)
;
;  Number Range: 000000..FFFFFF (0..16,777,215)
;
;  28 words, 537 cycles (including call and return)
;
Bin2Bcd
        clrf    BCDL+0          ; LSB..MSB
        clrf    BCDL+1          ;
        clrf    BCDL+2          ;
        clrf    BCDL+3          ;
        movlw   24              ;
        movwf   BitCtr          ;
ConvertBit
        rlcf    BINL+0,F        ; LSB..MSB
        rlcf    BINL+1,F        ;
        rlcf    BINL+2,F        ;
        movf    BCDL+0,W        ;
        addwfc  BCDL+0,W        ;
        daw                     ;
        movwf   BCDL+0          ;
        movf    BCDL+1,W        ;
        addwfc  BCDL+1,W        ;
        daw                     ;
        movwf   BCDL+1          ;
        movf    BCDL+2,W        ;
        addwfc  BCDL+2,W        ;
        daw                     ;
        movwf   BCDL+2          ;
        movf    BCDL+3,W        ;
        addwfc  BCDL+3,W        ;
        daw                     ;
        movwf   BCDL+3          ;
        decfsz  BitCtr,F        ;
        bra     ConvertBit      ;
        return                  ;
 
Mike,

I wanted to thank you for posting this code for 18F devices. I've been looking and trying different code from many sites without any success. What you posted allowed me to be able to write a 24-bit value to an LCD screen. I'm in the process of writing code for a frequency counter for home use. Thanks again for the code you posted years ago. I appreciate it!

Jared, KB0IAA
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…