[LKS32 软件] 利用热敏电阻通过ADC测温

[复制链接]
666|3
 楼主| zhuotuzi 发表于 2023-2-17 11:00 | 显示全部楼层 |阅读模式
  1. #include <math.h>
  2. // define range of temperature measurement
  3. #define MIN_TEMP -20
  4. #define MAX_TEMP 80
  5. // define voltage and resistor values
  6. #define VREF 5 // reference voltage
  7. #define R25 10000 // resistance [url=home.php?mod=space&uid=72445]@[/url] 25degC

  8. /* measure the resistance of the thermistor */
  9. int measure_resistance(void){
  10.     ...
  11. }

  12. /* Calculate temperature from resistance value */
  13. float calculate_temperature(int resistance) {
  14.     double temp;
  15.     temp = log(resistance/R25); // natural logarithm
  16.     temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp )) * temp ); // Steinhart-Hart equation
  17.     temp = temp - 273.15; // Convert Kelvin to Celsius
  18.     temp = (temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
  19.     if (temp<MIN_TEMP || temp > MAX_TEMP ) {
  20.         // invalid temperature, handle error
  21.     }
  22.     return temp;
  23. }

  24. /* main function */
  25. int main( void ) {
  26.     int resistance;
  27.     float temperature;
  28.    
  29.     resistance = measure_resistance();
  30.     temperature = calculate_temperature(resistance);

  31.     printf("The temperature is: %.1f °F", temperature);

  32.     return 0;

  33. }


szt1993 发表于 2023-2-23 14:49 | 显示全部楼层
C=1/(LN(RT/R25)/B+1/298.15)-273.15
NIMIJACK 发表于 2023-3-2 10:16 | 显示全部楼层
你这计算量也太大了吧,并且浮点数还不加f
caigang13 发表于 2023-3-9 22:25 来自手机 | 显示全部楼层
你这附点数有必要那么多小数位嘛?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

217

主题

3393

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部