EmbeddedRelated.com
Forums

MPX pressure sensor & PIC math

Started by bergweed November 24, 2004

Was wondering if someone might show me how to do the math in my PIC
assembly prgm to convert the A/D data from my Motorola MPX4115A to a
meaningful number. More to the point the transfer function is listed
as:
Vout = Vs (.009 * P - .095)
Vs would equal 5 volt Vdd rail (1024)
Vout will be somewhere in the range of 0-5v (0-1024)
P is kilo Pascals
I know the basic algebra, but what's kicking my butt is doing the math
in HEX.
What I want to end up with is the pressure in MBar.
I know that once I arrive at a answer for P is just a matter of
multipling x10 to get MBar.
I'm familiar with the PIC and doing add, subtract, multiply & divide
with whole numbers. I'm guessing the formula has to be rearranged, but
I'm not sure how.
Thanks
Mike B





OK, I assume that the transfer function is the pressure sensor's
analogue out. Frist, solve for P since you want to find pressure.

I get: P= ((Vout/Vs) - 0.095)/0.009

Now for the A/D reading. If your reference is 5V (either Vdd, or an
output from a precision voltage reference), then 5/1024 = volts/unit
step. And the volts/unit step is the number that the PIC tells
you. ie, 256 could be any voltage really, it is all relative to
your reference.

You don't have to think of 1024 in hex, or binary, or octal...
decimal will safice.

So in your little interrupt routine you can have this:

#int_AD
void AD_isr()
{
temp_val=read_adc(ADC_READ_ONLY);
}

// and from where you called the read_adc(ADC_start_only); you will
have the code...

My_pressure= ((temp_val * Vdd)/Vs -0.095)/0.009;

Now you see the clever thing motarolla did is if you set your PIC's
reference to the same reference (Vs) as the motarolla, you then get

My_pressure= (temp_val -0.095)/0.009;

One more thing you need to take note of is you should have defined
the My_pressure as a float. Then everything is in plane English.
101.1 Kpa is 101.1 Kpa, not some goofy language.

As long as you have both devices connected to the same powersupply
voltage, then you are allowed to use the above equation.. if not,
then you will not have Vdd and Vs cancel eachother out.
Theoretically, the supply voltage would be able to drop to 3V and
still give you the correct answer, but non-linerities in the
motarola unit may change this (and for that matter, the A/D unit on
the PIC).

Testing this things highest voltage (the motarolla is putting out
5V),

my_pressure = (1024 -0.095)/0.009 = 113.77 kPa.

The datasheet, however, says that it's max is 115 kPa, but note that
that is with Vs=5.1

From what i can remember, 113.77kPa is much higher than normal air
pressure (currently at Vancouver airport it is 100.75kPa)

I hope i answered that
Chris

--- In , "bergweed" <mikeberg@r...> wrote:
>
> Was wondering if someone might show me how to do the math in my PIC
> assembly prgm to convert the A/D data from my Motorola MPX4115A to
a
> meaningful number. More to the point the transfer function is
listed
> as:
> Vout = Vs (.009 * P - .095)
> Vs would equal 5 volt Vdd rail (1024)
> Vout will be somewhere in the range of 0-5v (0-1024)
> P is kilo Pascals
> I know the basic algebra, but what's kicking my butt is doing the
math
> in HEX.
> What I want to end up with is the pressure in MBar.
> I know that once I arrive at a answer for P is just a matter of
> multipling x10 to get MBar.
> I'm familiar with the PIC and doing add, subtract, multiply &
divide
> with whole numbers. I'm guessing the formula has to be rearranged,
but
> I'm not sure how.
> Thanks
> Mike B





See what multiplying both sides of the equation by 1000 gets you.

1000 Vout = Vs * ( 9 * P - 95)

1000 Vout - (95 * Vs) = Vs * 9 * P

P = (1000 Vout - (95 * Vs)) / (9 * Vs)

95 * Vs and 9 * Vs are constants so the math gets pretty simple.

P = (1000 Vout - 475) / 45 = (200 * Vout - 95) / 9

Check it out with a spreadsheet using floating point with the
original formula and integer arithmetic with this approach. You
still only have integers so the result will be quite granular. It
will be even worse if you only want a small part of the range.

Of course, Vout as converted, is really not 0 to 5V; it is 0..1023
counts. This will also affect the calculation above.

