View Single Post
Old 12-03-2010, 10:31 AM   #4076 (permalink)
DJBecker
EcoModding Apprentice
 
Join Date: Nov 2010
Location: Annapolis
Posts: 159
Thanks: 0
Thanked 32 Times in 27 Posts
Quote:
Originally Posted by Greg Fordyce View Post
Looking at Joe's graph, one idea that struck me is to come up with say half a dozen linear equations for the temperature conversion
That's the table approach. Just pick a few point to measure the temperature response, build a table, and interpolate between the values. You can do a higher-order interpolation, but just a linear one with two points will work fine. An advantage of linear interpolation is that the math and overflow/rounding analysis is simple. You won't get math errors that lead to non-monotonic results.

Again, the challenge with a thermistor is that you'll to calibrate your particular installation. That's not a big deal when you have a single system with a single sensor.

Here is an untested function to do the interpolation. It should be good with 16 bit math as long as you have enough calibration points.

/* Temperature normalizaiton calibration using a linear interpolation table.
* The table is build using calibration numbers taken at a few temperatures,
* including an estimate for the extreme lower and upper raw values.
*/
struct {int raw, C; } temp_table[] = {
{0, -50}, /* Fake a lower bound: raw_adc of 0 is -50C */
{400, 0}, /* Measured value of ice at 0C */
{500, 20}, /* Comfy room at 20C */
{700, 100}, /* Measured value of boiling water at 100C */
{1024, 200}, /* Estimate upper bound: raw_adc of 1024 200C */
};
int raw_to_calibrated(int adc_raw)
{
int i;
int celsius;

/* Scan the table, knowing that we can't fall off the end. */
for (i = 1; adc_raw < temp_table[i].raw; i++)
;
/* i is now the index for the higher value. Interpolate. */
celsius = (adc_raw - temp_table[i-1].raw)*temp_table[i].C +
(temp_table[i-1].raw - adc_raw)*temp_table[i-1].C;
celsius /= temp_table[i].raw - temp_table[i-1].raw;
return celsius;
}
  Reply With Quote
The Following User Says Thank You to DJBecker For This Useful Post:
Greg Fordyce (12-03-2010)