利用stc12c5a60s2串口配置连接esp8266的代码
时间: 2023-04-10 21:01:10 浏览: 284
我可以回答这个问题。以下是一个利用stc12c5a60s2串口配置连接esp8266的代码示例:
#include <reg52.h>
#include <intrins.h>
#define FOSC 11059200L
#define BAUD 9600
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit ESP8266_RST = P3^5;
sbit ESP8266_CH_PD = P3^6;
void InitUart();
void SendData(BYTE dat);
void SendString(char *s);
void ESP8266_Init();
void ESP8266_SendCmd(char *cmd);
void ESP8266_ConnectWifi(char *ssid, char *pwd);
void ESP8266_ConnectServer(char *ip, char *port);
void main()
{
InitUart();
ESP8266_Init();
ESP8266_ConnectWifi("wifi_ssid", "wifi_pwd");
ESP8266_ConnectServer("server_ip", "server_port");
while(1);
}
void InitUart()
{
TMOD = 0x20;
TH1 = TL1 = -(FOSC/12/32/BAUD);
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;
EA = 1;
ES = 1;
}
void SendData(BYTE dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
void SendString(char *s)
{
while(*s)
{
SendData(*s++);
}
}
void ESP8266_Init()
{
ESP8266_RST = 0;
ESP8266_CH_PD = 1;
_nop_();
_nop_();
ESP8266_RST = 1;
ESP8266_SendCmd("AT+RST\r\n");
ESP8266_SendCmd("AT+CWMODE=1\r\n");
}
void ESP8266_SendCmd(char *cmd)
{
SendString(cmd);
while(1)
{
if(RI)
{
RI = 0;
if(SBUF == 'O')
{
while(!RI);
RI = 0;
if(SBUF == 'K')
{
break;
}
}
}
}
}
void ESP8266_ConnectWifi(char *ssid, char *pwd)
{
char cmd[50];
sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd);
ESP8266_SendCmd(cmd);
}
void ESP8266_ConnectServer(char *ip, char *port)
{
char cmd[50];
sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%s\r\n", ip, port);
ESP8266_SendCmd(cmd);
}
阅读全文