Floating point is the way to go and is available at Microchip but it
all depends on your application.

If you aren't going to display the value then don't convert it. Do
your program logic with the 10 bit A/D value (or even 8 bits if you
can) and use a spreadsheet to figure out what the values should be.

Post back with more specific info re: the application. > > Was wondering if someone might show me how to do the math in my
PIC
> > assembly prgm to convert the A/D data from my Motorola MPX4115A
to
> a
> > meaningful number. More to the point the transfer function is
> listed
> > as:
> > Vout = Vs (.009 * P - .095)
> > Vs would equal 5 volt Vdd rail (1024)
> > Vout will be somewhere in the range of 0-5v (0-1024)
> > P is kilo Pascals
> > I know the basic algebra, but what's kicking my butt is doing
the
> math
> > in HEX.
> > What I want to end up with is the pressure in MBar.
> > I know that once I arrive at a answer for P is just a matter of
> > multipling x10 to get MBar.
> > I'm familiar with the PIC and doing add, subtract, multiply &
> divide
> > with whole numbers. I'm guessing the formula has to be
rearranged,
> but
> > I'm not sure how.
> > Thanks
> > Mike B





I beleive this is a sensor that I did some work with in PIC asm and I
did something similar (though I think I scaled up by a number that is
a power of 2 to make it even easier). When I get a chance to look up
what I did (probably not today;) I'll post it.

--Scott

--- In , "rtstofer" <rstofer@p...> wrote:
>
> See what multiplying both sides of the equation by 1000 gets you.
>
> 1000 Vout = Vs * ( 9 * P - 95)
>
> 1000 Vout - (95 * Vs) = Vs * 9 * P
>
> P = (1000 Vout - (95 * Vs)) / (9 * Vs)
>
> 95 * Vs and 9 * Vs are constants so the math gets pretty simple.
>
> P = (1000 Vout - 475) / 45 = (200 * Vout - 95) / 9
>
> Check it out with a spreadsheet using floating point with the
> original formula and integer arithmetic with this approach. You
> still only have integers so the result will be quite granular. It
> will be even worse if you only want a small part of the range.
>
> Of course, Vout as converted, is really not 0 to 5V; it is 0..1023
> counts. This will also affect the calculation above.
>
> Floating point is the way to go and is available at Microchip but it
> all depends on your application.
>
> If you aren't going to display the value then don't convert it. Do
> your program logic with the 10 bit A/D value (or even 8 bits if you
> can) and use a spreadsheet to figure out what the values should be.
>
> Post back with more specific info re: the application. > > > Was wondering if someone might show me how to do the math in my
> PIC
> > > assembly prgm to convert the A/D data from my Motorola MPX4115A
> to
> > a
> > > meaningful number. More to the point the transfer function is
> > listed
> > > as:
> > > Vout = Vs (.009 * P - .095)
> > > Vs would equal 5 volt Vdd rail (1024)
> > > Vout will be somewhere in the range of 0-5v (0-1024)
> > > P is kilo Pascals
> > > I know the basic algebra, but what's kicking my butt is doing
> the
> > math
> > > in HEX.
> > > What I want to end up with is the pressure in MBar.
> > > I know that once I arrive at a answer for P is just a matter of
> > > multipling x10 to get MBar.
> > > I'm familiar with the PIC and doing add, subtract, multiply &
> > divide
> > > with whole numbers. I'm guessing the formula has to be
> rearranged,
> > but
> > > I'm not sure how.
> > > Thanks
> > > Mike B



My 2 cents, for what it may be worth. Assuming you want to avoid
floating point, you want to use assembly, Vs = 5, the final result is
10X, you have 16 bit routines, Vout is 8 bits, and you do not want to
lose significant bits:

on paper multiply by 10 first then Vout = .45 * MBar - 4.75 =>
MBar = 2.22 * Vout + 4.75
100Mbar = 222 * Vout + 475
8X8 multiply => 100Mbar = Vout(1) + 475
16+8 add => 100Mbar = Vout(2)
16/8 divide Mbar = Vout(3)

Check my math. If Vout is 10 bits, then a little trickier.

Chad

--- bergweed <> wrote:

