准备材料:
STC15F2K60S2
esp8266
NTC热敏电阻
10K电阻一个
连接线若干
手机APP:NetAssist
参考贴子:http://www.51hei.com/bbs/dpj-57255-1.html
接线图:
APP界面:
单片机源程序如下:
- #include<ADC.c>
- #define uchar unsigned char
- #define uint unsigned int
- #define RELOAD_COUNT 0xFA //宏定义波特率发生器的载入值
- #define MAIN_Fosc 22118400L //定义主时钟
- #define Timer1_Reload (65536UL -(MAIN_Fosc / 4 / BaudRate1)) //Timer 1 重装值, 对应300KHZ
- #define Timer2_Reload (65536UL -(MAIN_Fosc / 4 / BaudRate1)) //Timer 2 重装值, 对应300KHZ
- #define BaudRate1 115200UL //选择波特率
- #define ID_ADDR_ROM 0xeff7 //60K程序空间的MCU
- #define ID_ADDR_RAM 0xef //对于只有256字节RAM的MCU(大部分系列)存放地址为0EFH
- unsigned int code Ttable[106][2]={//温度与电阻阻值对应关系表格
- 0,3274,//0度对应阻值32.74k
- 1,3111,//1度对应阻值31.11k
- 2,2957,//2度对应阻值29.57k
- 3,2812,//
- 4,2674,//
- 5,2545,6,2422,7,2306,8,2196,9,2092,10,1993,11,1900,12,1811,
- 13,1728,14,1648,15,1573,16,1501,17,1433,18,1369,19,1308,20,1250,
- 21,1194,22,1142,23,1092,24,1045,
- 25,1000,//25度对应阻值10k
- 26,957,//26度对应阻值9.57k
- 27,916,28,877,29,840,30,805,31,771,32,739,33,709,34,679,35,652,
- 36,625,37,600,38,576,39,553,40,531,41,510,42,490,43,471,44,453,
- 45,435,46,418,47,402,48,387,49,372,50,358,51,345,52,332,53,320,
- 54,308,55,297,56,286,57,276,58,266,59,256,60,247,61,238, 62,230,
- 63,222,64,214,65,207,66,199,67,193,68,186,69,180,70,174,71,168,
- 72,162,73,157,74, 152,75, 147,76, 142,77, 137,78, 133,79, 128,
- 80, 124,81, 120,82, 116,83, 113,84, 109,85, 106,
- 86, 102,//86度对应阻值1.02k
- 87, 99,//87度对应阻值0.99k
- 88, 96,89, 93,90, 90,91, 88,92, 85,93, 82,94, 80,95, 78,
- 96, 75,97, 73,98, 71,99, 69,100,67,101,65,102,63,103,61, 104,59,
- 105,58//105度对应阻值0.58k
- };
- #define receive_max 50 //串口接收缓冲长度
- uchar Recive_table[receive_max]; //用于接收wifi模块反馈到MCU上的数据
- u8 receive_count=0; //接受数据的个数
- u16 NTC_R;//定义热敏电阻阻值变量
- int temperature;//定义温度存储变量
- sbit LED0=P3^7;
- uchar sent_table[receive_max];
- void Uart_Init()//使用定时器1作为波特率发生器
- {
- SCON |= 0x40; //8位数据
- P_SW1 &= ~0xc0; //UART1 使用P30 P31口 默认
- TR1 = 0; //关闭定时器
- TR1 = 0; //波特率使用Timer1产生
- AUXR &= ~0x01; //S1 BRT Use Timer1;
- TMOD &= ~(1<<6); //Timer1 set As Timer
- TMOD &= ~0x30; //Timer1_16bitAutoReload;2
- AUXR |= (1<<6); //Timer1 set as 1T mode
- TH1 = (u8)(Timer1_Reload >> 8);
- TL1 = (u8)Timer1_Reload;
- TR1 = 1;
- PS = 1; //高优先级中断
- REN = 1; //允许接收
- ES = 1; //允许中断
- EA = 1; //允许全局中断
- }
- void Delay_ms(u16 n)
- {
- unsigned int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<123;j++);
- }
- void sendByte(uchar b)
- { ES=0;
- SBUF = b;
- while(!TI);
- TI=0;
- ES=1;
- }
- void sendString(uchar *s)
- {
- while(*s != '\0') //字符串默认结尾'\0',以此来判断字符串的结束
- {
- sendByte(*s++);
- }
- }
- void ESP8266_Set(uchar *puf) // 数组指针*puf指向字符串数组
- {
- sendString(puf);
- Delay_ms(10);
- sendString("\r\n");//回车
- }
- void ESP8266_Sent(uchar *puf) // 数组指针*puf指向字符串数组
- {
- ESP8266_Set("AT+CIPSEND=0,60");
- sendString(puf);
- Delay_ms(10);
- sendString("\r\n"); //回车
- }
- void ESP8266_Init()
- {
- Delay_ms(2000);
- Delay_ms(1000);
- ESP8266_Set("AT+CIPMUX=1"); //启动多连接
- Delay_ms(5000);
- LED0=!LED0;
- ESP8266_Set("AT+CIPSERVER=1,333");//建立server,端口为333
- }
- void T_dis(){//温度处理函数采集到的阻值与二维数组的阻值进行比较,从而获得相对应的温度值。
- unsigned char i;
- temperature = 0;
- for(i=105;i<106;i--) {
- if(NTC_R >= Ttable[i][1] && NTC_R < Ttable[i-1][1]){
- temperature=(Ttable[i][0]+1);
- break;
- }
- }
- }
- void ADC_config(void)
- {
- ADC_InitTypeDef ADC_InitStructure; //结构定义
- ADC_InitStructure.ADC_Px = ADC_P10 | ADC_P11 | ADC_P12; //设置要做ADC的IO, ADC_P10 ~ ADC_P17(或操作),ADC_P1_All
- ADC_InitStructure.ADC_Speed = ADC_360T; //ADC速度 ADC_90T,ADC_180T,ADC_360T,ADC_540T
- ADC_InitStructure.ADC_Power = ENABLE; //ADC功率允许/关闭 ENABLE,DISABLE
- ADC_InitStructure.ADC_AdjResult = ADC_RES_H8L2; //ADC结果调整, ADC_RES_H2L8,ADC_RES_H8L2
- ADC_InitStructure.ADC_Polity = PolityLow; //优先级设置 PolityHigh,PolityLow
- ADC_InitStructure.ADC_Interrupt = DISABLE; //中断允许 ENABLE,DISABLE
- ADC_Inilize(&ADC_InitStructure); //初始化
- ADC_PowerControl(ENABLE); //单独的ADC电源操作函数, ENABLE或DISABLE
- }
- // convert to char
- char convert(u8 i) {
- switch(i){
- case 0:
- return '0';
- case 1:
- return '1';
- case 2:
- return '2';
- case 3:
- return '3';
- case 4:
- return '4';
- case 5:
- return '5';
- case 6:
- return '6';
- case 7:
- return '7';
- case 8:
- return '8';
- case 9:
- return '9';
- default:
- return i;
- }
- }
- void ShowResult(uchar ch)
- {
- u16 adc_res10,//测量设定通道adc值
- bandgap, //bandgap预储存校准值,单位毫伏
- adc_9gallery_res; //测量第九通道(bandgap)值
- float power_voltage, //系统供电电压,单位毫伏
- R,ADC_voltage; //设定通道电压值,单位毫伏
-
- uchar code *cptr; //定义ROM(代码)区指针
- // uchar idata *iptr;//定义RAM(内存)区指针
-
- cptr = ID_ADDR_ROM; //从程序区读取BandGap电压值(单位:毫伏mV)
- bandgap=*cptr++;
- bandgap<<=8;
- bandgap+=*cptr;
- /*iptr = ID_ADDR_RAM; //从内存区读取BandGap电压值(单位:毫伏mV)
- bandgap=*iptr++; //两种方法结果一样,上面的方法需要在下载式勾选"在ID号前添加重要测试参数"选项,才可在程序中获取此参数
- bandgap<<=8; //下面的方法不需要
- bandgap+=*iptr; */
- //测量设定通道adc值
- ADC_RES = 0; //清除结果寄存器
- P1ASF = 0x80; //设置P1口为AD口
- Get_ADC10bitResult(ch);
- Get_ADC10bitResult(ch); //读三次获得稳定
- adc_res10= Get_ADC10bitResult(ch);
- //测量第九通道(bandgap)值
- ADC_RES = 0; //清除结果寄存器
- P1ASF = 0x00; //设置读第九通道
- Get_ADC10bitResult(0); //测bandgap时,调用此函数时通道数只能填0
- Get_ADC10bitResult(0); //读三次获得稳定
- adc_9gallery_res=Get_ADC10bitResult(0);
- //计算系统供电电压
- power_voltage= (float)bandgap*1024/adc_9gallery_res;
- //计算ADC通道测得电压值
- ADC_voltage= (float)bandgap*adc_res10/adc_9gallery_res;
- R=1000*ADC_voltage/(power_voltage-ADC_voltage);
- NTC_R=R;//把float浮点数转化为int型
- }
- void main()
- {
-
- Uart_Init();
- ESP8266_Init();
- Delay_ms(20);//待系统稳定
- ADC_config();
-
- while(1){
- ShowResult(7);
- if(receive_count > 0)//如果接受到数据
- {
- Delay_ms(500);
- receive_count = 0;//接收计数清0
- if(Recive_table[9]=='0') {
-
- sent_table[receive_max]= '\0';
-
- sent_table[0]='N';
- sent_table[1]='T';
- sent_table[2]='C';
- sent_table[3]='=';
- sent_table[4]=convert(NTC_R/1000);
- sent_table[5]=convert(NTC_R%1000/100);
- sent_table[6]=convert(NTC_R%100/10);
- sent_table[7]=convert(NTC_R%10);
-
- T_dis();
- sent_table[8]='T';sent_table[9]='=';
-
- sent_table[10]=convert(temperature%1000/100);
- sent_table[11]=convert(temperature%100/10);
- sent_table[12]=convert(temperature%10);
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
NTC10K温度计.zip
(3.21 MB, 下载次数: 117)
|