程序可以跑通,也可以和手機連接,控制小燈也沒有問題,就是想通過按鍵來向手機發送數據,一直不行,不知道什么原因
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit LED = P1^0;
sbit LED1=P1^2;
u8 dat;
sbit k1=P3^4;
void Init(void);//串口初始化函數
void Delay_ms(u16 n);//延時子函數
void Sent_ZF(u8 dat);//發送一個字節
void AT_Send_String(u8 *string);//發送字符串
void ESP8266_Init();//ESP8266初始化
void Init(void) //串口初始化函數
{
TMOD=0x20; //定時器工作方式2,8位自動重載(0010 0000)
TL1=0xfd; //裝入初值
TH1=0xfd;
TR1=1; //啟動定時器1
REN=1; //允許串行口接收數據
SM0=0; //工作方式1,10位異步收發
SM1=1;
EA=1; //打開全局中斷控制
ES=1; //打開串行口中斷
}
void chuansu()
{
AT_Send_String("AT+CIPSEND=0,5\r\n");
Delay_ms(100);
AT_Send_String("123\r\n");
Delay_ms(100);
}
void main()
{
Init();
ESP8266_Init();
if(k1==0)
{
Delay_ms(10);
if(k1==0)
{
chuansu();
LED1=0;
}
while(!k1) ;
}
while(1);
}
void Sent_ZF(u8 dat) //發送一個字節
{
ES = 0;
TI=0;
SBUF = dat;
while(!TI);
TI = 0;
ES = 1;
}
void Delay_ms(u16 n) //延時子程序
{
unsigned int i,j;
for(i=n;i>0;i--)
for(j=110;j>0;j--);
}
void AT_Send_String(u8 *string) //發送字符串
{
while(*string!='\0')
{
Sent_ZF(*string);
string++;
Delay_ms(10);
}
}
void ESP8266_Init() //ESP8266初始化
{
AT_Send_String("AT+RST\r\n"); //重啟模塊
Delay_ms(100);
Delay_ms(100);
AT_Send_String("AT+CIPMUX=1\r\n"); //設置為多連接模式,啟動模塊
Delay_ms(100);
Delay_ms(100);
AT_Send_String("AT+CIPSERVER=1,8080\r\n"); //服務器的設置端口
Delay_ms(100);
Delay_ms(100);
}
void InterruptUART() interrupt 4 //控制小燈開關
{
RI= 0; //清零
dat= SBUF; //收到的數據占存到SBUF中
{
if(dat=='o')
{
LED =0;
}
if(dat=='f')
{
LED =1;
}
}
}
|