;8 or 16 Bit Binary to 3 or 5 Ascii Routine for 18F4550 24May08
;tested works OK. redone 26/01/2012 OK
list p=18f4550,f=inhX32
#include <p18f4550.inc>
errorlevel -302, -207
CONFIG PLLDIV=5,CPUDIV=OSC1_PLL2,USBDIV=2,FOSC=HSPLL_HS,IESO=OFF
CONFIG VREGEN=ON,CCP2MX=ON,WDT=OFF,WDTPS=32768,PBADEN=OFF ,PWRT=OFF,MCLRE=ON
CONFIG LPT1OSC=OFF,BOR=ON,BORV=2,STVREN=ON,LVP=OFF,ICPRT= OFF,XINST=OFF,DEBUG=OFF
CONFIG WRTB=ON
Xtal SET 4
;AscBfrs for convert subr
AscBfr4 equ 0x060;; not needed for 8bit
AscBfr3 equ 0x061;; not needed for 8bit
AscBfr2 equ 0x062
AscBfr1 equ 0x063
AscBfr0 equ 0x064
BinValL equ 0x068
BinValM equ 0x069; not needed for 8bit
No_Bits equ 0x06B
Digit_Cnt equ 0x06C
;-------------------
org 0x0000
Main:
movlw .123;39h ;just for testing preload value
movwf BinValL
;;movlw 0;30h
;;movwf BinValM
call Bin2Asc
done:
goto done; Main
;Binary to Ascii conversion Subr
;enter with BinValH and BinValL holding the [8] 16bit binary value to be converted.
;exit with [3] 5 Ascii value,,,, the BinValH/L is all '0' on exit
;NOTE: to expand to a 16bit WORD and 5 ASCII character output, remove the ';;' before the
;intructions....
Bin2Asc:
;;clrf AscBfr4 ;clear buffers
;;clrf AscBfr3
clrf AscBfr2
clrf AscBfr1
clrf AscBfr0
movlw .8;;.16
movwf No_Bits
BitLoop:
rlcf BinValL,F
;;rlcf BinValM,F ;carry = msb < 1
LFSR FSR0,AscBfr0 ;lsd 1st
movwf FSR0 ;pointer to ascbfr
movlw 0X03;;05 ;digits to do
movwf Digit_Cnt
AdjLoop:
rlcf INDF0,F ;shift ASCII byte in [INDF0] 1 bit left
movlw 0X0A ;decimal 10
subwf INDF0,W ;W=[INDF0]-10
btfsc STATUS,C ;skip if no carry, its a negative result
movwf INDF0 ;only save if postive result
movf STATUS,W ;NB. the decf FSR0L sets Carry!
decf FSR0L,F ;point to next msd Ascbfr
movwf STATUS ;recall Status from before the decf FSR0L
decfsz Digit_Cnt,F ;dec AscBfr counter
goto AdjLoop ;go next ascii
decfsz No_Bits,F
goto BitLoop ;go next bit shift left for BinVal
movlw 0X30 ;make asci
;;iorwf AscBfr4,F
;;iorwf AscBfr3,F
iorwf AscBfr2,F
iorwf AscBfr1,F
iorwf AscBfr0,F
return
end