51单片机用的是普中开发板,晶振11.0592MHz,波特率设置9600
ESP8266RX,TX分别反接单片机TX,RX端
上电后,串口助手显示正常,发送指令时芯片蓝灯闪烁 ,但ESP8266无反馈ok或者别的信息 TCP调试助手也无法连接热点
代码如下,求助各位大神!!!
- #include<reg51.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- uchar i;
- sbit RS=P2^6;
- sbit RW=P2^5;
- sbit E=P2^7;
- sbit D1=P1^0;
- sbit D2=P1^1;
- sbit k1=P2^1;//加按键
- sbit k2=P2^2;//减按键
- sbit k3=P2^0;//设置按键
- uint th=360;//设定 高温
- uint tl=350;//设定 低温
- uint ta;//实际温度
- uchar code t0[]="WD= . ";
- uchar code t1[]="TH= . TL= . ";
- uchar code wendu[]="0123456789"; //利用一个温度表解决温度显示乱码
- sbit DQ=P3^7;//定义DS18B20总线IO
- uchar position;//设置的位置
- uchar table1[]="lowtemp ";
- uchar table2[]="hightemp";
- uchar table3[]="smoke ";
- uchar table4[]="gas ";
- uchar table5[]="help me ";
- //延时子函数
- void delay(uint z)
- {
- uint x,y;
- for(x=100;x>1;x--)
- for(y=z;y>1;y--);
- }
- void ms_delay(uint t)
- {
- uint i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- void us_delay(uchar t)
- {
- while(t--);
- }
- //LCD1602液晶写命令子函数
- void write_com(uchar com)
- {
- RS=0;
- P0=com;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- //LCD1602液晶写数据子函数
- void write_date(uchar date)
- {
- RS=1;
- P0=date;
- delay(5);
- E=1;
- delay(5);
- E=0;
- }
- //LCD1602液晶初始化子函数
- void LCD1602_init()
- {
- E=0;
- RW=0;
- write_com(0x38);
- write_com(0x01);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x80);
- for(i=0;i<16;i++)
- {
- write_date(t0[i]);
- delay(0);
- }
- write_com(0x80+0x40);
- for(i=0;i<16;i++)
- {
- write_date(t1[i]);
- delay(0);
- }
- }
- //延时子函数
- void tmpDelay(int num)
- {
- while(num--);
- }
- //DS18B20温度传感器初始化子函数
- void DS18B20_init()
- {
- uchar x=0;
- DQ=1; //DQ复位
- tmpDelay(8); //稍做延时
- DQ=0; //单片机将DQ拉低
- tmpDelay(80); //精确延时 大于 480us
- DQ=1; //拉高总线
- tmpDelay(14);
- x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
- tmpDelay(20);
- }
- //DS18B20温度传感器读一个字节子函数
- uchar ReadOneChar()
- {
- uchar i=0;
- uchar dat=0;
- for(i=8;i>0;i--)
- {
- DQ=0; // 给脉冲信号
- dat>>=1;
- DQ=1; // 给脉冲信号
- if(DQ)
- dat|=0x80;
- tmpDelay(4);
- }
- return(dat);
- }
- //DS18B20温度传感器写一个字节子函数
- void WriteOneChar(uchar dat)
- {
- uchar i=0;
- for(i=8;i>0;i--)
- {
- DQ=0;
- DQ=dat&0x01;
- tmpDelay(5);
- DQ=1;
- dat>>=1;
- }
- }
- //读取温度子函数
- uint Readtemp()
- {
- uchar a=0;
- uchar b=0;
- uint t=0;
- float tt=0;
- DS18B20_init();
- WriteOneChar(0xCC); // 跳过读序号列号的操作
- WriteOneChar(0x44); // 启动温度转换
- DS18B20_init();
- WriteOneChar(0xCC); //跳过读序号列号的操作
- WriteOneChar(0xBE); //读取温度寄存器
- a=ReadOneChar(); //连续读两个字节数据 //读低8位
- b=ReadOneChar(); //读高8位
- t=b;
- t<<=8;
- t=t|a; //两字节合成一个整型变量。
- tt=t*0.0625; //得到真实十进制温度值,因为DS18B20可以精确到0.0625度,所以读回数据的最低位代表的是0.0625度
- t=tt*10+0.5; //放大十倍,这样做的目的将小数点后第一位也转换为可显示数字,同时进行一个四舍五入操作。
- return(t);
- }
- //LCD1602液晶显示子函数
- void display()
- {
- uint shi,ge,xiaoshu; //这里的num,shi,ge,xiaoshu 必须用unsigned int无符号整数来表示,用unshigned char 字符型则显示错误
- shi=th/100; //显示 最高温度 Th
- ge=th/10%10;
- xiaoshu=th%10;
- write_com(0x80+0x40+3);
- write_date(wendu[shi]);
- write_com(0x80+0x40+4);
- write_date(wendu[ge]);
- write_com(0x80+0x40+6);
- write_date(wendu[xiaoshu]);
- shi=tl/100; //显示 最低文帝 Tl
- ge=tl/10%10;
- xiaoshu=tl%10;
- write_com(0x80+0x40+11);
- write_date(wendu[shi]);
- write_com(0x80+0x40+12);
- write_date(wendu[ge]);
- write_com(0x80+0x40+14);
- write_date(wendu[xiaoshu]);
- }
- void Uart_Init() //使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51等均可)
- {
- TMOD = 0x21;
- SCON = 0x50; //设置串行方式
- TH1 = 0xFD; //波特率9600
- TL1 = TH1;
- PCON = 0x00;
- EA = 1; //总中断打开
- ES = 1; //开串口中断
- TR1 = 1; //启动定时器1
- }
- /********************************************************************
- 名称:串口发送函数 功能:MCU向无线WIFI模块ESP8266发送数据
- ********************************************************************/
- void Send_Uart(uchar value)
- {
- ES=0; //关闭串口中断
- TI=0; //清发送完毕中断请求标志位
- SBUF=value; //发送
- while(TI==0); //等待发送完毕
- TI=0; //清发送完毕中断请求标志位
- ES=1; //允许串口中断
- TH0=0;
- TL0=0;
- }
- /********************************************************************
- 名称:WIFI模块设置函数 作用: 启动模块,以便可以实现无线接入和控制
- ********************************************************************/
- void ESP8266_Set(uchar *puf) // 数组指针*puf指向字符串数组
- {
- while(*puf!='\0') //遇到空格跳出循环
- {
- Send_Uart(*puf); //向WIFI模块发送控制指令。
- us_delay(5);
- puf++;
- }
- us_delay(5);
- Send_Uart('\r'); //回车
- us_delay(5);
- Send_Uart('\n'); //换行
- }
- //报警子函数
- void temp_check()
- {
- uint shi,ge,xiaoshu; //这里的num,shi,ge,xiaoshu 必须用unsigned int无符号整数来表示,用unshigned char 字符型则显示错误
- ta=Readtemp();
- if(ta>th)
- {
- D1=1;
- D2=0;
- ESP8266_Set("AT+CIPSEND=0,10"); ////发送字符长度
- ms_delay(100);
- ESP8266_Set(table2); //发送数组
- ms_delay(100);
- }
- else if(ta<tl)
- {
- D1=0;
- D2=1;
- ESP8266_Set("AT+CIPSEND=0,10"); ////发送字符长度
- ms_delay(100);
- ESP8266_Set(table1); //发送数组
- ms_delay(100);
- }
- else
- {
- D1=0;
- D2=0;
- }
- shi=ta/100; //显示 实际温度
- ge=ta/10%10;
- xiaoshu=ta%10;
- write_com(0x80+3);
- write_date(wendu[shi]);
- write_com(0x80+4);
- write_date(wendu[ge]);
- write_com(0x80+6);
- write_date(wendu[xiaoshu]);
- }
- //按键扫描子函数
- void key()
- {
- if(k3==0) //set 按键 按下
- {
- delay(1);
- if(k3==0)
- { //设置位置设定
- position++;
- if(position>2)
- position=0;
- if(position==0) // 无设置位置
- {
- write_com(0x80+0x40+7);
- write_date(' ');
- write_com(0x80+0x40+15);
- write_date(' ');
- }
- if(position==1) //设置TH 在th后显示< 标识当前位置
- {
- write_com(0x80+0x40+7);
- write_date('<');
- write_com(0x80+0x40+15);
- write_date(' ');
- }
- if(position==2) //设置Tl 在tl后显示< 标识当前位置
- {
- write_com(0x80+0x40+7);
- write_date(' ');
- write_com(0x80+0x40+15);
- write_date('<');
- }
- while(k3==0);
- }
- }
- if(k1==0)
- {
- delay(1);
- if(k1==0)
- {
- if(position==1)
- {
- th+=5;
- if(th>1000)
- th=990;
- }
- if(position==2)
- {
- tl+=5; //tl>th时 是个错误,这里避免此情况发生
- if(tl>=th)
- tl=th-5;
- }
- display();
- }
- while(k1==0);
- }
- if(k2==0)
- {
- delay(1);
- if(k2==0)
- {
- if(position==1)
- {
- th-=5; //th<tl时 是个错误,这里避免此情况发生
- if(th<=tl)
- th=tl+5;
- }
- if(position==2)
- {
- if(tl<10)
- tl=10;
- else
- tl-=5;
- }
- display();
- }
- while(k2==0);
- }
- }
- /************按键模块****************/
- void delaykey(uint x) //延时函数
- {
- uint a,b;
- for(a=x;a>0;a--)
- for(b=110;b>0;b--);
- }
- sbit ledrq=P1^2; //定义LED灯为RD0
- sbit keyrq=P3^5; //定义INT0为RB0(按键)
- sbit ledone=P1^3; //定义LED灯为RD0
- sbit keyone=P3^6; //定义INT0为RB0(按键)
- sbit ledyw=P1^4; //定义LED灯为RD0
- sbit keyyw=P3^7; //定义INT0为RB0(按键)
- void keywarn()
- {
- static bit temp1,temp2,temp3; //定义一个静态的位变量,注意得放在第一行,否则会提示一堆的错误
- temp1=ledrq; //把RD0 LED灯的当前状态赋给变量temp
- temp2=ledone;
- temp3=ledyw;
- if(keyrq==0) //判断按键是否有被按下,有则为0
- {
- delaykey(10); //去抖
- if(keyrq==0) //去抖完得再判断一次
- {
- temp1=!temp1; //每按一次,位变量temp取反一次
- while(!keyrq) //松手检测,按下时为0取反为1,一直循环直至松手为1取反后为0退出循环
- ledrq=temp1; //把取反后的temp值赋给RD0即LED灯
- ESP8266_Set("AT+CIPSEND=0,10"); ////发送字符长度
- ms_delay(100);
- ESP8266_Set(table4); //发送数组
- ms_delay(100);
- }
- }
- if(keyone==0) //判断按键是否有被按下,有则为0
- {
- delaykey(10); //去抖
- if(keyone==0) //去抖完得再判断一次
- {
- temp2=!temp2; //每按一次,位变量temp取反一次
- while(!keyone) //松手检测,按下时为0取反为1,一直循环直至松手为1取反后为0退出循环
- ledone=temp2; //把取反后的temp值赋给RD0即LED灯
- ESP8266_Set("AT+CIPSEND=0,10"); ////发送字符长度
- ms_delay(100);
- ESP8266_Set(table5); //发送数组
- ms_delay(100);
- }
- }
- if(keyyw==0) //判断按键是否有被按下,有则为0
- {
- delaykey(10); //去抖
- if(keyyw==0) //去抖完得再判断一次
- {
- temp3=!temp3; //每按一次,位变量temp取反一次
- while(!keyyw) //松手检测,按下时为0取反为1,一直循环直至松手为1取反后为0退出循环
- ledyw=temp3; //把取反后的temp值赋给RD0即LED灯
- ESP8266_Set("AT+CIPSEND=0,10"); ////发送字符长度
- ms_delay(100);
- ESP8266_Set(table3); //发送数组
- ms_delay(100);
- }
- }
- }
- //主函数
- void main()
- {
- uint i;
- LCD1602_init();
- display();
-
-
- Uart_Init(); //波特率发生器
- ms_delay(500);
- ESP8266_Set("AT+CWMODE=2"); //设置路由器模式1 station,模式2 AP,模式3 station+AP混合模式 设置前wifi的波特率设置成9600
- ms_delay(500);
- ESP8266_Set("AT+RST"); //重新启动wifi模块
- ms_delay(500);
- ESP8266_Set("AT+CWSAP=\"HELLO\",\"wodemima\",11,4"); //AT+CWSAP="wifi_yuan","123456789",11,4 设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK)
- ms_delay(500);
- ESP8266_Set("AT+CIPMUX=1"); //允许接入
- ms_delay(500);
- ESP8266_Set("AT+CIPSERVER=1,8080"); //启动TCP/IP 实现基于网络//控制 ESP8266_Set("AT+CIPSERVER=1,5000");
- ms_delay(500);
- ESP8266_Set("AT+CIPSTO=0"); //永远不超时
- ms_delay(500);
- while(1)
- {
- delay(1);
- keywarn();
- if((++i)>500) //500ms 检测一次温度
- {
-
- temp_check();
- i=0;
- }
- key();
-
-
- }
- }
复制代码
|