Pages: [1]   Go Down
Author Topic: BCD conversion  (Read 1313 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking to convert a 'long to bcd' to drive 5 led displays.
(HP5082 -7302)

Is there a function in a library somewhere to do this, or will it have to be done from scratch.

I know things have moved on since these were about, but would just like to use them....

TIA.
Logged

I'll be glad when I've had enough!

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sure there's a more efficient way to code this.

Code:
void BCD (unsigned long b, char* o)
{
   for (int i=10; i; --i)
   {
      *o = b % 10;
      b /= 10;
      o++;
   }
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sure there's a more efficient way to code this.

Code:
void BCD (unsigned long b, char* o)
{
   for (int i=10; i; --i)
   {
      *o = b % 10;
      b /= 10;
      o++;
   }
}

Shouldn't you be packing two digits per byte (one nibble per digit) if you're outputting BCD?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sure there's a more efficient way to code this.

Code:
void BCD (unsigned long b, char* o)
{
   for (int i=10; i; --i)
   {
      *o = b % 10;
      b /= 10;
      o++;
   }
}

Shouldn't you be packing two digits per byte (one nibble per digit) if you're outputting BCD?

You're probably right, but his wasn't clear what he wanted.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 94
Posts: 4758
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And probably should use byte (which is unsigned) instead of char (which is signed).
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought about that but the stored values are small enough that it doesn't really matter.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


the basic BCD packaging:

Code:
uint8_t dec2bcd(uint8_tdec)
{
  return (dec/10)*16 + (dec%10);
}

uint8_t bcd2dec(uint8_t bcd)
{
  return (bcd/16) * 10 + bcd%16;
}

For longer numbers one need a BCD array
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the info.
The 'data' to be displayed presently, is just a counter.
The solutions are somewhat more simple than i'd anticipated.

Which is great news!

I've found the displays are actually HP5082 -7340
- which means, I can display the number in Decimal or Hex smiley
Of course in Hex, I won't need to use the conversion.
Logged

I'll be glad when I've had enough!

Pages: [1]   Go Up
Jump to: