Math Formula / Variable Problem! Help!


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    PBP does integer math only. You should rewrite the equation to multiply the A/D converter result by 5000 then divide by 64320 (or multiply by 500 and divide by 6432).

    The result will be an integer, so you will lose the fractional part. However, you can get an extra digit of resolution if you modify the formula to something like:

    FirstPart = (ADresult * 5000)/6432

    HSEROUT [FirstPart/10,".",FirstPart//10]
    Charles Linquist

  2. #2
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Wouldn't multiplying the AD result by 5000 or 500 cause the variable to overflow?

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Yes it would. Sorry, my error.

    So, the best you could do is to multiply by 50 and divide by 643.
    You could possibly get closer by using a different multiply/divide ratio.

    Another option would be to use the DIV32 function.
    Charles Linquist

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    > Lets say I get a reading on the ADC of 11260.

    Well, that in itself would be a problem... your 10-bit ADC shouldn't give you readings outside the range 0-1024.

    Do the *5000 multiplication first, this in effect would cause an overflow from a word variable, but if immediately followed by a DIV32 (as Charles suggested) would result in an integer value.

    So your input of 0-1024 multiplied by 5000 and divided by 64320 would result in a range of 0-79 which is very small and you lose a great deal of precision.

    You can add TWO additional signigicant figures by multiplying by 50000 and dividing by 6432, this will then give you a range of 0-7960 which then by using the DIG function will allow you to extract the digits and insert a decimal point displaying your two decimal points to give you 0-79.60

    Always work within a WORD variable but if you must spill out of it momentarily, immediately use the DIV32 function to come back.

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Variable problem
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 30th May 2008, 21:25
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. PicBasic Pro Math Problem??
    By Glen65 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th March 2006, 04:36
  5. Math Problem
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 1st March 2006, 13:01

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