|
51单片机对esp8266发送的信号没有反应,找不到原因,求大神解决!
这是网上找的单片机代码
- #include <reg52.h>
- #include <string.h>
- #include <stdio.h>
- sbit P20_LED=P2^0;
- sbit P32=P3^2;
- char Recive_table[20]=""; //接收缓冲,最大20个字节
- char Recive_state = 0; //接收完成标志
- void WIFI_Init(void);
- void Uart_Init(void);
- void ms_delay(int t);
- void LED(void);
- int main (void)
- {
- /********************功能初始化***********************/
- Uart_Init();//串口初始化,波特率为9600
- ms_delay(1000) ;
- WIFI_Init(); //wifi初始化
- P20_LED=0;
- /****************************************************/
- /**********************主循环************************/
- while(1)
- {
- ms_delay(10) ;
- if(Recive_state == 1)
- {
- ES=0; //清空接收标志位
- if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//接收到的字符串形式为+IPD,x,x:y
- {
- if((Recive_table[3]=='D')&&(Recive_table[6]==','))
- {
- if(Recive_table[9]=='0')
- P20_LED = 0;
- if(Recive_table[9]=='1')
- P20_LED = 1;
- }
- }
- memset(Recive_table,'\0',20);
- Recive_state = 0;
- ES=1; //打开接收标志位
- }
- /*if(0 == P32) // 通过串口给手机发送字符串 wantin
- {
- ms_delay(400);
- if(0 == P32)
- {
- ES = 0;
- TI = 1;
- printf("AT+CIPSEND=?\r\n");
- LED();
- ms_delay(1000) ;
- printf("AT+CIPSEND=0,10\r\n");
- LED();
- ms_delay(1000) ;
- printf("wangting\r\n");
- LED();
- ms_delay(1000) ;
- LED();
- ms_delay(1000) ;
- LED();
- ms_delay(1000) ;
- while(!TI);
- TI = 0;
- ES = 1;
- }
- while(0 == P32);
- } */
- }
- /****************************************************/
-
- }
- /******************************************************************
- 函 数: void Uart_Interrupt() interrupt 4
- 功 能: 串口中断函数,将收到的字符存到Recive_table[]数组中
- 参 数: 无
- 返回值: 无
- *******************************************************************/
- void Uart_Interrupt() interrupt 4
- {
- static char i=0; //因为是一位一位接收,所以用static
- if(RI==1)
- {
- ES = 0;
- RI=0;
- Recive_table[i]=SBUF;
- i++;
- if((Recive_table[i-1] == '\n'))
- {
- Recive_table[i]='\0';
- i=0;
- Recive_state = 1;
- }
- ES = 1;
- }
- else
- TI = 0;
- }
- /******************************************************************
- 函 数: void Uart_Init(void)
- 功 能: 串口初始化,波特率为9600(这个不会,上网百度)
- 参 数: 无
- 返回值: 无
- *******************************************************************/
- void Uart_Init(void)
- {
- TMOD=0x20;
- TH1=0xfD;
- TL1=0xfD;
- TR1=1;
- REN=1;
- SM0=0;
- SM1=1;
- EA=1;
- ES=1;
- }
- /******************************************************************
- 函 数: void ms_delay(int t)
- 功 能: 毫秒级延时
- 参 数: 无
- 返回值: 无
- *******************************************************************/
- void ms_delay(int t)
- {
- int i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- /******************************************************************
- 函 数: void LED(void)
- 功 能: 发送完命令后显示用的函数
- 参 数: 无
- 返回值: 无
- *******************************************************************/
- void LED(void)
- {
- P2 = 0;
- ms_delay(100);
- P2 = 0xff;
- ms_delay(100);
- }
- /******************************************************************
- 函 数: void WIFI_Init(void)
- 功 能: wifi初始化(名字:esp8266;密码:1234567890)
- 参 数: 无
- 返回值: 无
- *******************************************************************/
- void WIFI_Init(void)
- {
- ES = 0;
- TI = 1;
- printf("AT+RST\r\n");
- LED();
- ms_delay(1000) ;
- printf("AT+CWMODE=3\r\n");
- LED();
- ms_delay(1000) ;
- printf("AT+CIPMUX=1\r\n");
- LED();
- ms_delay(1000) ;
- printf("AT+CIPSERVER=1,8080\r\n");
- LED();
- ms_delay(1000) ;
- printf("AT+CIOBAUD=9600\r\n"); // 设置与单片机一致的波特率
- LED();
- ms_delay(1000) ;
- while(!TI);
- TI = 0;
- ES = 1;
- }
复制代码
这是esp8266的烧录
我的8266运行的波特率9600,与51单片机RX TX端口没有接错,但3.3v供电还是用的ttl。尝试过电脑网络调试助手监控,手机网络助手发送1字符时,电脑网络助手可以收到31。但8266连上51就什么反应都没有,不知道是哪里错了,求大神解决!
|
|