Motherboard Forums


Reply
Thread Tools Display Modes

Newbie Tech: Hex to BCD conversion

 
 
Jim Knight
Guest
Posts: n/a
 
      01-31-2004, 03:36 AM
Having an old 8 bit ASM converted to newer (ST7) 8-bit ASM. ST does not
have a BCD mode (which the older processor did), so I need to convert
arithmetic (mostly adds with carry) from hex to BCD, and back.

The BCD uses a 4 bit nibble that does not exceed 10 decimal. In this
application, the BCD is primarily used for 7 segment LED displays (to keep
from trying to have an A-F put out to the LED drivers).

Question:

Is it difficult to explain how you would do this conversion in a system
(ST7) that only works in Hex?

Now that I have re-read this note, I realize acronyms were made to keep
people from communicating with each other ;-)

Jim Knight
remove NOSPAM from direct email responses



 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-31-2004, 04:34 AM
Jim Knight wrote:
>
> Having an old 8 bit ASM converted to newer (ST7) 8-bit ASM. ST
> does not have a BCD mode (which the older processor did), so I
> need to convert arithmetic (mostly adds with carry) from hex to
> BCD, and back.
>
> The BCD uses a 4 bit nibble that does not exceed 10 decimal. In
> this application, the BCD is primarily used for 7 segment LED
> displays (to keep from trying to have an A-F put out to the LED
> drivers).
>
> Question:
>
> Is it difficult to explain how you would do this conversion in
> a system (ST7) that only works in Hex?


This sounds as if you don't care what base values are displayed
in. Conversion to BCD is isomorphic to conversion to an Ascii
string. However you might consider Octal displays, which simply
have to take 3 bits at a time, and use only digits 0 through 7.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
 
Gary Kato
Guest
Posts: n/a
 
      01-31-2004, 04:40 AM
>Is it difficult to explain how you would do this conversion in a system
>(ST7) that only works in Hex?
>


No, not difficult. All you really need to do is to look at what happens to one
nibble. This assumes you added two legal BCD values together into a sum.

First, you need to detect when the nibble goes beyond 9. So, what do you do?
Try an example:

1001 9
0001 1
1010 10(hex) Illegal BCD

What we want is 10hex. To get this, we need to subtract 10(hex) from this
nibble and add 1 to the next higher nibble. If we are looking at the least
significant nibble of a byte, we see that this means adding 16 to increment the
high nibble. So, +16-10 = +6. We come out with this general rule:

1. If the low 4 bits of the sum are > 9, add 6 to sum.

But we have to take another case into account. What if the low nibble of the
sum is legal BCD, but there was a carry from the low nibble to the high during
our binary addition? Example:

01001 9
01001 9
10010 12(hex) = 18 decimal

What we want is 18(hex). We need to add 6 to the sum to make it right. Which
leads to our next rule:

2. If there was a carry from low nibble to high, add 6 to the sum.

Fortunately, the ST7 has a carry bit to show a carry from the high nibble, and
a half-carry bit to show a carry from the low nibble to the high nibble.

I'll leave BCD subtraction to you, though you probably don't need that.



 
Reply With Quote
 
CBarn24050
Guest
Posts: n/a
 
      01-31-2004, 10:04 AM
Hi, the simplest way is to use hex for all your operations. Convert to bcd only
for the led output.
 
Reply With Quote
 
Jim Knight
Guest
Posts: n/a
 
      01-31-2004, 02:03 PM
Gary,
Thanks. Appreciate the clear instructions even someone like me can
understand. Guess I should be able to write a routine that does this and
then just call the routine whenever the original code is operating in
Decimal mode.

Again thanks.

Jim Knight
"Gary Kato" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >Is it difficult to explain how you would do this conversion in a system
> >(ST7) that only works in Hex?
> >

>
> No, not difficult. All you really need to do is to look at what happens to

one
> nibble. This assumes you added two legal BCD values together into a sum.
>
> First, you need to detect when the nibble goes beyond 9. So, what do you

do?
> Try an example:
>
> 1001 9
> 0001 1
> 1010 10(hex) Illegal BCD
>
> What we want is 10hex. To get this, we need to subtract 10(hex) from this
> nibble and add 1 to the next higher nibble. If we are looking at the least
> significant nibble of a byte, we see that this means adding 16 to

