Strugling without floating point


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    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  

  2. #2
    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 " !!!
    *****************************************

  3. #3
    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

  4. #4
    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.

  5. #5
    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 " !!!
    *****************************************

  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
    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....

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


    Did you find this post helpful? Yes | No

    Default Round two, or three depending on who's counting

    Ok, I needed an intermission for this one.

    So we are left with the equation:
    ____________________________________
    definitions:
    result (analog result from A/D conversion)
    P (pressure in kPa)
    VS (voltage supply ie 1024)
    ____________________________________

    result = VS * (((25 * P) + 271) / 6775)

    So to start solving for P .....

    result * 6775 = VS * ((25 * P) + 271)

    (result * 6775) / VS = 25 * P + 271

    ((result * 6775) / VS) - 271 = 25 * P

    (((result * 6775) / VS) - 271) / 25 = P

    Or P = (((result * 6775) / VS) - 271) / 25

    It only hurts a little when I try to figure these things out. I suppose the more you do it, the less it hurts. Or if you are a math guru, maybe this is fun. You guys are sick!

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


    Did you find this post helpful? Yes | No

    Default

    Darrel pointed out that I could further reduce the equation, and I had the following error:

    The range for A/D conversion is only from 0 to 1023, not 1024 like I had stated. Alain caught that too, but I missed that.

    So to further simplify the equation (notice how complicated it is to simplify things?)

    We start with the same thing we ended with in the last post.

    P = (((result * 6775) / VS) - 271) / 25

    Put in the known high range value for VS

    P = (((result * 6775) / 1023) - 271) / 25

    Don't be afraid of the decimals here ....

    Dividing by 25 we get:

    P = (((result * 271) / 1023) - 10.84)

    271/1023 = .2649

    P = (result * .2649) - 10.84

    We can get there by .... (Thanks again Darrel!)
    Code:
    ADCIN 0, result
    Pressure = result * 2649
    Pressure = DIV32 1000
    Pressure = Pressure - 108
    
    LCDOUT DEC Pressure/10,".",DEC Pressure//10
    The above code converts the .2649 to 2.649 by multiplying result * 2649, then dividing by 1000. It subtracts 10 x the 10.84 which averages out to 108. so the division by 10, and the remainder from the division by 10 can give accurate results.

    And we can do all this without using slow, bloated, floating point code, available here:

    <A Href="http://www.melabs.com/resources/fp.htm">Using Microchip's <b>Floating</b> Point routines with PicBasic Pro Compiler</a>

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


    Did you find this post helpful? Yes | No

    Default

    Ok, that was pretty bassackwards. If anyone followed all that, I don't know whether to feel sorry for you, or commend you. Sorry to wast all that space. Wish I could edit, or even better delete...

    Here is the "simple" way.....

    Known equation from the data sheet:
    Vout = Vs* (0.00369*P + 0.04)

    using 1/271 = .00369, and dividing both sides by .00369

    Vout * 271 = VS * (P + (.04/.00369))
    or Vout * 271 = VS * (P + 10.84)

    Vout * 271 / VS = P +10.84

    (Vout * 271 / VS) - 10.84 = P

    (Vout * (271 / 1023)) - 10.84 = P

    (Vout * .2649) - 10.84 = P

    But, again, the program that Alain pointed to in one of the above posts is useful to find a fraction to represent .00369.

    Hopefully this is much cleaner to follow. It still yields the same result, just possibly less excedrin.

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


    Did you find this post helpful? Yes | No

    Default

    Since you arrived at the same conclusion.

    Does it really matter how you got there?

    Factoring polynomials is more of an Art.
    There's almost always more than one way to get there.

    Nice work Walter!
    <br>
    DT

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