binary to BCD 8051

meowth08

Member
Hi,

I want to convert an 8-bit binary to 12 bit Binary Coded Decimal using assembly programming in 8051.

I mean: ex. binary:1111 1111 to BCD: 0010 0101 0101
I am having a hard time figuring out how to do this.

Could someone show me the code for this or maybe the technique on how to do this?

Thank you.

Ian Rogers

User Extraordinaire
Forum Supporter
Luckily with the 8051 instructions we have the MUL AB and the DIV AB commands

If you place 255 into the A register and 100 in the B register

After the DIV AB command A will contain 55 and B will contain 2

save them in R0 & R1 then load A and B with 55 and 10 DIV AB again and save

Code:
        clr A
MOV R0, A
MOV R1, A	; clear reg's'
mov A, #0FFh	; 255
mov B, #64h	; 100
DIV AB
mov R0, A	; store A in R0 ( 2)
mov A, B		; get remainder (55)
mov B, #0AH
DIV AB		; / 10
mov R2, A	; save tens elsewhere ( 5)
mov A, B		; get units
mov R1,A		; save in R1
mov A, R2	; retreive tens
RR A
RR A
RR A
RR A		; shift into high byte
orl A, R1	; or with units
mov R1, A	; store in R1

Last edited:

meowth08

Member
Hi,

Thanks so much for the code.
After posting this, I did google bcd to binary.
Luckily I found a hint too so I just worked backwards.

Actually, it is just a part of our homework's program requirement.
We were tasked to output the decimal equivalent of the 8-bit binary in three seven segment displays.
Included is an ENABLE bit that when it goes from logic 1 to 0, the numbers on the seven segment displays are updated.

question:
I used P0.2, P0.1, and P0.0 as the output for each SSD.
I used P0.3 for the enable bit. (input)
Will it be a problem?

Last edited:

dougy83

Well-Known Member
question:
I used P0.2, P0.1, and P0.0 as the output for each SSD.
I used P0.3 for the enable bit. (input)
Will it be a problem?
You'll need more than 3 pins to control the SSD; 3 pins (assuming a decoder IC is used) will allow for values from 0..7, but not 8 & 9. A 4th pin will allow the full range for each digit.

meowth08

Member
You'll need more than 3 pins to control the SSD; 3 pins (assuming a decoder IC is used) will allow for values from 0..7, but not 8 & 9. A 4th pin will allow the full range for each digit.
Hi,

Sorry I did not make it clear.
P0.2, P0.1, P0.0 controls the common pins of the SSD which in this case are all common anode.
8-bits of P2 is used to control the segments of the SSD including the dot.
I used P0.3 as the enable bit.

My question:
Is it okay to use the same port as input and output?

m8