increment the
> high nibble. So, +16-10 = +6. We come out with this general rule:
>
> 1. If the low 4 bits of the sum are > 9, add 6 to sum.
>
> But we have to take another case into account. What if the low nibble of

the
> sum is legal BCD, but there was a carry from the low nibble to the high

during
> our binary addition? Example:
>
> 01001 9
> 01001 9
> 10010 12(hex) = 18 decimal
>
> What we want is 18(hex). We need to add 6 to the sum to make it right.

Which
> leads to our next rule:
>
> 2. If there was a carry from low nibble to high, add 6 to the sum.
>
> Fortunately, the ST7 has a carry bit to show a carry from the high nibble,

and
> a half-carry bit to show a carry from the low nibble to the high nibble.
>
> I'll leave BCD subtraction to you, though you probably don't need that.
>
>
>



 
Reply With Quote
 
bogax
Guest
Posts: n/a
 
      02-01-2004, 05:54 PM
Sorry if this is obvious

Don't know if this applies, but notice that if you're doing multiple
digit hex numbers, it's not enough to just convert the hex nibbles to
their BCD equvalents if what you want is to end up with actual BCD

Suppose you've got a two digit hex number. You've got a 1's place
and a 16's place. If you simply convert that 16's place hex digit
to BCD you end up with (sort of) BCD coded hex. You'll need to multiply
it by 16 to get the BCD.

C0 in HEX which is 192 in decimal

if you only convert the high nibble to it's BCD equvalent you get

120 in BCD (or "BCD coded hex")

You have to multiply that 12 by 16



"Jim Knight" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Gary,
> Thanks. Appreciate the clear instructions even someone like me can
> understand. Guess I should be able to write a routine that does this and
> then just call the routine whenever the original code is operating in
> Decimal mode.
>
> Again thanks.
>
> Jim Knight
> "Gary Kato" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > >Is it difficult to explain how you would do this conversion in a system
> > >(ST7) that only works in Hex?
> > >

> >
> > No, not difficult. All you really need to do is to look at what happens to

> one
> > nibble. This assumes you added two legal BCD values together into a sum.
> >
> > First, you need to detect when the nibble goes beyond 9. So, what do you

> do?
> > Try an example:
> >
> > 1001 9
> > 0001 1
> > 1010 10(hex) Illegal BCD
> >
> > What we want is 10hex. To get this, we need to subtract 10(hex) from this
> > nibble and add 1 to the next higher nibble. If we are looking at the least
> > significant nibble of a byte, we see that this means adding 16 to

> increment the
> > high nibble. So, +16-10 = +6. We come out with this general rule:
> >
> > 1. If the low 4 bits of the sum are > 9, add 6 to sum.
> >
> > But we have to take another case into account. What if the low nibble of

> the
> > sum is legal BCD, but there was a carry from the low nibble to the high

> during
> > our binary addition? Example:
> >
> > 01001 9
> > 01001 9
> > 10010 12(hex) = 18 decimal
> >
> > What we want is 18(hex). We need to add 6 to the sum to make it right.

> Which
> > leads to our next rule:
> >
> > 2. If there was a carry from low nibble to high, add 6 to the sum.
> >
> > Fortunately, the ST7 has a carry bit to show a carry from the high nibble,

> and
> > a half-carry bit to show a carry from the low nibble to the high nibble.
> >
> > I'll leave BCD subtraction to you, though you probably don't need that.
> >
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BCD limited to 2 entries mikeyhsd Gigabyte 0 11-21-2007 11:17 PM
help with elf to hex conversion josilton@yahoo.com Embedded 0 01-09-2007 10:45 AM
fast binary to BCD source code philblack Embedded 4 07-06-2004 06:05 PM
New AVR projects using nokia LCD and BCD math library Rolf R Bakke Embedded 1 09-15-2003 11:22 PM
New AVR projects using nokia LCD and BCD math library Rolf R Bakke Embedded 0 09-15-2003 08:26 PM


All times are GMT. The time now is 11:59 AM.


Welcome!
Welcome to Motherboard Point
 

Advertisment