;*************************************
; Program : 16 Bit Bin to 4 Digit Dec
; Date : September 6th, 2009
;*************************************
List P=16F628a
#include "P16F628a.INC"
__CONFIG _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF
;*** Cblock ***
CBLOCK 0x20
Ones ;
Tens ;
Hund ;
Thou ;
TenK ;
Hi
Lo
ENDC
;*** START OF RAM ***
ORG 0x000 ; Start of program vector
GOTO Start ;
ORG 0x004 ; Interrupt vector
;*** Configuration ***
Start:
MOVLW 0x07 ; Turn comparators off and enable pins for I/O
MOVWF CMCON ; ^
CLRF PORTA ; PortA all low
CLRF PORTB ; PortB all low
BSF STATUS,RP0 ; Bank 1
MOVLW b'00100000' ; Bit 5 input
MOVWF TRISA ; PortA all output except MCLR
CLRF TRISB ; PortB all output
BCF STATUS,RP0 ; Bank 0
;*** Main Code ***
Main:
MOVLW b'00001010'
MOVWF NumL ; 6666
MOVLW b'00011010'
MOVWF NumH
CALL Convert
GOTO Main
;*** Bin to BCD routine ***
Convert: ; Takes number in NumH:NumL
;{
swapf Hi,w ; Returns decimal in
iorlw b'11110000' ; TenK:Thou:Hund:Tens:Ones
movwf Thou
addwf Thou,f
addlw 0xe2
movwf Hund
addlw 0x32
movwf Ones
movf Hi,w
andlw 0x0f
addwf Hund,f
addwf Hund,f
addwf Ones,f
addlw 0xe9
movwf Tens
addwf Tens,f
addwf Tens,f
swapf Lo,w
andlw 0x0f
addwf Tens,f
addwf Ones,f
rlf Tens,f
rlf Ones,f
comf Ones,f
rlf Ones,f
movf Lo,w
andlw 0x0f
addwf Ones,f
rlf Thou,f
movlw 0x07
movwf TenK
movlw 0x0A ; Ten
Lb1: ; At this point, the original number is
addwf Ones,f ; equal to
decf Tens,f ; TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
btfss 3,0 ; if those entities are regarded as two's
goto Lb1 ; complement binary. To be precise, all of
Lb2: ; them are negative except TenK. Now the number
addwf Tens,f ; needs to be normalized, but this can all be
decf Hund,f ; done with simple byte arithmetic.
btfss 3,0
goto Lb2
Lb3:
addwf Hund,f
decf Thou,f
btfss 3,0
goto Lb3
Lb4:
addwf Thou,f
decf TenK,f
btfss 3,0
goto Lb4
return
;}
END
; That's all folks!