找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6947|回复: 6
打印 上一主题 下一主题
收起左侧

STC15F2K60S2单片机+esp8266+NTC APP温度测量制作

  [复制链接]
跳转到指定楼层
楼主
准备材料:
STC15F2K60S2
esp8266
NTC热敏电阻
10K电阻一个
连接线若干
手机APP:NetAssist
参考贴子:http://www.51hei.com/bbs/dpj-57255-1.html

接线图:


APP界面:


单片机源程序如下:
  1. #include<ADC.c>

  2. #define uchar unsigned char
  3. #define uint  unsigned int

  4. #define RELOAD_COUNT 0xFA         //宏定义波特率发生器的载入值
  5. #define MAIN_Fosc                22118400L        //定义主时钟        
  6. #define        Timer1_Reload        (65536UL -(MAIN_Fosc / 4 / BaudRate1))                //Timer 1 重装值, 对应300KHZ
  7. #define        Timer2_Reload        (65536UL -(MAIN_Fosc / 4 / BaudRate1))        //Timer 2 重装值, 对应300KHZ
  8. #define        BaudRate1                115200UL        //选择波特率

  9. #define ID_ADDR_ROM 0xeff7      //60K程序空间的MCU
  10. #define ID_ADDR_RAM 0xef        //对于只有256字节RAM的MCU(大部分系列)存放地址为0EFH

  11. unsigned int code Ttable[106][2]={//温度与电阻阻值对应关系表格
  12. 0,3274,//0度对应阻值32.74k
  13. 1,3111,//1度对应阻值31.11k
  14. 2,2957,//2度对应阻值29.57k
  15. 3,2812,//
  16. 4,2674,//
  17. 5,2545,6,2422,7,2306,8,2196,9,2092,10,1993,11,1900,12,1811,
  18. 13,1728,14,1648,15,1573,16,1501,17,1433,18,1369,19,1308,20,1250,
  19. 21,1194,22,1142,23,1092,24,1045,
  20. 25,1000,//25度对应阻值10k               
  21. 26,957,//26度对应阻值9.57k
  22. 27,916,28,877,29,840,30,805,31,771,32,739,33,709,34,679,35,652,
  23. 36,625,37,600,38,576,39,553,40,531,41,510,42,490,43,471,44,453,
  24. 45,435,46,418,47,402,48,387,49,372,50,358,51,345,52,332,53,320,
  25. 54,308,55,297,56,286,57,276,58,266,59,256,60,247,61,238, 62,230,
  26. 63,222,64,214,65,207,66,199,67,193,68,186,69,180,70,174,71,168,
  27. 72,162,73,157,74,        152,75,        147,76,        142,77,        137,78,        133,79,        128,
  28. 80,        124,81,        120,82,        116,83,        113,84,        109,85,        106,
  29. 86,        102,//86度对应阻值1.02k
  30. 87,        99,//87度对应阻值0.99k
  31. 88,        96,89,        93,90,        90,91,        88,92,        85,93,        82,94,        80,95,        78,
  32. 96,        75,97,        73,98,        71,99,        69,100,67,101,65,102,63,103,61, 104,59,
  33. 105,58//105度对应阻值0.58k
  34. };       

  35. #define receive_max 50          //串口接收缓冲长度   
  36. uchar Recive_table[receive_max];        //用于接收wifi模块反馈到MCU上的数据
  37. u8 receive_count=0; //接受数据的个数
  38. u16 NTC_R;//定义热敏电阻阻值变量
  39. int temperature;//定义温度存储变量
  40. sbit LED0=P3^7;

  41. uchar sent_table[receive_max];  


  42. void Uart_Init()//使用定时器1作为波特率发生器
  43. {
  44.     SCON |= 0x40;               //8位数据
  45.     P_SW1 &=  ~0xc0;       //UART1 使用P30 P31口  默认
  46.     TR1 = 0;                    //关闭定时器
  47.     TR1 = 0;                        //波特率使用Timer1产生
  48.     AUXR &= ~0x01;                //S1 BRT Use Timer1;
  49.     TMOD &= ~(1<<6);   //Timer1 set As Timer
  50.     TMOD &= ~0x30;     //Timer1_16bitAutoReload;2
  51.     AUXR |=  (1<<6);        //Timer1 set as 1T mode
  52.     TH1 = (u8)(Timer1_Reload >> 8);
  53.     TL1 = (u8)Timer1_Reload;
  54.     TR1  = 1;
  55.     PS = 1; //高优先级中断
  56.     REN = 1;        //允许接收
  57.     ES  = 1;        //允许中断
  58.     EA = 1;                //允许全局中断
  59. }

  60. void Delay_ms(u16 n)
  61. {
  62.         unsigned int  i,j;
  63.         for(i=0;i<n;i++)
  64.                 for(j=0;j<123;j++);
  65. }

  66. void sendByte(uchar b)
  67. {   ES=0;
  68.     SBUF = b;
  69.     while(!TI);
  70.     TI=0;
  71.     ES=1;
  72. }
  73. void sendString(uchar *s)
  74. {
  75.     while(*s != '\0') //字符串默认结尾'\0',以此来判断字符串的结束
  76.     {
  77.         sendByte(*s++);
  78.     }
  79. }


  80. void ESP8266_Set(uchar *puf) // 数组指针*puf指向字符串数组
  81. {
  82.     sendString(puf);
  83.     Delay_ms(10);
  84.     sendString("\r\n");//回车
  85. }

  86. void ESP8266_Sent(uchar *puf)      // 数组指针*puf指向字符串数组
  87. {
  88.     ESP8266_Set("AT+CIPSEND=0,60");
  89.     sendString(puf);
  90.     Delay_ms(10);
  91.     sendString("\r\n");   //回车
  92. }

  93. void ESP8266_Init()
  94. {
  95.         Delay_ms(2000);
  96.         Delay_ms(1000);
  97.         ESP8266_Set("AT+CIPMUX=1"); //启动多连接
  98.     Delay_ms(5000);
  99.     LED0=!LED0;
  100.     ESP8266_Set("AT+CIPSERVER=1,333");//建立server,端口为333
  101. }


  102. void T_dis(){//温度处理函数采集到的阻值与二维数组的阻值进行比较,从而获得相对应的温度值。
  103.          unsigned char i;
  104.         temperature = 0;
  105.          for(i=105;i<106;i--)  {
  106.                 if(NTC_R >= Ttable[i][1] && NTC_R < Ttable[i-1][1]){
  107.                    temperature=(Ttable[i][0]+1);
  108.                    break;
  109.                 }
  110.         }
  111. }


  112. void ADC_config(void)
  113. {
  114.         ADC_InitTypeDef                ADC_InitStructure;                                //结构定义
  115.         ADC_InitStructure.ADC_Px        = ADC_P10 | ADC_P11 | ADC_P12;        //设置要做ADC的IO,        ADC_P10 ~ ADC_P17(或操作),ADC_P1_All
  116.         ADC_InitStructure.ADC_Speed     = ADC_360T;                        //ADC速度                        ADC_90T,ADC_180T,ADC_360T,ADC_540T
  117.         ADC_InitStructure.ADC_Power     = ENABLE;                        //ADC功率允许/关闭        ENABLE,DISABLE
  118.         ADC_InitStructure.ADC_AdjResult = ADC_RES_H8L2;                //ADC结果调整,        ADC_RES_H2L8,ADC_RES_H8L2
  119.         ADC_InitStructure.ADC_Polity    = PolityLow;                //优先级设置        PolityHigh,PolityLow
  120.         ADC_InitStructure.ADC_Interrupt = DISABLE;                        //中断允许                ENABLE,DISABLE
  121.         ADC_Inilize(&ADC_InitStructure);                                        //初始化
  122.         ADC_PowerControl(ENABLE);                                                        //单独的ADC电源操作函数, ENABLE或DISABLE
  123. }
  124. // convert to char
  125. char convert(u8 i) {
  126.         switch(i){
  127.                 case 0:
  128.                 return '0';
  129.                 case 1:
  130.                 return '1';
  131.                 case 2:
  132.                 return '2';
  133.                 case 3:
  134.                 return '3';
  135.                 case 4:
  136.                 return '4';
  137.                 case 5:
  138.                 return '5';
  139.                 case 6:
  140.                 return '6';
  141.                 case 7:
  142.                 return '7';
  143.                 case 8:
  144.                 return '8';
  145.                 case 9:
  146.                 return '9';
  147.                 default:
  148.                 return i;
  149.         }
  150. }


  151. void ShowResult(uchar ch)
  152. {
  153.     u16 adc_res10,//测量设定通道adc值
  154.         bandgap,  //bandgap预储存校准值,单位毫伏
  155.     adc_9gallery_res; //测量第九通道(bandgap)值

  156.     float power_voltage,        //系统供电电压,单位毫伏
  157.     R,ADC_voltage;     //设定通道电压值,单位毫伏
  158.    
  159.     uchar code *cptr; //定义ROM(代码)区指针
  160.    // uchar  idata *iptr;//定义RAM(内存)区指针
  161.    
  162.     cptr = ID_ADDR_ROM;         //从程序区读取BandGap电压值(单位:毫伏mV)
  163.     bandgap=*cptr++;
  164.     bandgap<<=8;
  165.     bandgap+=*cptr;
  166.     /*iptr = ID_ADDR_RAM;         //从内存区读取BandGap电压值(单位:毫伏mV)
  167.     bandgap=*iptr++;              //两种方法结果一样,上面的方法需要在下载式勾选"在ID号前添加重要测试参数"选项,才可在程序中获取此参数
  168.     bandgap<<=8;                  //下面的方法不需要
  169.     bandgap+=*iptr; */  
  170.     //测量设定通道adc值
  171.     ADC_RES = 0;                    //清除结果寄存器
  172.     P1ASF = 0x80;                   //设置P1口为AD口
  173.     Get_ADC10bitResult(ch);
  174.     Get_ADC10bitResult(ch);                 //读三次获得稳定
  175.     adc_res10= Get_ADC10bitResult(ch);
  176.     //测量第九通道(bandgap)值
  177.     ADC_RES = 0;                    //清除结果寄存器
  178.     P1ASF = 0x00;                   //设置读第九通道
  179.     Get_ADC10bitResult(0);                 //测bandgap时,调用此函数时通道数只能填0
  180.     Get_ADC10bitResult(0);                 //读三次获得稳定
  181.     adc_9gallery_res=Get_ADC10bitResult(0);
  182.     //计算系统供电电压
  183.     power_voltage= (float)bandgap*1024/adc_9gallery_res;
  184.     //计算ADC通道测得电压值
  185.     ADC_voltage= (float)bandgap*adc_res10/adc_9gallery_res;
  186.         R=1000*ADC_voltage/(power_voltage-ADC_voltage);
  187.         NTC_R=R;//把float浮点数转化为int型
  188. }


  189. void main()
  190. {
  191.       
  192.         Uart_Init();
  193.         ESP8266_Init();
  194.         Delay_ms(20);//待系统稳定
  195.         ADC_config();
  196.        
  197.         while(1){

  198.                 ShowResult(7);

  199.         if(receive_count > 0)//如果接受到数据
  200.         {
  201.             Delay_ms(500);
  202.             receive_count = 0;//接收计数清0
  203.                          if(Recive_table[9]=='0') {
  204.                                
  205.                                  sent_table[receive_max]= '\0';
  206.                                
  207.                                 sent_table[0]='N';
  208.                                 sent_table[1]='T';
  209.                                 sent_table[2]='C';
  210.                                 sent_table[3]='=';
  211.                                 sent_table[4]=convert(NTC_R/1000);
  212.                                 sent_table[5]=convert(NTC_R%1000/100);
  213.                                 sent_table[6]=convert(NTC_R%100/10);
  214.                                 sent_table[7]=convert(NTC_R%10);
  215.                                
  216.                                 T_dis();
  217.                                 sent_table[8]='T';sent_table[9]='=';
  218.                                
  219.                                 sent_table[10]=convert(temperature%1000/100);
  220.                                 sent_table[11]=convert(temperature%100/10);
  221.                                 sent_table[12]=convert(temperature%10);
  222.                        
  223. ……………………

  224. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
NTC10K温度计.zip (3.21 MB, 下载次数: 114)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:282506 发表于 2018-12-2 17:13 | 只看该作者
谢谢了!楼主辛苦了!!!!!!!!!!!!
回复

使用道具 举报

板凳
ID:437861 发表于 2018-12-2 19:26 | 只看该作者
不错啊
回复

使用道具 举报

地板
ID:85480 发表于 2019-6-26 16:40 | 只看该作者
不错不错 学习学习!
回复

使用道具 举报

5#
ID:207421 发表于 2019-6-27 13:21 | 只看该作者
不错,8266也不错。
回复

使用道具 举报

6#
ID:589418 发表于 2019-7-24 10:36 | 只看该作者
温度测量误差为多少?
回复

使用道具 举报

7#
ID:248392 发表于 2019-7-24 18:03 | 只看该作者
不错不错 学习学习!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表