#include <math.h>
// define range of temperature measurement
#define MIN_TEMP -20
#define MAX_TEMP 80
// define voltage and resistor values
#define VREF 5 // reference voltage
#define R25 10000 // resistance [url=home.php?mod=space&uid=72445]@[/url] 25degC
/* measure the resistance of the thermistor */
int measure_resistance(void){
...
}
/* Calculate temperature from resistance value */
float calculate_temperature(int resistance) {
double temp;
temp = log(resistance/R25); // natural logarithm
temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp )) * temp ); // Steinhart-Hart equation
temp = temp - 273.15; // Convert Kelvin to Celsius
temp = (temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
if (temp<MIN_TEMP || temp > MAX_TEMP ) {
// invalid temperature, handle error
}
return temp;
}
/* main function */
int main( void ) {
int resistance;
float temperature;
resistance = measure_resistance();
temperature = calculate_temperature(resistance);
printf("The temperature is: %.1f °F", temperature);
return 0;
}
|