我用esp發送的數據,at能夠通信。但是程序不能接收到客戶端發送的數據,應該是程序的問題,檢查了半天也看不出錯誤,串口調試助手能夠接收到數據,希望各位大佬看看。
#include "stc8f.h"
#include "intrins.h"
#define FOSC 11059200UL
#define BRT (65536 - FOSC / 2400 / 4)
#define BRT1 (65536 - FOSC / 115200 / 4)
#define uchar unsigned char
#define uint unsigned int
sbit go1 = P1^6;//¹âñ¹Ø
sbit go2 = P1^7;//ESP
sbit spen = P3^5;//spwm Æô¶¯
sbit em = P3^2;
uint fre;
bit busy;
uchar wptr=1;
uchar wpeg;
bit busy1;
bit flag;
uchar wptr1;
char datjudge;
uchar buffer[5]={'0',1,2,3,1};
uchar buffer1[16];
uchar jcase;
uchar vote;
uchar timer;
uchar husysdat;
void delay_uint(uint xms)
{
uint x,y;
for(x=0;x<xms;x++)
for(y=0;y<110;y++);
}
void TM0_Isr() interrupt 1
{
timer++;
if(timer&0x50)
{
timer=0;
flag=1;
}
}
void UartIsr() interrupt 4
{
if (TI)
{
TI = 0;
busy = 0;
}
if (RI)
{
RI = 0;
if(husysdat&0x01)
{
buffer[wptr] = SBUF;
wptr++;
if(wptr&0x04)
wptr=1;
}
}
}
void Uart2Isr() interrupt 8
{
if (S2CON & 0x02)
{
S2CON &= ~0x02;
busy1 = 0;
}
if (S2CON & 0x01)
{
S2CON &= ~0x01;
buffer1[wptr1] = S2BUF;
if(buffer1[wptr1]=='\n')
wptr1=0;
else
wptr++;
}
}
void UartInit()
{
SCON = 0x50;
TMOD = 0x00;
TL1 = BRT;
TH1 = BRT >> 8;
TR1 = 1;
wptr = 0x00;
busy = 0;
}
void Uart2Init()
{
S2CON = 0x10;
T2L = BRT1;
T2H = BRT1 >> 8;
wptr1 = 0x00;
busy1 = 0;
}
void UartSend(uchar dat)
{
while (busy);
busy = 1;
SBUF = dat;
}
void Uart2Send(char dat1)
{
while (busy1);
busy1 = 1;
S2BUF = dat1;
}
void Uart2SendStr(char *p)
{
while (*p)
{
Uart2Send(*p);
p++;
}
}
void dq8010(uchar adr,uchar da)
{
UartSend(adr);
delay_uint(120);
UartSend(da);
}
void esp8266rst()
{
Uart2SendStr("AT+CWMODE=2\r\n");
delay_uint(12000);
Uart2SendStr("AT+CWSAP_DEF=\"bpq\",\"12345670\",4,4\r\n");
delay_uint(12000);
Uart2SendStr("AT+RST\r\n");
delay_uint(24000);
Uart2SendStr("AT+CIPMUX=1\r\n");
delay_uint(12000);
Uart2SendStr("AT+CIPSERVER=1,5050\r\n");
delay_uint(12000);
Uart2SendStr("AT+CWDHCP=0,1\r\n");
delay_uint(12000);
Uart2SendStr("AT+CIPSTO=0\r\n");
}
void voteud()
{
if(vote<0x8c)
vote=0x8c;
if(vote>0xa0)
vote=0xa0;
dq8010(0x83,vote);
}
void judge()
{
switch(buffer1[9])
{
case '0':dq8010(0x81,0xaa);go1=0;break;//Í£Ö¹
case '1':dq8010(0x81,0x55);go1=1;break;//Æô¶¯
case '2':jcase=(0<<7);dq8010(0x82,jcase);break;//Íⲿģʽ
case '3':jcase=(1<<7);dq8010(0x82,jcase);break;//ÄÚ²¿Ä£Ê½
case '4':jcase=(1<<4);dq8010(0x82,jcase);break;//±äѹ
case '5':jcase=(0<<4);dq8010(0x82,jcase);break;//²»±äѹ
case '6':jcase=(1<<3);dq8010(0x82,jcase);break;//ÈíÆð¶¯
case '7':jcase=(0<<3);dq8010(0x82,jcase);break;//²»ÈíÆð¶¯
case '8':vote++;;voteud();break;//µçѹÉý
case '9':vote--;voteud();break;//µçѹ½µ
}
}
void frejudge()
{
uchar b;
uint a;
a=buffer1[10];
if(fre!=a)
{
a=a*2.54+0.5;
b=a;
dq8010(0x84,b);
fre=a;
}
}
void main()
{
em = 0;
go1 = 0;
go2 = 0;
P1M0 = 0XFF;
P1M1 = 0X00;
P3M0 = 0XFF;
P3M1 = 0X00;
//P_SW1 = 0x40;
P_SW2 = 0x00;
UartInit();
Uart2Init();
AUXR = 0x54;
ES = 1;
IE2 = 0x01;
TL0 = 0x00;
TH0 = 0x4C;
ET0 = 1;
IP=0X01;
IPH=0X11;
IP2=0X00;
IP2H=0X01;
EA = 1;
go2 = 1;
esp8266rst();
jcase=0xf2;
dq8010(0x82,0xe2);
dq8010(0x83,0x98);
TR0 = 1;
husysdat=0x01;
while (1)
{
judge();
frejudge();
if(flag==1)
{
TR0 = 0;
Uart2SendStr("AT+CIPSEND=0,5\r\n");
delay_uint(120);
dq8010(0x41,0x00);
delay_uint(120);
Uart2SendStr(buffer);
wpeg=0;
flag=0;
TR0 = 1;
}
}
}
|