Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how to convert hex to bcd?

Status
Not open for further replies.

garg29

New Member
hi friendz
me designing a counter .want to know how to change hexadecimal code into BCD.what is the routine required .kindly help.
thanks
 
what are you using? as you have asked for a routine you should have also mentioned which microprocessor or microcontroller you are using. i remember that the 8085 microprocessor had a DAA (decimal adjust accumulator) instruction for this purpose

the DAA instruction did a number of things to convert the Hex into BCD. if for instance you have an 8 bit number then it first checked whether the 4 LSBs were greater than 9. if it was then it added 06 to the accumulator. then if the 4 MSBs were greater than 9 then it added 60 to the accumulator.
 
Something like this should work, but it would be more efficient to use the uC's/uP's instructions (if you are even using a uC or uP?).

bcd = ((hex / 10) * 16) + (hex mod 10)
 
thanks for replying .i am using 8051 microcontroller.suppose the value which i get is 1F8h (hexadecimal) ,then what will be its BCD format and how to write the code?
 
I think you first need to make clear EXACTLY what you are trying to do, for a start do you really need BCD (a term which is often misused), or just decimal?.

BCD is generally a micro-processor term, and refers to a single byte containing two decimal digits - so it can go from 0 to 99 only. Micro-processors often have special modes which allow you to perform maths on such BCD bytes.

If all you want to do is display the decimal value of your hexadecimal data, you don't need BCD (it would serve no purpose).

You've never mentioned what range of data you want to convert, you need to know this before you can write a suitable routine.

But displaying the decimal value of hexadecimal (or binary) data is a VERY common procedure, I'm sure a quick google would find you plenty of suitable routines ready made - certainly there are plenty of PIC ones available.

Your 1F8H would convert to:

05
04

As BCD (result in two bytes)

Or:

05
00
04

In decimal (result in three bytes).
 
thanks for helping.but i am designing a rpm meter with range 1 to 10000rpm.in this i am running a counter for 1 min with a single hole on the disc.the number of counts gives me the direct value of rpm.for this counting i am using two registers,by incrementing them by cascading.i will be getting a 16 bit value in these two registers.and now i want to display this value on 5, 7-segment dispalys.so for this i need the code of changing 16bit hex value (which is in two 8 bit registers) into bcd.
 
garg29 said:
thanks for helping.but i am designing a rpm meter with range 1 to 10000rpm.in this i am running a counter for 1 min with a single hole on the disc.the number of counts gives me the direct value of rpm.for this counting i am using two registers,by incrementing them by cascading.i will be getting a 16 bit value in these two registers.and now i want to display this value on 5, 7-segment dispalys.so for this i need the code of changing 16bit hex value (which is in two 8 bit registers) into bcd.

As I mentioned above, I doubt you want BCD, more likely you want decimal!.

Have you ever heard of Google?, or other search engines?.

A simple google for '+8051 +binary +decimal' produces loads of helpful looking links.

You might have a look at which could help you?.
 
Russlk said:
It seems that he plans to use a BCD to 7-segment decoder (like 7447), so he needs to convert to BCD.

He's never suggested that, and as he's using a micro-controller why on earth would you want to use an extra chip for no reason?.
 
I'm sure it's not your fault, but a lot of spurious terms are being tossed about.

Your input data isn't hexadecimal, it's binary in the registers. Hex is the human-readable form here.

BCD is one decimal DISPLAY digit per byte, typically F0-F9 for digits 0 through 9.

Packed decimal is two decimal digits per byte, one per nybble.

Since I don't know precisely what format you'll want your data in, I'll give you a general algorithm which will put values 0 through 9 in a series of bytes. You can then OR in an additional left nybble if you think you need it.

DATA DEFINITIONS

digit3 … byte
digit2 … byte
digit1 … byte
digit0… byte

input … word
work … word

ALGORITHM

work = input;

digit0 = mod(work,10)
work = work / 10

digit1 = mod(work,10)
work = work / 10

digit2 = mod(work,10)
work = work / 10

digit3 = mod(work,10)
work = work / 10

(… continue if needed)

Now you have 4 digits in digit3, digit2, digit2, digit0 to use them as you will.

In other words, you're peeling digits off the right end, and in doing so shifting the input data to the right.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top