1. 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.
    Dismiss Notice

how to convert hex to bcd?

Discussion in 'Electronic Projects Design/Ideas/Reviews' started by garg29, Feb 4, 2005.

  1. garg29

    garg29 New Member

    Joined:
    Sep 30, 2004
    Messages:
    49
    Likes:
    0
    Location:
    India
    hi friendz
    me designing a counter .want to know how to change hexadecimal code into BCD.what is the routine required .kindly help.
    thanks
     
  2. samcheetah

    samcheetah New Member

    Joined:
    Dec 30, 2003
    Messages:
    872
    Likes:
    0
    Location:
    Pakistan
    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.
     
  3. pandaemonium

    pandaemonium Member

    Joined:
    May 2, 2004
    Messages:
    65
    Likes:
    0
    Location:
    Canada
    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)
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. garg29

    garg29 New Member

    Joined:
    Sep 30, 2004
    Messages:
    49
    Likes:
    0
    Location:
    India

    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?
     
  6. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,331
    Likes:
    653
    Location:
    Derbyshire, UK
    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).
     
  7. garg29

    garg29 New Member

    Joined:
    Sep 30, 2004
    Messages:
    49
    Likes:
    0
    Location:
    India
    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.
     
  8. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,331
    Likes:
    653
    Location:
    Derbyshire, UK
    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 http://www.hkrmicro.com/course/lesson08.html which could help you?.
     
  9. Russlk

    Russlk New Member

    Joined:
    Nov 18, 2003
    Messages:
    2,454
    Likes:
    2
    Location:
    New Hampsire, USA
    It seems that he plans to use a BCD to 7-segment decoder (like 7447), so he needs to convert to BCD.
     
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,331
    Likes:
    653
    Location:
    Derbyshire, UK
    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?.
     
  11. Tom_Braider

    Tom_Braider New Member

    Joined:
    Feb 13, 2005
    Messages:
    12
    Likes:
    0
    Location:
    Orlando
    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.
     

Share This Page