PDA

View Full Version : Implementing a linear function...



sirvo
- 25th July 2007, 04:21
Hi everyone...

I'm working on a temperature sensor and here is a table between signal versus temperature °C: (almost linear)

TEMP VOLTS
0______3.45
10_____2.84
20_____2.3
30_____1.81
40_____1.36
49_____1.02

I would like to show the temperature in a lcd display (from 0 to 49 by unit). What is 'the best' way to implement that?

Thanks in advance..

Sylvio

sayzer
- 25th July 2007, 07:45
Your data is not "almost linear".

Actually, there is no relation among the values at all.

You may want to approximate the values and use Select Case.


What is the sensor you are using?

-----------------------------------

Darrel Taylor
- 25th July 2007, 09:43
"Almost" is a relative term, and I guess it depends on how much "Almost" your application can tolerate.

From this excel graph you can see there's quite a bit of "curve" to it. But it may not be that bad.

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1880&stc=1&d=1185352451">

Converting the formula excel came up with to this...
TempC VAR WORD
ADval VAR WORD

TempC = ADval * 396
TempC = DIV32 1000
TempC = 67 - TempC

LCDOUT "Temp C = ", SDEC TempC," "You should get these results

8-bit
Volts A/D Temp PIC reads
----- --- ----- ---------
3.45 175 0 -2
2.84 144 10 10
2.3 117 20 21
1.81 92 30 31
1.36 69 40 40
1.02 52 49 47

It's off by 2 degrees at the top and bottom of the scale, 1 degree in the middle, and is very close in the areas of 10 and 40 deg.

If that's not within the "Almost" for your program, then Sayzer's idea of a Stepped Linear Interpolation using Select or Lookup/down, will get you closer.

HTH,

sirvo
- 25th July 2007, 14:42
"Almost" is a relative term, and I guess it depends on how much "Almost" your application can tolerate.

From this excel graph you can see there's quite a bit of "curve" to it. But it may not be that bad.

Converting the formula excel came up with to this...

TempC VAR WORD
ADval VAR WORD

TempC = ADval * 396
TempC = DIV32 1000
TempC = 67 - TempC

LCDOUT "Temp C = ", SDEC TempC," "You should get these results


8-bit
Volts A/D Temp PIC reads
----- --- ----- ---------
3.45 175 0 -2
2.84 144 10 10
2.3 117 20 21
1.81 92 30 31
1.36 69 40 40
1.02 52 49 47

It's off by 2 degrees at the top and bottom of the scale, 1 degree in the middle, and is very close in the areas of 10 and 40 deg.

If that's not within the "Almost" for your program, then Sayzer's idea of a Stepped Linear Interpolation using Select or Lookup/down, will get you closer.

HTH,

It is not necessary in my application a 'perfect' result OF temperature. As the excel show, it is ''almost'' linear and it is a good solution. Maybe it will be necessary to get a 'perfect result' measuring another signal in this application and I think that this signal is not 'almost' linear. Then I will try the lookup/down....Thanks Sayzer by helping.

Darrel, thanks for the attention and solution. The result is very satisfatory and I will use it.

Now, I got another doubt. What would be the difference if you change the TempC = DIV32 1000 for this: TempC = TempC / 1000 ?

Thank you very much..!

Regards

Sylvio

Darrel Taylor
- 25th July 2007, 14:54
What would be the difference if you change the TempC = DIV32 1000 for this: TempC = TempC / 1000 ?

At the low end of the temperature scale the A/D value can be up to 175 (0 deg.)
If you multiply that times 396, the result is 69,300. That's over 65,535, so the result of the normal divide would give an incorrect answer.
It has to be DIV32.
<br>