Math for numbers > 65535?


Closed Thread
Results 1 to 5 of 5
  1. #1
    Scottacus's Avatar
    Scottacus Guest

    Default Math for numbers > 65535?

    I am a hobbyist who new to PIC BASIC and have written a little beginner program that displays values from a pot to an LCD screen using the "pot" command. I have put a subroutine in that displays the ohm values when a switch is pressed and had a question about the math. Since the "pot" command displays from 0 to 255 and the rating of the pot is 10k ohms I divided the two to get each increment equal to 39.21 ohms. I would like to use three significant figures so I changed 39.21 to 392 and divided the result by 10 but the issue is that a word sized variable can only go to 65,535 and I need 100,000. I realize that I could scale back on the numbers (i.e. 196 instead of 392 and divide by 5 rather then 10) to stay in range. Is there a way of getting a variable of greater than word sized 16 bits (i.e. 32bits) with PIC BASIC?

    Thanks in advance,
    Scott

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Scott,

    As long as a 31 bit number will do, there is a way to deal with larger numbers with PBP. Multiplying 2 word sized variables together gives a 32-bit result. You can't use the result directly, but if you immediately follow it with a DIV32 statement you can work with some pretty big numbers.

    Code:
    PotValue  Var byte
    Dummy     Var Word
    W1        Var Word
    
    Dummy = 39216       ' 10,000/255 * 1000 rounded
    W1 = PotValue       ' Copy to a Word variable
    Dummy = Dummy * W1  ' 32-bit result is in PBP system vars
    Dummy = Div32 1000  ' divide 32 bit result by 1000
    ' ** Dummy now holds the resistance value. **
    10000/255 = 39.21568627    So round that to 3 decimal places and multiply by 1000 = 39216

    Now multiply that number times the POT value (0-255)
    39216 * 255 = 10,000,080  This is the largest number, so it fits easily in 31-bits.

    Then in the very next statement divide by 1000 with the Div32
    10,000,080 / 1000 = 10,000 integer

    Granted with only 256 steps, accuracy is probably not very important, but it should do the job.

    For more info on the Div32 operator, see page 33 in the PicBasic manual.

    Another way to approach it might be to use the A/D convertor to read the Pot's position. This would increase the resolution to 10-bit or 1024 steps = 9.775 ohms per step.

    Best regards,
       Darrel

  3. #3
    Scottacus's Avatar
    Scottacus Guest


    Did you find this post helpful? Yes | No

    Smile

    Dear Darrel,

    Thank you very much for your reply. Your help is greatly appreciated!

    Scott Miller

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    My pleasure Scott,

    It's kinda slow around here, got any more probs?

    Anyone?

    Hello -- ello -- llo -- lo

    Echo -- echo -- cho


  5. #5
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default Darrel

    Darrel,
    Yes there is not so much to do here. Because PBP is so good and there are not so much problems to make solutions.
    I use PBP for 5 years now and every time I update and upto now everything works within a few hours.
    I made several applications with PIC's. form cable checkers to parrallel to serial convertors to fully automatical controlled dual fuel engines with data collection for after tests to look with a labtop if everything went good. Al programs are written in PBP (2.44) for PIC and the applications for the pc I wrote in Qbasic.
    So data communication is also no problem.
    I think its a to good compiler !!
    Greetings and happy new year
    Mat

Similar Threads

  1. Line Graph, math problem...
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2009, 04:20
  2. Pulsin Math question
    By ruijc in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 2nd April 2008, 16:15
  3. Multiple if then optimization
    By Mugelpower in forum mel PIC BASIC Pro
    Replies: 35
    Last Post: - 5th March 2008, 12:15
  4. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  5. not quite understanding the MATH function
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th January 2006, 20:20

Members who have read this thread : 1

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