Thanks both of you for helpnig me with this. I've got it working (several versions!) and I think it's finally sinking in.
Thanks both of you for helpnig me with this. I've got it working (several versions!) and I think it's finally sinking in.
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![]()
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.
![]()
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 " !!!
*****************************************
Forgive me for being stupid, but how do I do that ? ( value * (505 /2 )) ?
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.Originally Posted by scalerobotics
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 ?
Hmm, ok, sounds logical..Originally Posted by scalerobotics
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.
Hi,
At First :
Don't you see something strange ??? ...Vout = Vs* (0.00369*P + 0.04) ± Error
..Wich I thought would be the same as
calcpress = ((( pressure / Vs ) - 0.04 ) / 0.00396 )
You will display Bars or KPa ... but certainly not calculate with Bars or KPa ...I want the value in kPa or Bar.
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 " !!!
*****************************************
Well, that is the easy part:
result = (value * 505)/2
or
result = value * 505
result = result / 2
That does not quite work. Here is the way to test it: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 )
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....
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!
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!)
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.Code:ADCIN 0, result Pressure = result * 2649 Pressure = DIV32 1000 Pressure = Pressure - 108 LCDOUT DEC Pressure/10,".",DEC Pressure//10
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>
Bookmarks