Does anyone have an assembler 16bit binary to bcd converter, or a link in the right direction? I know how to do 8 bits, but the 16 is throwing me for a loop.

Jim

Author

Message

Does anyone have an assembler 16bit binary to bcd converter, or a link in the right direction? I know how to do 8 bits, but the 16 is throwing me for a loop.

Jim

http://www.avr-asm-tutorial.net/...

Go to

Quote:

From binary to BCD

; Bin2ToBcd5

; ==========

; converts a 16-bit-binary to a 5-digit-BCD

Many thanks!!

I never heard of this site. Looks kinda cool

Jim

There is a more efficient method "shift-plus-3".

I have written the sample code as below. The input hex value

put in R17:R16, and this function will return R20:R19:R18

with the BCD value.

; r17:r16 = HEX value ; r20:r19:r18 = BCD value hexToBcd: push r16 push r17 push r21 push r22 push xl push xh clr r18 clr r19 clr r20 clr xh ldi r21, 16 hexToBcd1: ldi xl, 20 + 1 hexToBcd2: ld r22, -x subi r22, -3 sbrc r22, 3 st x, r22 ld r22, x subi r22, -0x30 sbrc r22, 7 st x, r22 cpi xl, 18 brne hexToBcd2 lsl r16 rol r17 rol r18 rol r19 rol r20 dec r21 brne hexToBcd1 pop xh pop xl pop r22 pop r21 pop r17 pop r16 ret

Great stuff!!

I have used the shift-plus 3 method in CPLD's. I'll keep this in mind for future micro jobs

Jim