What is algorithm of converting 8 bit binary to 16 bit BCD?
for example:
how can i do this?
1111 1111 (binary) -> 0000 0010 0101 0101
The fastest computational method would be a modification of the "bin2BCD8" routine as described in AVR204. Subtract 100 until the number is less than 100, incrementing a single byte counter (in an even register) each time. Then do the same for 10, with the counter in the register after the hundreds counter. Use SWAP
to move the tens counter to the high nibble, add the units remainder, and return the 16-bit register value.
You can jump to find_hundreds
(after clearing CL and CH) to convert a single 8-bit value stored in FL (AVR 8-bit instruction set).
.DEF FL = r17
.DEF FH = r18
.DEF CL = r19
.DEF CH = r20
; input:
; FH:FL registers holding hex value
; output:
; FH:FL registers holding converted BCD value
hex_to_bcd:
ldi CL, 0
ldi CH, 0
find_thousands:
subi FL, low(1000)
sbci FH, high(1000)
brmi thousands_found ; branch if minus
subi CH, -16
rjmp find_thousands;
thousands_found:
subi FL, low(-1000)
sbci FH, high(-1000)
find_hundreds:
subi FL, low(100)
sbci FH, high(100)
brmi hundreds_found
subi CH, -1
rjmp find_hundreds;
hundreds_found:
subi FL, low(-100)
sbci FH, high(-100)
find_tens:
subi FL, low(10)
sbci FH, high(10)
brmi tens_found
subi CL, -16
rjmp find_tens;
tens_found:
subi FL, low(-10)
sbci FH, high(-10)
find_ones:
subi FL, low(1)
sbci FH, high(1)
brmi ones_found
subi CL, -1
rjmp find_ones;
ones_found:
subi FL, low(-1)
sbci FH, high(-1)
mov FL, CL
mov FH, CH