标题: STC8A8K64S4A12单片机用10K的NTC温度传感电阻测温串口发送的实验程序 [打印本页]

作者: liuqing    时间: 2021-2-6 22:18
标题: STC8A8K64S4A12单片机用10K的NTC温度传感电阻测温串口发送的实验程序
STC8A8K64S4A12用10K的NTC温度传感电阻测温串口发送的实验。
只为验证这种方法的可行性。可以成功测量正确温度。
用单片机5V作为基准电压,NTC上拉10K的电阻。
有兴趣的朋友可以看看,帮完善一下。最好能测8路温度,
业余新手的试验

单片机源程序如下:
  1. #include "stc8.h"
  2. #include "uart.h"
  3. #include "adc.h"
  4. #include <stdio.h>
  5. #include <math.h>


  6. const float Rp=10000.0;                 //10K,25度时的阻值         
  7. const float T2 = (273.15+25.0);   //T2,25度时的开尔文温度
  8. const float Bx = 3950.0;                //B值
  9. const float Ka = 273.15;        //开尔文温度

  10. float Get_Temp(float Rt)
  11. {

  12.         float temp;

  13.                                 //like this R=10000, T2=273.15+25,B=3470, RT=10000*EXP(3470*(1/temp-1/(273.15+25)),  
  14. //        temp = Rt/Rp;
  15. //        temp = log(temp);                //ln(Rt/Rp)
  16.         
  17. //        temp/=Bx;                          //ln(Rt/Rp)/B
  18.         //temp+=(1/T2);
  19. //        temp = 1/(temp);
  20. //        temp-=Ka;
  21.         temp=1/(log(Rt/Rp)/Bx+(1/T2))-Ka;
  22.         return temp;
  23. }

  24. void printstar(void)
  25. {
  26.         printf("*************************\n");
  27. }

  28. void print_message(void)
  29. {
  30.         printf("hello world\n");
  31.         printf("how do you do!\n");
  32.         printf("欢迎学习STC8A8K单片机\n");
  33. }


  34. void main(void)
  35. {
  36.         float t,r,vol;
  37.         P1M0=0X00;P1M1=0XFF;
  38.         Uart1_Init() ;
  39.         adc_init();
  40.         
  41.         while(1)
  42.         {
  43.         
  44.            if(P20==0)
  45.            {
  46.                   

  47.                         print_message();
  48.                         printstar();
  49.                         vol=((float)adc_read()/4096)*5;
  50.                         r=vol/((5-vol)/10000);
  51.                
  52.                          t=Get_Temp(r);
  53.                         printf("ADC %.2f", t) ;
  54.                 }
  55.         }

  56. }
复制代码

全部程序51hei下载地址:
NTC传感器测温.rar (35.1 KB, 下载次数: 134)






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1