>
> Was wondering if someone might show me how to do the math in my PIC
> assembly prgm to convert the A/D data from my Motorola MPX4115A to a
> meaningful number. More to the point the transfer function is listed
> as:
> Vout = Vs (.009 * P - .095)
> Vs would equal 5 volt Vdd rail (1024)
> Vout will be somewhere in the range of 0-5v (0-1024)
> P is kilo Pascals
> I know the basic algebra, but what's kicking my butt is doing the
> math
> in HEX.
> What I want to end up with is the pressure in MBar.
> I know that once I arrive at a answer for P is just a matter of
> multipling x10 to get MBar.
> I'm familiar with the PIC and doing add, subtract, multiply & divide
> with whole numbers. I'm guessing the formula has to be rearranged,
> but
> I'm not sure how.
> Thanks
> Mike B >
>


=====
My software has no bugs, only undocumented features.
__________________________________




Thanks for the replies.
However, so far I've not found any answers.

The datasheet available at
http://www.alldatasheet.com/datasheet-pdf/pdf/MOTOROLA/MPX4105A.html
showns a graph with the transfer function formula as an inset.

For example, plugging in a Vout of 3 volts should yield about a P =
75, yet none of the answers I've seen solves this correctly.
Please don't get me wrong here, I do appreciate the help. But either
I'm missing something or else my earlier discription has confused
the folks trying to help out.

Maybe I should restate my question to
How do I rearrange the transfer formula to solve for P?
the original transfer formula is:
Vout = Vs (.009 * P - .095)
Vs = 5.1 (in my case it's 5.0)

So, what I need is a formula the reads like:
P= blah blah blah

I really should have stayed awake in algebra :)
Mike




Sorry, told you to check my math. :-p

on paper divide by 10 first then Vout = .0045 * MBar - .475
Solve for Mbar =>
Mbar = 222 * Vout + 106
8X8 multiply of 222 * Vout => Mbar = Vout(1) + 106
16+8 add of Vout(1) + 106 => Mbar = Vout(2)

Vout = 2.9 => 2.9 * 222 + 106 = 749.8 MBar

If Vout is 10 bits, then a little trickier.

--- In , "bergweed" <mikeberg@r...> wrote:
>
> Thanks for the replies.
> However, so far I've not found any answers.
>
> The datasheet available at
> http://www.alldatasheet.com/datasheet-pdf/pdf/MOTOROLA/MPX4105A.html
> showns a graph with the transfer function formula as an inset.
>
> For example, plugging in a Vout of 3 volts should yield about a P =
> 75, yet none of the answers I've seen solves this correctly.
> Please don't get me wrong here, I do appreciate the help. But either
> I'm missing something or else my earlier discription has confused
> the folks trying to help out.
>
> Maybe I should restate my question to
> How do I rearrange the transfer formula to solve for P?
> the original transfer formula is:
> Vout = Vs (.009 * P - .095)
> Vs = 5.1 (in my case it's 5.0)
>
> So, what I need is a formula the reads like:
> P= blah blah blah
>
> I really should have stayed awake in algebra :)
> Mike




Hmm, has anyone compared datasheets that AllDatasheet and freescale
(motarolla) offer. The formula's given are different. Muhaa. So, comparing graphs, I choose alldatasheet to fit the graph
correctly.

From my last messsage, change my_pressure = (Vout/Vs + 0.09)*0.01

Then in the ad language, you would have
my_pressure = ((temp_val*Vdd)/Vs +0.09)*0.01

