piclist 2002\12\24\164925a >
Thread: 24Bit and 32 Bit Binary to ASCII conversion with DAW instruction, How?
www.piclist.com/techref/microchip/devices.htm?key=pic
BY : Bob Ammerman email (remove spam text)

> Hello,
>
> Has anyone come up with a relatively fast integer 24bit or 32bit signed
> conversion to ASCII form using the 18xxx series pics DAW instruction.
>
> Regards,
>
> James

!!warning: untested code!!

Here is a simple macro that will convert a binary value to BCD. It will work
for any size numbers. This is, I am sure, far from the most efficient.

; BIN2BCD - Convert binary value 'bin' which is 'binsize' bytes long into
;     a packed bcd value 'bcd' which is 'bcdsize' bytes long. 'temp' is a
;     temporary location used by the macro.
;
;     Thus, 'bin' is a 'binsize' * 8 bit binary value.
;     and 'bcd' is a 'bcdsize' * 2 digit packed decimal result
;
;      'binsize' and 'bcdsize' have to be constant values.
;
;      'bin' is destroyed
;
;    Values in [] are instruction counts executed

BIN2BCD MACRO bin,binsize,bcd,bcdsize,temp
local looper
variable ptr

; clear the result area
; [bcdsize]

ptr = 0
while (ptr < bcdsize)
clrf    bcd+ptr
ptr += 1
endw

; set up to loop thru all the bits in the binary value
; [2]

movlw    binsize*8    ; # of bits in binary value
movwf    temp

looper:

; multiply the binary value by two. note that we don't care
; [binsize*8*binsize]

ptr = 0
while (ptr < binsize)
rlf    bin+ptr,f
ptr += 1
endw

; multiply the bcd value by two, in bcd. bring in the carry
; from shifting the binary value
; [binsize*8*bcdsize*4]

ptr = 0
while (ptr < bcdsize)
movf    bcd+ptr,w
daw
movwf    bcd+ptr
ptr += 1
endw

; check for more bits
; [binsize*8*3-1]

decfsz    temp,f
bra        looper

endm

***** NOTE:

both the bcd and binary value are assumed to be stored in little-endian
order.

***** USAGE SAMPLE

bcdbytes = 5
binbytes = 4
CBLOCK X'??'
bcdval:bcdbytes
binvar:binbytes
mytemp
ENDC

BIN2BCD binval,binbytes,bcdval,bcdbytes,mytemp

***** SOME ANALYSIS

Size of code = 5*bcdsize + binsize + 4
Instructions executed =

[bcdsize]
+ [2]
+ [binsize*8*binsize]
+ [binsize*8*bcdsize*4]
; [binsize*8*3-1]

Simplified this comes to

8 * binsize ^ 2
+ 32 * binsize * bcdsize
+ 24 * binsize
+ 1 * bcdsize
+ 1

IMPORTANT NOTE: This routine is isochronous. In other words it takes the
exact same number of cycles no matter what its input is.

***** EXAMPLE 1:

binsize = 4 (32 bit input)
bcdsize = 5 (10 digit output)

5*5 + 4 + 4
= 25 + 8
=  33 words of code

8*16 + 32*4*5 + 24*4 + 5 + 1
= 128 + 640 + 96 + 6
= 870 instructions executed

At 40MHz (10 MIPS) this comes to 87 microseconds.

***** EXAMPLE 2:

binsize = 1 (8 bit input)
bcdsize = 2 (4 digit output)

5*2 + 1 + 4
= 15 words of code

8*1 + 32*1*2 + 24*1 + 2 + 1
= 8 + 64 + 24 + 3
= 99 instructions

At 40MHz (10 MIPS) this comes to 9.9 microseconds.

EXAMPLE 2:

binsize = 2 (16 bit input)
bcdsize = 2 (4 digit output)

Note: since the output is only 4 digits this variation will only work up to
9999.

5*2 + 2 + 4
= 16 words of code

8*4 + 32*2*2 + 24*2 + 2 + 1
= 32 + 128 + 48 + 2 + 1
= 211 instructions

At 40MHz (10 MIPS) this comes to 21.1 microseconds

Bob Ammerman
RAm Systems

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics
<00d101c2ab96\$21496f20\$5800a8c0@DELL8100> 7bit