futz
Active Member
OK, I finished my subroutine. I tested it a bit and it seems to work well. Further testing may find bugs. We shall see...
It converts a 10-bit value (could be easily modified to do 16 bits too) to four ASCII bytes, thou, hund, ten and one, ready for outputting to display.
If you don't want ASCII then just comment out the 'addlw 0x30' lines.
I mooched the 16-bit subtract and addition routines from Chuck McManis' site. The rest of the code is mine.
It converts a 10-bit value (could be easily modified to do 16 bits too) to four ASCII bytes, thou, hund, ten and one, ready for outputting to display.
If you don't want ASCII then just comment out the 'addlw 0x30' lines.
I mooched the 16-bit subtract and addition routines from Chuck McManis' site. The rest of the code is mine.
Code:
cblock 0x00
count,dst2,dst1,src2,src1,thou,hund,ten,one
endc
;***********************************************************
;* 10 bit converter - pass MSB in dst1, LSB in dst2
;* Returns digits as ASCII values in thou, hund, ten and one
;* Requires src2, src1 and dst2, dst1 variables in that
;* little-endian order (LSB first)
fix10b movlw 0x03 ;src = 1000 ($3e8)
movwf src1
movlw 0xe8
movwf src2
thous call sub16 ;subtract 1000 from number
btfsc STATUS,C ;negative result?
goto thouyes ;no, go set thou=1
call add16 ;add back failed subtract
movlw 0x30 ;yes, set thou=0
movwf thou
goto hundred ;and move on to hundreds
thouyes movlw 0x31 ;set thou=1
movwf thou
hundred clrf count ;init counter=0
clrf src1 ;src = 100
movlw 0x64
movwf src2
hund_1 call sub16 ;subtract 100 from number
btfsc STATUS,C ;negative result?
goto h_again ;no, go again
movf count,W ;yes, get count in W
addlw 0x30 ;add $30 to make it ASCII
movwf hund ;and store it in hund variable
call add16 ;add back last subtract
goto tens ;and go do tens
h_again incf count ;increment hundreds count
goto hund_1 ;and go again
tens clrf count ;init counter=0
clrf src1 ;src=10
movlw 0x0a
movwf src2
tens_1 call sub16
btfsc STATUS,C ;negative result?
goto t_again ;no, go again
movf count,W ;yes, get count in W
addlw 0x30 ;add $30 to make it ASCII
movwf ten ;store tens value
call add16 ;put last subtract back
movf dst2,W ;get remainder
addlw 0x30 ;add $30 to make it ASCII
movwf one ;store ones
return
t_again incf count ;increment tens counter
goto tens_1 ;and go again
sub16 movf src2,W
subwf dst2
movf src1,W
btfss STATUS,C
incf src1,W
subwf dst1
return
add16 movf src2,W
addwf dst2
movf src1,W
btfsc STATUS,C
incf src1,W
addwf dst1
return
Last edited: