How can i convert 16 bit number to decimal ?

Status
Not open for further replies.

SphinX

Advanced Member level 3
Joined
Jan 25, 2002
Messages
822
Helped
58
Reputation
116
Reaction score
29
Trophy points
1,308
Location
EGYPT
Activity points
7,045
bin16 2 bcd microchip

Hi,

In my work we need to convert 16 bit number to decimal using ATMEL.

Ex. This number 65535 ----> R0=6 R1=5 R2=5 R3=3 R4=5

I need just an idea only.
I know a method for 8 bit conversion but what about 16 bit number.

Please help me or i will be fired :wink:
Sphinx
 

decimal to 16 bit converter

Look for binary to BCD conversion algorithms, there are a lot of examples online.

By the way: 'ATMEL' does not say a lot about the architercture you are using. They sell MARC4, 8051, AVR and ARM based cores...

Anyway, here is one example in 8051 assembly:

**broken link removed**
 

16 bit number to decimal

Hi Gorilla,

:multi: THANX VERY MUCH. :multi:
We use 8051 mcu.

SphinX
 

Hi SphinX

This is my 8051 version to convert 16 bit number to decimal:

; FUNCTION: BIN2BCD
; BYTES: 27
; CYCLES: 327
; INPUT: R6 (Msb) and R7 (Lsb) (binary_16_bits)
; OUTPUT: R3 (Msb), R4 and R5 (Lsb) (PACKET BCD)
; Destroy: R2, R6, R7, A, and PSW
;
; ALGORITM:
;
; BCD = 0;
; COUNT = 16; // (TO PROCESS 16 BITS)
; do {
; BIN16 = BIN16 * 2
; BCD = BCD * 2 + CARRY
; COUNT = COUNT - 1
; } while (COUNT != 0);
;


BIN2BCD:
CLR A ; BCD = 0
MOV R5,A
MOV R4,A
MOV R3,A

MOV R2,#16 ; TO PROCESS 16 BITS

BIN_10:
MOV A,R7 ; BIN16 = BIN16 * 2
ADD A,R7
MOV R7,A
MOV A,R6
ADDC A,R6 ; CARRY = MSB of BIN16
MOV R6,A

MOV A,R5 ; BCD = BCD * 2 + CARRY
ADDC A,R5
DA A
MOV R5,A

MOV A,R4
ADDC A,R4
DA A
MOV R4,A

MOV A,R3
ADDC A,R3
DA A
MOV R3,A

DJNZ R2,BIN_10
RET


Best regards.
 

Hi SphinX

There were few topics related to hex to bcd conversion . But especially for ATMEL there is Atmel's written high efficiency routines for BCD arithmetics (see application notes for AVR on www.atmel.com ) . Those are written in asm and execute faster than C implementation .
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…