Go Down

### Topic: BCD conversion (Read 5265 times)previous topic - next topic

#### JohnSan

##### Jan 15, 2012, 01:02 am
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.

#### mkwired

#1
##### Jan 15, 2012, 02:42 am
I'm sure there's a more efficient way to code this.

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

#### PeterH

#2
##### Jan 15, 2012, 02:53 am

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

Code: [Select]
`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?

#### mkwired

#3
##### Jan 15, 2012, 04:01 am

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

Code: [Select]
`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.

#### GoForSmoke

#4
##### Jan 15, 2012, 09:37 am
And probably should use byte (which is unsigned) instead of char (which is signed).
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### mkwired

#5
##### Jan 15, 2012, 09:50 am
I thought about that but the stored values are small enough that it doesn't really matter.

#### robtillaart

#6
##### Jan 15, 2012, 11:15 am

the basic BCD packaging:

Code: [Select]
`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
Rob Tillaart

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

#### JohnSan

#7
##### Jan 15, 2012, 11:41 am
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
Of course in Hex, I won't need to use the conversion.