ESP8266与32单片机的通信
单片机源程序如下:
- #include "reg52.h"
- typedef unsigned char u8;
- typedef unsigned int u16;
- sbit LED1 = P0^0;
- sbit LED2 = P0^1;
- sbit LED3 = P0^2;
- sbit LED4 = P0^3;
- u8 dat;
- void ConfigUART()
- {
- TMOD = 0x20;
- SCON = 0x50;
- TH1 = 0xFD;
- TL1 = TH1;
- PCON = 0x00;
- TR1 = 1;
- // RCAP2H=0x0B;//自动重载高8位,
- // RCAP2L=0xDC;//低8位;
- //
- // ET2=1;
- // EA=1;
- // TR2=1;
- }
- void Delay_Us(u8 us)
- {
- while(us--);
- }
- void Delay_ms(u16 n)
- {
- unsigned int i,j;
- for(i=0;i<n;i++)
- for(j=0;j<123;j++);
- }
- void Send_Uart(u8 value)
- {
- SBUF=value;
- while(!TI);
- TI=0;
- }
- void ESP8266_Set(u8 *puf)
- {
- while(*puf!='\0')
- {
- Send_Uart(*puf);
- Delay_Us(5);
- puf++;
- }
- Delay_Us(5);
- Send_Uart('\r');
- Delay_Us(5);
- Send_Uart('\n');
- Delay_ms(200);
- }
- void ESP8266_Init()
- {
- ESP8266_Set("AT+CWMODE=2");
- Delay_ms(1000);
- Delay_ms(1000);
- ESP8266_Set("AT+RST");
- Delay_ms(1000);
- Delay_ms(1000);
- ESP8266_Set("AT+CWSAP=\"kaiwen\",\"chenkaiwen\",11,3");
- Delay_ms(1000);
- Delay_ms(1000);
- ESP8266_Set("AT+CIPMUX=1");
- Delay_ms(1000);
- Delay_ms(1000);
- ESP8266_Set("AT+CIPSERVER=1,5000");
- }
- void ESP8266_SEND()
- {
- if(RI)
- {
- RI = 0;
- dat = SBUF;
- switch(dat)
- {
- case 'a':LED1=1;LED3=1;LED2=1;LED4=1;ESP8266_Set("AT+CIPSEND=0,12");ESP8266_Set(">");ESP8266_Set("LEDOFF");ESP8266_Set("+++");break;
- case 'b':LED1=1;LED3=1;LED2=0;LED4=0;ESP8266_Set("AT+CIPSEND=0,20");ESP8266_Set(">");ESP8266_Set("LEDON 2and4");ESP8266_Set("+++");break;
- case 'c':LED1=0;LED3=0;LED2=1;LED4=1;ESP8266_Set("AT+CIPSEND=0,20");ESP8266_Set(">");ESP8266_Set("LEDON 1and3");ESP8266_Set("+++");break;
- case 'd':LED1=1;LED3=0;LED2=1;LED4=0;ESP8266_Set("AT+CIPSEND=0,20");ESP8266_Set(">");ESP8266_Set("LEDON 3and4");ESP8266_Set("+++");break;
- case 'e':LED1=0;LED3=1;LED2=0;LED4=1;ESP8266_Set("AT+CIPSEND=0,20");ESP8266_Set(">");ESP8266_Set("LEDON 1and2");ESP8266_Set("+++");break;
- }
- }
- if (TI)
- {
- SBUF=dat;
- TI = 0;
- }
- }
- void main()
- {
- LED1=1;
- LED2=1;
- LED3=1;
- LED4=1;
- ConfigUART();
- ESP8266_Init();
- while(1)
- {
- ESP8266_SEND();
- }
- }
复制代码
所有资料51hei提供下载:
ESP8266松.rar
(28.04 KB, 下载次数: 19)
|