Strugling without floating point


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    pjsmith's Avatar
    pjsmith Guest

    Unhappy Strugling without floating point

    Hi,

    I'm trying to build a pressure sensor (altimeter). I have a working circuit and a mpx pressure sensor. I have changed the ref voltages to get more resolution. I'm using a 16f877.

    My problem is that I have worked out I get .002421875v per step (0 to 1024)+ the 2.47 ref. I'm currently measuring 671 from my a/d result. I've read about scalling up etc, but I'd exceed the limit of an integer with this?

    I want to do

    671 * 0.02421875

    This must be possible, but maths is my stong point I am afraid! Could someone point me in the right direction?

    Thanks.

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


    Did you find this post helpful? Yes | No

    Default

    Your constant is 0.002421875... that's 10 significant figures... far too many for 16-bit math to cope with...

    You've got the 0.002421875 value by taking your 2.48 VRef and dividing it by your 1024 bit resolution...

    Let's forget all decimal places... and just stick to significant digits...

    So now we have 2421875... that's down to 7 significant figures, but if you multiply this constant of yours by eight... you get 19375... five significant figures and an easy word variable to boot...

    671 * 0.002421875 = 1.625078125

    If now you take 671 and multiply it by our new constant of 19375 you get...

    671 * 19375 = 13000625 (result)

    But we are a factor of eight out... so use DIV32 to bring us back in line and simutaneously scale us back into the world of integers...

    result / 800 = 16250 (a resultant easy 16-bit integer value).

    so... by using this method, we end up with 1 and four decimal places (6250) which can be extracted with the DIG command.

    At no time have we exceeded the limits for word integer math or the capabilities of PICBasic. Your best resolution with this method will be +/-0.0001 steps.

    You can do a lot more math and get it better... but it's a first step on the path to enlightenment so to speak...

    Melanie

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    DEFINE LOADER_USED 1
    DEFINE OSC 20
    X VAR WORD
    ADval VAR WORD
    Quanta CON 6181 ' +Vref = 2.47V : Quanta=2.47V/1023*256=0.6181=6181

    Begin:
    FOR X = 1 TO 1023
    ADval = X */ Quanta
    HSEROUT ["Raw = ", DEC X, ": Result = ", dec ADval,13,10]
    NEXT X
    GOTO Begin

    With +Vref at 2.47V, your resolution = 2.47V/1023 = 0.002414467

    671 * 0.002414467 = 1.620V

    If you run the above routine you'll see the values returned on any serial terminal while it simulates returned values with the dummy A/D input values.

    Raw = 1: Result = 24
    Raw = 2: Result = 48
    Raw = 3: Result = 72
    Raw = 4: Result = 96
    Raw = 5: Result = 120
    Raw = 6: Result = 144

    144 represents 0.0144V

    It's fairly close. 2.47V/1023*6=0.014486804. You loose some precision with the */ modifier returning only the middle 16-bits of the multiplication, but it's darn close for integer math.

    <snip>

    Raw = 668: Result = 16128
    Raw = 669: Result = 16152
    Raw = 670: Result = 16176
    Raw = 671: Result = 16200
    Raw = 672: Result = 16225
    Raw = 673: Result = 16249
    Raw = 674: Result = 16273
    Raw = 675: Result = 16297
    Raw = 676: Result = 16321
    Raw = 677: Result = 16345
    Raw = 678: Result = 16369
    Raw = 679: Result = 16394
    Raw = 680: Result = 16418

    2.47V/1023*680=1.641837. Not too bad so far.

    <snip>

    Raw = 1020: Result = 24627
    Raw = 1021: Result = 24651
    Raw = 1022: Result = 24675
    Raw = 1023: Result = 24699

    2.47V/1023*1023=2.47V. 24699 is only 100uV off.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    pjsmith's Avatar
    pjsmith Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks both of you for helpnig me with this. I've got it working (several versions!) and I think it's finally sinking in.

  5. #5
    Join Date
    Sep 2008
    Location
    Stockholm
    Posts
    80


    Did you find this post helpful? Yes | No

    Default

    Hmm.. borrowing this thread a bit..

    I really hate math, especially when I constantly find myself struggling with float math when trying to do something in PBP.. It would be SOOOOO nice if this could be built into PBP, so I didnt have to use my brain for this kind of things.. this is what computers are supposed to be good at.

    In my current program I would like to do:

    calcpress = ((( pressure / Vs) - 0.04 ) / 0.00396 )

    (Vs is the reference Voltage, 5v)

    I really tried to understand what melanie wrote, knowing that it could solve this problem too, but nopes.. dont get it to work out..

    I really would like to get a grip of this, coz when working with the ADC's I constantly get this kind of troubles when I want a "real" value calculated with the formula in the sensors datasheet, like in this case.

    Please help

  6. #6
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Glenn View Post
    In my current program I would like to do:

    calcpress = ((( pressure / Vs) - 0.04 ) / 0.00396 )
    There was a really nice program that someone, (Bruce I think) linked to somewhere in this forum. Ive been searching for it for about half an hour now, but I can't seem to re-find it. Anyway, it was very useful because it could change a fraction into numbers that the PIC can handle. For instance, if you entered .00396 it would show you a couple options like, 2/505, with some options being more accurate than others. Hopefully someone will chime in here and show us the light.

    What you have to do (without the fraction calculator tool) is try to convert your decimal into a fraction. What I did was take 1 and divide it by .00396. The result was 252.52525. So, if you multiply both side by 2, you get 505.0505/2 or pretty close to 505/2. So 2/505 = .00396

    Since you need to divide by .00396, that is the same as multiplying by 505/2

    The left side of your equation is a little harder. I am going to assume you have a 10 bit a/d converter. So I am going to say your 5v = 1024. So your Pressure will be somewhere between 0 and 1024.

    ((Pressure * 1024) - 0.04) * (505/2)

    now that .04 sure would be nice to get rid of. So we can multiply that by 25, but we will also have to divide it by 25, so we keep the number the same. We will also have to multiply Pressure by 25, since it too will be divided by 25.

    Attached Images Attached Images  

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink Calculations

    Hi,

    May be this one ??? ...

    http://www.miscel.dk/MiscEl/miscel.html

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    May be this one ??? ...

    http://www.miscel.dk/MiscEl/miscel.html

    Alain
    Exactly what I was looking for! Thanks Alain

  9. #9
    Join Date
    Sep 2008
    Location
    Stockholm
    Posts
    80


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by scalerobotics View Post

    [ x8 Alot of interesting stuff cutted 8x ]

    Since you need to divide by .00396, that is the same as multiplying by 505/2
    Forgive me for being stupid, but how do I do that ? ( value * (505 /2 )) ?

    Quote Originally Posted by scalerobotics
    The left side of your equation is a little harder. I am going to assume you have a 10 bit a/d converter. So I am going to say your 5v = 1024. So your Pressure will be somewhere between 0 and 1024.
    Yes, the input is a pressure sensor and the vref for it is 5v, I also use a 10bit AD, so 5v = 1024 and 0v is 0.

    My idea was to first convert this value to voltage, and put that value into the next step.

    However this is a bit strange, coz if 5v == 1024, each step is 0.0048828125 . then a reading of 40 == 0.1953125, BUT, if I measure the output pin with a multimeter I get 0.205v ?

    Ah, maybe its not really 5v I thought ? ..measured it, and ok, 4.97, that makes each step 0,004853515625, and then the value is 0,194140625 ? Hmm Hmm..

    I also tried another multimeter, and that showed 0.20 (at the sensors output pin), so I guess thats not the problem.

    But as I understand you I dont have to convert it first at all ?

    Quote Originally Posted by scalerobotics
    ((Pressure * 1024) - 0.04) * (505/2)

    now that .04 sure would be nice to get rid of. So we can multiply that by 25, but we will also have to divide it by 25, so we keep the number the same. We will also have to multiply Pressure by 25, since it too will be divided by 25.
    Hmm, ok, sounds logical..

    To be sure that everything is clear here I'll try to describe what I'm tryiong to do in detail.

    Basically I want to read a pressure sensor (Freescale MPX4250D) with my pic (PIC16F877A) using a analog pin (AN5) with a 10bit AD-converter. I want the value in kPa or Bar.

    Reading the Pressure sensors datsheet I found:

    Vout = Vs* (0.00369*P + 0.04) ± Error

    ..Wich I thought would be the same as

    calcpress = ((( pressure / Vs ) - 0.04 ) / 0.00396 )

    ..I can be wrong here, I'm not very fond of math, and it was many years ago I used my (nearly not existing) mathskills..

    Also, if I understand what you wrote before, I really dont have to convert the output to voltage first ?


    ..Another thing, its very hard to test this stuff.. how do I create a known pressure ? ..I know that the car repairers have a little thing that they use to create pressure or vaccuum witha meter on it, but I guess they are expensive.

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink Think Integer !!!

    Hi,

    At First :

    Vout = Vs* (0.00369*P + 0.04) ± Error

    ..Wich I thought would be the same as

    calcpress = ((( pressure / Vs ) - 0.04 ) / 0.00396 )
    Don't you see something strange ??? ...


    I want the value in kPa or Bar.
    You will display Bars or KPa ... but certainly not calculate with Bars or KPa ...

    May be calculate with Pa ... will give a decent result.

    BUT ... wouldn't it be best to work with ADC Units ( 0 -1023 ...) and ONLY convert to Pressure for display purposes.


    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  11. #11
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Glenn View Post
    how do I do that ? ( value * (505 /2 )) ?
    Well, that is the easy part:

    result = (value * 505)/2

    or

    result = value * 505
    result = result / 2

    Reading the Pressure sensors datsheet I found:

    Vout = Vs* (0.00369*P + 0.04) ± Error

    ..Wich I thought would be the same as

    calcpress = ((( pressure / Vs ) - 0.04 ) / 0.00396 )
    That does not quite work. Here is the way to test it:
    Look at the data sheet where it says 2.5 volts, and check out how much pressure it takes to get it there. Looking down, it shows 130 kPa. Using Vs as 5 for 5v we can multiply it out.

    top equation result is 2.5985v
    your equation result is 7,035.23v (wear some good personal protective equipment for that)

    Also, if I understand what you wrote before, I really dont have to convert the output to voltage first ?

    Voltage is voltage. If we change both side of the equation to mv, v, or a/d steps, we are good.

    I will have to think a while how to convert the equation to something easier. But, if I use the handy dandy program that Alain linked above, we can find that 1/271 = .00369

    So if I do some math (must be done to the .04 as well as the .00369*P) I eventually (after a few mistakes of course.....)

    Result = vs * (((25*Pressure) + 271)/6775)

    This comes about by solving for 1/271*P + 1/25

    First I multiply by 271 .... so....

    (271/271*p + 271/25)/271

    Then I multiply all by 25 to get rid of the fraction

    since 271/271 = 1
    (25*P + 271)/(25 * 271)

    or
    (25 *P +271)/6775

    We can test this using the same method as I tested yours. I caught about 5 of my errors this way, before I go the right answer....

Similar Threads

  1. Getting out of floating point
    By jcb344 in forum General
    Replies: 3
    Last Post: - 5th August 2008, 21:18
  2. floating point numbers
    By n qwerty in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th April 2008, 04:18
  3. Floating Point Display Problem (serial string out)
    By Cash Olsen in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th December 2007, 02:03
  4. Microchip Floating Point Routines
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 12th October 2006, 19:51
  5. DIV32 instead of floating point routines?
    By Tomasm in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd April 2004, 07:50

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