|
51單片機(jī)對esp8266發(fā)送的信號沒有反應(yīng),找不到原因,求大神解決!
這是網(wǎng)上找的單片機(jī)代碼
- #include <reg52.h>
- #include <string.h>
- #include <stdio.h>
- sbit P20_LED=P2^0;
- sbit P32=P3^2;
- char Recive_table[20]=""; //接收緩沖,最大20個(gè)字節(jié)
- char Recive_state = 0; //接收完成標(biāo)志
- 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;
- /****************************************************/
- /**********************主循環(huán)************************/
- while(1)
- {
- ms_delay(10) ;
- if(Recive_state == 1)
- {
- ES=0; //清空接收標(biāo)志位
- 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; //打開接收標(biāo)志位
- }
- /*if(0 == P32) // 通過串口給手機(jī)發(fā)送字符串 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);
- } */
- }
- /****************************************************/
-
- }
- /******************************************************************
- 函 數(shù): void Uart_Interrupt() interrupt 4
- 功 能: 串口中斷函數(shù),將收到的字符存到Recive_table[]數(shù)組中
- 參 數(shù): 無
- 返回值: 無
- *******************************************************************/
- void Uart_Interrupt() interrupt 4
- {
- static char i=0; //因?yàn)槭且晃灰晃唤邮,所以用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;
- }
- /******************************************************************
- 函 數(shù): void Uart_Init(void)
- 功 能: 串口初始化,波特率為9600(這個(gè)不會(huì),上網(wǎng)百度)
- 參 數(shù): 無
- 返回值: 無
- *******************************************************************/
- void Uart_Init(void)
- {
- TMOD=0x20;
- TH1=0xfD;
- TL1=0xfD;
- TR1=1;
- REN=1;
- SM0=0;
- SM1=1;
- EA=1;
- ES=1;
- }
- /******************************************************************
- 函 數(shù): void ms_delay(int t)
- 功 能: 毫秒級延時(shí)
- 參 數(shù): 無
- 返回值: 無
- *******************************************************************/
- void ms_delay(int t)
- {
- int i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- /******************************************************************
- 函 數(shù): void LED(void)
- 功 能: 發(fā)送完命令后顯示用的函數(shù)
- 參 數(shù): 無
- 返回值: 無
- *******************************************************************/
- void LED(void)
- {
- P2 = 0;
- ms_delay(100);
- P2 = 0xff;
- ms_delay(100);
- }
- /******************************************************************
- 函 數(shù): void WIFI_Init(void)
- 功 能: wifi初始化(名字:esp8266;密碼:1234567890)
- 參 數(shù): 無
- 返回值: 無
- *******************************************************************/
- 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"); // 設(shè)置與單片機(jī)一致的波特率
- LED();
- ms_delay(1000) ;
- while(!TI);
- TI = 0;
- ES = 1;
- }
復(fù)制代碼
這是esp8266的燒錄
51hei圖片20191119214251.png (14.63 KB, 下載次數(shù): 49)
下載附件
這是8266的燒錄
2019-11-19 21:43 上傳
我的8266運(yùn)行的波特率9600,與51單片機(jī)RX TX端口沒有接錯(cuò),但3.3v供電還是用的ttl。嘗試過電腦網(wǎng)絡(luò)調(diào)試助手監(jiān)控,手機(jī)網(wǎng)絡(luò)助手發(fā)送1字符時(shí),電腦網(wǎng)絡(luò)助手可以收到31。但8266連上51就什么反應(yīng)都沒有,不知道是哪里錯(cuò)了,求大神解決!
|
|