Convert long binary number to ASCII Decimal


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    65

    Default Convert long binary number to ASCII Decimal

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    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?
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Aug 2006
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Code:
    ' 
    
    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.
    Last edited by aratti; - 9th July 2009 at 10:03.
    All progress began with an idea

  6. #6
    Join Date
    Aug 2006
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    I was sure some adjustment was going to be necessary since I didn't tested it.


    Al.
    Last edited by aratti; - 9th July 2009 at 14:18.
    All progress began with an idea

Similar Threads

  1. How to convert binary to decimal?
    By Cabomba in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 13th October 2007, 01:27
  2. hex ascii help please
    By ffr58kk90 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th December 2006, 21:09
  3. Decimal to Binary Conversion
    By schlaray in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd December 2006, 14:58
  4. Decimal to binary conversion
    By Demon in forum Code Examples
    Replies: 9
    Last Post: - 25th February 2005, 20:05
  5. Convert a word variable to decimal
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 4th December 2004, 20:02

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts