PDA

View Full Version : Convert long binary number to ASCII Decimal



Joe Rocci
- 8th July 2009, 20:08
I've searched this forum from top to bottom, but I can't seem to find a solution to something that I'm sure must have been addressed before.

I'm developing a system that has numerous sub-functions built in, one of which is a precision frequency counter. The counter counts up to 20mhz with 1 Hz resolution using the PIC's internal timer/counters.

The counter code is working just fine, and I have a measurement result expressed as the number times the 16-bit counter has overflowed, plus the value of the final 16-bit count. I want to send this counter result to an ASCII display system as an 8-digit decimal number, but I can't figure out how to convert it given the limited size of PBP's 16-bit integer variables.

Thanks for any guidance,
Joe

mackrackit
- 8th July 2009, 21:45
The easy way might be to use an 18F and LONGs.

The harder way is to break the 16bit number down at some
point, do some calcs and if the final result fits in the 16 bit size
bring it all back together.

Do you want to display the result only or save it to a variable?

Joe Rocci
- 9th July 2009, 01:19
How would you convert it, even if longs were available? The only way I can think of requires division by numbers greater than 16 bits, and multple-byte subtracts.

sinoteq
- 9th July 2009, 06:19
It all depends on speed I think. How fast does this display function have to be?
One way would be to have 2 word size variables (High_Val and Low_Val) and 10 byte variables (No_1 to No_10)

Look at the DIG function in the manual


Example:
High_Val=51234
Low_Val=65535

No_1= High_Val DIG 4 'this makes No_1 to 5
No_2= High_Val DIG 3 'this makes No_2 to 1
No_3= High_Val DIG 2 'this makes No_3 to 2
No_4= High_Val DIG 1 'this makes No_4 to 3
No_5= High_Val DIG 0 'this makes No_5 to 4


No_6= Low_Val DIG 4 'this makes No_6 to 6
No_7= Low_Val DIG 3 'this makes No_7 to 5
No_8= Low_Val DIG 2 'this makes No_8 to 5
No_9= Low_Val DIG 1 'this makes No_9 to 3
No_10= Low_Val DIG 0 'this makes No_10 to 5

Then you just LCD out the numbers you need.

aratti
- 9th July 2009, 09:56
'

Variables
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++

W var Word [8] ' words array
Tcount var Word ' timer count
Ocount var word ' OverFlow timer count

A0 var byte ' general purpose variable

'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++


W[0] = (Ocount * 5) + Tcount dig 4
W[1] = (Ocount * 3) + Tcount dig 3 + W[0] dig 1
W[2] = (Ocount * 5) + Tcount Dig 2 + W[0] dig 2 + W[1] dig 1
W[3] = (Ocount * 5) + Tcount Dig 1 + W[0] dig 3 + W[1] dig 2 + w[2] dig 1
W[4] = (Ocount * 6) + Tcount Dig 0 + W[1] dig 3 + W[2] dig 2 + W[3] dig 1
W[5] = W[2] dig 3 + W[3] dig 2 + W[4] dig 1
W[5] = W[3] dig 3 + W[4] dig 2 + W[5] dig 1
W[6] = W[4] dig 3 + W[5] dig 2 + W[6] dig 1
W[7] = W[5] dig 3 + W[6] dig 2


For A0 = 7 to 0 step -1
'Display W[A0] Dig 0
Next A0



This code (not tested) should add up (overflow number x 65535) and timer counter and distribute the result into the array in position 0 the number (digit) to be displayed.

Just run a for/next cycle with W[x] Dig 0 and you should display your number on your 8 digits display.

If you need the ascii value then you have to add 48 to every single digit in the for/next loop.

Al.

Joe Rocci
- 9th July 2009, 13:48
Thanks all!

Aratti, I ended up using your algorithm, but I fixed a few errors to make it work. Here's the code I ended up with:

W[0] = (Ocount * 6) + Tcount dig 0
W[1] = (Ocount * 3) + Tcount dig 1 + W[0] dig 1
W[2] = (Ocount * 5) + Tcount Dig 2 + W[0] dig 2 + W[1] dig 1
W[3] = (Ocount * 5) + Tcount Dig 3 + W[0] dig 3 + W[1] dig 2 + w[2] dig 1
W[4] = (Ocount * 6) + Tcount Dig 4 + W[1] dig 3 + W[2] dig 2 + W[3] dig 1
W[5] = W[2] dig 3 + W[3] dig 2 + W[4] dig 1
W[6] = W[3] dig 3 + W[4] dig 2 + W[5] dig 1
W[7] = W[4] dig 3 + W[5] dig 2 + W[6] dig 1

Thanks again
Joe

aratti
- 9th July 2009, 13:58
I was sure some adjustment was going to be necessary since I didn't tested it.


Al.