This seems to work from a few fast calc's i did. Chris --- In , "sirtiffguy" <sirtiff@h...> wrote:
>
> OK, I assume that the transfer function is the pressure sensor's
> analogue out. Frist, solve for P since you want to find pressure.
>
> I get: P= ((Vout/Vs) - 0.095)/0.009
>
> Now for the A/D reading. If your reference is 5V (either Vdd, or
an
> output from a precision voltage reference), then 5/1024 =
volts/unit
> step. And the volts/unit step is the number that the PIC tells
> you. ie, 256 could be any voltage really, it is all relative to
> your reference.
>
> You don't have to think of 1024 in hex, or binary, or octal...
> decimal will safice.
>
> So in your little interrupt routine you can have this:
>
> #int_AD
> void AD_isr()
> {
> temp_val=read_adc(ADC_READ_ONLY);
> }
>
> // and from where you called the read_adc(ADC_start_only); you
will
> have the code...
>
> My_pressure= ((temp_val * Vdd)/Vs -0.095)/0.009;
>
> Now you see the clever thing motarolla did is if you set your
PIC's
> reference to the same reference (Vs) as the motarolla, you then get
>
> My_pressure= (temp_val -0.095)/0.009;
>
> One more thing you need to take note of is you should have defined
> the My_pressure as a float. Then everything is in plane English.
> 101.1 Kpa is 101.1 Kpa, not some goofy language.
>
> As long as you have both devices connected to the same powersupply
> voltage, then you are allowed to use the above equation.. if not,
> then you will not have Vdd and Vs cancel eachother out.
> Theoretically, the supply voltage would be able to drop to 3V and
> still give you the correct answer, but non-linerities in the
> motarola unit may change this (and for that matter, the A/D unit
on
> the PIC).
>
> Testing this things highest voltage (the motarolla is putting out
> 5V),
>
> my_pressure = (1024 -0.095)/0.009 = 113.77 kPa.
>
> The datasheet, however, says that it's max is 115 kPa, but note
that
> that is with Vs=5.1
>
> From what i can remember, 113.77kPa is much higher than normal air
> pressure (currently at Vancouver airport it is 100.75kPa)
>
> I hope i answered that
> Chris
>
> --- In , "bergweed" <mikeberg@r...> wrote:
> >
> > Was wondering if someone might show me how to do the math in my
PIC
> > assembly prgm to convert the A/D data from my Motorola MPX4115A
to
> a
> > meaningful number. More to the point the transfer function is
> listed
> > as:
> > Vout = Vs (.009 * P - .095)
> > Vs would equal 5 volt Vdd rail (1024)
> > Vout will be somewhere in the range of 0-5v (0-1024)
> > P is kilo Pascals
> > I know the basic algebra, but what's kicking my butt is doing
the
> math
> > in HEX.
> > What I want to end up with is the pressure in MBar.
> > I know that once I arrive at a answer for P is just a matter of
> > multipling x10 to get MBar.
> > I'm familiar with the PIC and doing add, subtract, multiply &
> divide
> > with whole numbers. I'm guessing the formula has to be
rearranged,
> but
> > I'm not sure how.
> > Thanks
> > Mike B




--- In , "bergweed" <mikeberg@r...> wrote:
...
> Maybe I should restate my question to
> How do I rearrange the transfer formula to solve for P?
> the original transfer formula is:
> Vout = Vs (.009 * P - .095)
> Vs = 5.1 (in my case it's 5.0)
>
> So, what I need is a formula the reads like:
> P= blah blah blah

ok, divide both sides by Vs. Vout/Vs = 0.009*P-0.095
add 0.095 to both sides Vout/Vs + 0.095 = 0.009*P
divide both sides by 0.009 (Vout/Vs + 0.095)/0.009 = P

You haven't said what the application is but I think you are going
about it the hard way. The equation is linear. You should be able to
find a value of your ADC reference (and maybe Vs) that gives a simple
relationship between P and your ADC reading.

An example to illustrate my point. For measuring temperature, I have a
sensor that gives 10 mV per degree of temp F. Setting the reference
to 2.55V gives me 10 mV per unit of an 8 bit ADC value. Thus 700 mV
reads as 70 from my ADC and is, gasp, 70 degrees. no conversions
needed. If I had used a Vref of 5, I would have had to scale by .512
It is true that it can't read negative temps or over 255 degrees but
my app doesn't care.

>
> I really should have stayed awake in algebra :)

I couldn't agree more.

Phil





OK, I got curious about this sensor and looked it up on MOTOROLA's
(please note spelling) site. Turns out it is Freescale semi but no
matter. Maybe adding a link to the datasheet in your post would be a
good idea next time...

Note the spec for sensitivity. that gives you 45.9 mV/kPa. so,
picking 4.7V (45.9*1023 = 4.696) as your Vref for a 10 bit ADC will
yield your answer (almost) directly in kPa*10. Its "almost" because
you have to factor in Voffset (204 mV at 15 kPa) by adding 106. This
is 10.55*10 rounded up (10.55 is 10 5/9 - does that look familiar?) to
get the correct answer in kPa*10. You'll need to tweak the vref
slightly if you are using 5V instead of the 5.1 from the datasheet.

There's usually a way to avoid floating point if you think about it...

Phil