Also:
See also:
Archive:
Comments:
Questions:
Convert the Hex digits to binary (16bits) and then convert the binary to ASCII.
Apply one of the "2 digit Hex to Binary 8 bits" routines to the first two hex digits and then again to the second, thereby producing two 8 bit values that are then passed to any one of the "16bit to ASCII Decimal 5 digits" routines.
Interested:
See:
;some code snippets that someone might find useful... ;This routine will return the number of decimal ;hundreds from an 8-bit binary ;Input: w ;Output: w ;RAM:2 ;Cycles: 12-24 GETHNDS movwf t1 clrf w2 gethnds_loop movlw .100 incf w2,f subwf t1,f btfsc STATUS,C goto gethnds_loop decf w2,w return ;--- ;This routine will return the number of decimal ;tens from an 8-bit binary ;Loop based, so it might take some time... ;It needs getones too GETTENS movwf t1 clrf w2 gettens_loop movlw .10 incf w2,f subwf t1,f btfsc STATUS,C goto gettens_loop decf w2,w goto getones ;--- ;This routine will return the number of decimal ;ones from an 8-bit binary GETONES movwf w2 movlw .10 deltens_loop subwf w2,f btfsc STATUS,C goto deltens_loop addwf w2,w return
Code:
Compact and fast 10bit to BCD conversion for converting
small / midrange PIC 10bit ADC ADRSH and ADRSL
;****************************************************
;* 10bit BINARY to BCD *
;****************************************************
;
;An implimentation of the 'Shift and add-3' Algorithm
; for small MicroChip 'PIC' microcomputers
;
;ALGORITHM
;
;1)Shift binary number one bit left (into a BCD 'result'
; (initially empty)
;2)If any 4 bit BCD column is greater than 5[hex] add 3[hex]
;3)Goto 1) {until the LSB binary bit is shifted out}
;
;<------MSB LSB----->
;------------- BCD ------------] [------ binary -----
;
; TENS UNITS
; BCD column BCD column MSB bit bit
; 0 0 0 0 0 0 0 0 1 0 1
;
; <------ / <---/ <---/
;Inalisation
;**************
Processor 16F818
include <p16f818.inc>
cblock 0x020 ;Define variable block starting at $020
binH ;bin is the 8bit binary value to be converted
bin ;The 2bit binary MSB's to be converted
bcdH ;Thousands (always blank)/ Hundreds nybbles
bcdL ;Tens / Units nybbles
counter
temp
endc
_bin2bcd movlw d'8'
movwf counter
clrf bcdL
clrf bcdH
;Save time by not shifting in first 6 bits (always '0's)
;--------------------------------------------------------
swapf binH,1 ;Chop off first nybble (TEN THOUSANDS)
rlf binH,1 ;Shift out first 2 MSB's(always '0's)
rlf binH,1
;Ssave more ime by no test and add +3' for first TWO shifts
;---------------------------------------------------------------------
rlf binH,F ;Shifting 'binH' left through carry into BCD nybbles
rlf bcdL,F
rlf binH,F
rlf bcdL,F ;binH (pic ADC register ADRESH) done with!
;Iteration loop shifts the 4 'columns' (10TH, THO, HUN, TEN, UNT) 1 bit left
;Tests each coulmn (4bit nybble) if > 5, adds '3'
;Shifts in the next MSB of the binary for conversion on the right.
;(for remaining EIGHT shifts)
Next_bit movfw bcdL
addlw 0x33 ;Add 3 to both nybbles (T/U) in 'temp'
movwf temp
movfw bcdL
btfsc temp,3 ;Test MSB of Units+3 nybble in 'temp
addlw 0x03 ;Add 3 to U nybble if units+3 MSB = 1
btfsc temp,7 ;Test MSB of Tens+3nybble in 'temp'
addlw 0x30 ;Add 3 to T nybbleiftens+3 MSB=1 (>5)
movwf bcdL
movfw bcdH
addlw 0x33 ;Add 3 to both nybbles (T/H) in temp
mmovfw bcdH
btfsc temp,3 ;Test MSB of Hundreds+3 nybble in temp
addlw 0x03 ;Add 3 to H nybble if units+3 MSB=1
btfsc temp,7 ;Test MSB of Thousand+3 nybble in temp
addlw 0x30 ;Add 3 to TH nybble if tens+3 MSB=1
movwf bcdH
rlf bin,F ;Shift in next MSB from bin into T/UNITS
rlf bcdL,F ;Shift next MSB from TENS/UNITS to TH/H
rlf bcdH,F ;Shift up
decfsz counter,F
goto Next_bit
return
end