PDA

View Full Version : Floating point math



Glenn
- 4th January 2009, 16:17
Now I have rushed into something that I dont know how it works again.

I trying to build something that reads the voltage from a sensor, I connected the sensor as in the datasheet with correct capacitors and connected Vs to the same +5v as the PIC, then I connected the output from the sensor to an ADC input of a 16F877A and reads it with ADCIN.

This works fine, and I get a value that varys between 11 and 90. (When just outputting it to an LCD)

BUT, now I want to know what the real value is, not this representation of the actual voltage.

In the datasheet of the sensor it says:

Vout = Vs*(0.00363*P + 0.04)

..Wich would be (with a little help from my friends, I suck at math)

P = ((Vout/Vs)-0.04)/0.00396

But then I first need the actual voltage, how do I get that ? what kid of value do I get from ADCIN ?

..And How do I use the formula in PBP ? its floating point.. how can I use it to get the value of P on my LCD ?


..I tried to read the PBP manual but cannot find anything, but maybe this is more of a math problem than a PBP problem ?

LinkMTech
- 4th January 2009, 22:29
This works fine, and I get a value that varys between 11 and 90. (When just outputting it to an LCD)

BUT, now I want to know what the real value is, not this representation of the actual voltage.

Assuming an ADCIN of 10bits:
The displayed value comes from Vdd/10bit or 5V/1024= 4.88mv per bit. So 11* 4.88mv= 53.7mv and 90*4.88mv= 439.5mV. I would measure with a DMM to confirm or use a known good voltage at say 2.5V to see a reading of 512 on your display.
Knowing this, you might be able to multiply the ADCIN value to another for your desired display.

Glenn
- 5th January 2009, 00:43
Assuming an ADCIN of 10bits:
The displayed value comes from Vdd/10bit or 5V/1024= 4.88mv per bit. So 11* 4.88mv= 53.7mv and 90*4.88mv= 439.5mV


Aaah.. ofcourse, stupid me, if I had stopped looking for answears in the manual and instead been thinking of how a DA-converter works this would have been obvious. :)

Still, how do I handle decimals ? ..if I just want to display the voltage on the LCD to begin with ? ..and then cobnvert it to a real value with the formula ?

LinkMTech
- 5th January 2009, 01:23
I usually take my decimal numbers and multiply them up to make it an integer. Take this simple equation as an example:
15 * 3/5 = 9
Since 3/5=.6
I mulitiply .6 by 10 to get 6. And divide the whole thing by 10 afterwards.
(15*6)/10=9
I know this is kinda simple but with calculator in hand along with trial and error to check my math (I'm a little weak) I eventually get there. ;)

LinkMTech
- 5th January 2009, 03:47
Forget that lame suggestion, here's a better one from the experts:

http://www.picbasic.co.uk/forum/showthread.php?t=712