Code:
ADDONES
movf WCOUNT,W
addwf PCL,F
dt 8,4,2,6,8,4,2,1 ; amount of ones for each bit value (backwards)
; 1,2,4,8,16,32,64,128
ADDTENS
movf WCOUNT,W
addwf PCL,F
dt 2,6,3,1,0,0,0,0 ; amount of tens for each bit value (backwards)
BTOBCD
movlw 0x08
movwf WCOUNT ; Move 8 into wcount to limit the amount of times we go round
movlw 0x00
BTOBCDL
btfsc BCDTEMP,0 ; Test bit in BCDTEMP
call ADDONES ; if it is set, go to the addones thing and get the amount of ones
addwf BCDONES,F ; add the returned table value to the bcdones reg
movlw 0x00
btfsc BCDTEMP,0 ; do the same for the tens
call ADDTENS
addwf BCDTENS,F
movlw 0x00
rrf BCDTEMP,1 ; rotate bcdtemp right one, chop off the bit we've done
decfsz WCOUNT,1 ; decrement wcount, so that a different table value is returned
goto BTOBCDL ; if not 0 all bits not done so go back
movf BCDONES,W
movwf TEMP
movlw 0x0A ; subtract 10 from bcdones, if it goes negative don't increment
subwf BCDONES,F
btfss STATUS,C
incf BCDTENS
btfss STATUS,C ;
clrf BCDONES
movlw 0x30 ; add 30h to each register to turn it into ascii
addwf BCDONES,F
movlw 0x30
addwf BCDTENS,F
return
The routine BTOBCD should be called with the byte you want to change in BCDTEMP. The routine is incomplete because I can't get it to respond how I want even removing all the subtract ten from the ones and add to the tens section. If I put the binary for one in BCDTEMP, the sub returns with the ascii for 0 and 7 the first time it's called, then 0 and 8 the second time.