|
机智云串口租售无法使wifi进入配置模式,不知道为什么使用机智云串口助手就是不好用,连接8266后也无法配置,
求教哪位好心的大神帮忙解决一下,万分感激!!!!
#include "STC15W4K.H" // 包含 "STC15W4K.H"寄存器定义头文件
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcd0=P1^1;
sbit key_config_Ap=P1^2;
sbit key_config_Air=P1^3;
idata uchar rece_buf[32],comm_dat[32],temper;
idata uchar sn,checksum;
idata uchar recFlag;
idata uchar mainclock=0;
idata uchar mainsec = 0;
idata uchar num=0; // 存放接收到的1个字节的数据
idata uchar recevdatapm = 0;
idata uchar usrtlen = 0;
idata uchar usartbuf[32]=0;
idata uchar uart_num=0;
idata uchar usarrtflag = 0;
idata uchar endflag = 0;
idata uchar comm_len=0;
uchar ButtonFlag = 0;
uchar send_3_config,send_7_error,send_8_dev,send_9_dev;
/*wifi模组请求设备信息,mcu回复*/
uchar mcu_send_1[75]={
0xff,0xff,0x00,0x47,0x02,0x55,0x00,0x00,
0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x34,
0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x34,
0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,
0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,
0x35,0x65,0x33,0x66,0x62,0x61,0x37,0x32,
0x34,0x32,0x33,0x61,0x34,0x63,0x36,0x63,
0x62,0x38,0x37,0x39,0x33,0x38,0x39,0x39,
0x62,0x61,0x61,0x66,0x31,0x35,0x38,0x30,
0x00,0x00,0x55};
/*wifi模组与设备mcu心跳,mcu回复*/
uchar mcu_send_2[9]={0xff,0xff,0x00,0x05,0x08,0x55,0x00,0x00,0x55};
/*设备mcu通知wifi模组进入配置模式,mcu发送*/
uchar mcu_send_3[10]={0xff,0xff,0x00,0x06,0x09,0x55,0x00,0x00,0x02,0x55};
/*设备mcu重置wifi,mcu发送*/
uchar mcu_send_4[9]={0xff,0xff,0x00,0x05,0x0b,0x55,0x00,0x00,0x55};
/*wifi模组向设备通知工作状态变化,mcu回复*/
uchar mcu_send_5[9]={0xff,0xff,0x00,0x05,0x0e,0x55,0x00,0x00,0x55};
/*wifi模组请求重启mcu,mcu回复*/
uchar mcu_send_6[9]={0xff,0xff,0x00,0x05,0x10,0x55,0x00,0x00,0x55};
/*非法消息通知,mcu回复*/
uchar mcu_send_7[10]={0xff,0xff,0x00,0x06,0x12,0x55,0x00,0x00,0x11,0x55};
/*wifi模组读取设备当前状态,mcu回复*/
uchar mcu_send_8[11]={0xff,0xff,0x00,0x07,0x04,0x55,0x00,0x00,0x03,0x11,0x55};
/*设备向wifi主动上报当前状态,mcu发送*/
uchar mcu_send_9[11]={0xff,0xff,0x00,0x07,0x05,0x55,0x00,0x00,0x04,0x11,0x55};
/*wifi模组控制设备,mcu回复*/
uchar mcu_send_10[9]={0xff,0xff,0x00,0x05,0x04,0x55,0x00,0x00,0x55};
/*mcu通知wifi进入可绑定模式,mcu发送*/
uchar mcu_send_12[9]={0xff,0xff,0x00,0x05,0x15,0x55,0x00,0x00,0x55};
/*延时函数*/
void Delay_ms(unsigned char z)
{
unsigned char x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void MEMCOPY(uchar *dest,uchar *src,uint len)
{
while(len--)
{
*dest++ = *src++;
}
}
/*mcu发送数据程序*/
void Usart_SendArrang(uchar *dat,uchar len)
{
unsigned char i;
for(i=0;i<len;i++)
{
ES=0;
SBUF=dat[i];
while(!TI);
TI=0;
ES=1;
}
}
/**************************************************
- 功能描述:51单片机的串口发送字节的函数
- 隶属模块:STC51串口操作
- 函数属性:外部,使用户使用
- 参数说明:mydata:要发送的一个字节
- 返回说明:无
- 注:发送一个字节,是串口发送的基础操作
**************************************************/
void UART_Send_Byte(unsigned char mydata)
{
ES=0;
TI=0;
SBUF=mydata;
while(!TI);
TI=0;
ES=1;
}
/**************************************************
- 功能描述:51单片机的串口发送字符串
- 隶属模块:STC51串口操作
- 函数属性:外部,使用户使用
- 参数说明:s:指向字符串的指针
- 返回说明:无
**************************************************/
void UART_Send_Str(char *s)
{
int i=0;
while(s[i]!=0)
{
UART_Send_Byte(s[i]);
i++;
}
}
void uart_int(void)
{ // 下面代码设置定串口1
T2H = 0xFE; // 波特率:9600 /22.1184MHZ,1T
T2L = 0xE0; // 波特率:9600 /22.1184MHZ,1T
AUXR = 0x15; // 0001 0101,T2R=1启动T2运行,T2x12=1,定时器2按1T计数,S1ST2=1
SCON = 0x50; // 0101 0000 SM0.SM1=01(最普遍的8位通信),REN=1(允许接收)
AUXR1 |= 0x40;
// 下面代码设置定时器中断
TMOD = 0x01; //设置定时器模式
TL0 = 0xB0; //设置定时初值
TH0 = 0x3C; //设置定时重载值
ET0 = 1; //清除TF0标志
TR0 = 1; //定时器0开始计时
ES = 1; // 开串口1中断
EA = 1; // 开总中断
}
void Usart_Communication()
{
uchar i;
if(mainclock==10)
{
ET0=0;
mainclock=0;
usarrtflag=1;
REN=0;
}
if(usarrtflag)
{
sn=usartbuf[5];
checksum=0;
switch(usartbuf[4])
{
case 0x01:
mcu_send_1[5]=sn;
for(i=2;i<74;i++)
{
checksum=checksum+mcu_send_1[i];
}
checksum=checksum%256;
mcu_send_1[74]=checksum;
Usart_SendArrang(mcu_send_1,75);
break;
case 0x07:
mcu_send_2[5]=sn;
for(i=2;i<8;i++)
{
checksum=checksum+mcu_send_2[i];
}
checksum=checksum%256;
mcu_send_2[8]=checksum;
Usart_SendArrang(mcu_send_2,9);
break;
case 0x0a:
break;
case 0x0c:
break;
case 0x0d:
mcu_send_5[5]=sn;
for(i=2;i<8;i++)
{
checksum=checksum+mcu_send_5[i];
}
checksum=checksum%256;
mcu_send_5[8]=checksum;
Usart_SendArrang(mcu_send_5,9);
break;
case 0x0f:
mcu_send_6[5]=sn;
for(i=2;i<8;i++)
{
checksum=checksum+mcu_send_6[i];
}
checksum=checksum%256;
mcu_send_6[8]=checksum;
Usart_SendArrang(mcu_send_6,9);
break;
case 0x11:
mcu_send_7[5]=sn;
send_7_error=usartbuf[8];
mcu_send_7[8]=send_7_error;
for(i=2;i<9;i++)
{
checksum=checksum+mcu_send_7[i];
}
checksum=checksum%256;
mcu_send_7[9]=checksum;
Usart_SendArrang(mcu_send_7,10);
break;
case 0x03:
if(usartbuf[3]=0x06&&usartbuf[8]==0x02)
{
mcu_send_8[5]=sn;
send_8_dev=~P1;
mcu_send_8[9]=send_8_dev;
for(i=2;i<10;i++)
{
checksum=checksum+mcu_send_8[i];
}
checksum=checksum%256;
mcu_send_8[10]=checksum;
Usart_SendArrang(mcu_send_8,11);
break;
}
if(usartbuf[3]=0x08&&usartbuf[8]==0x01)
{
//Control_Mcu(); //收到数据后,mcu控制设备函数
mcu_send_10[5]=sn;
for(i=2;i<8;i++)
{
checksum=checksum+mcu_send_10[i];
}
checksum=checksum%256;
mcu_send_10[8]=checksum;
Usart_SendArrang(mcu_send_10,9); //mcu回复
mcu_send_9[5]=sn;
send_9_dev=~P1;
mcu_send_9[9]=send_9_dev;
for(i=2;i<10;i++)
{
checksum=checksum+mcu_send_9[i];
}
checksum=checksum%256;
mcu_send_9[10]=checksum;
Usart_SendArrang(mcu_send_9,11); //mcu主动上报设备状态
break;
}
case 0x06:
break;
case 0x16:
break;
}
usrtlen=0;
usarrtflag=0;
REN=1;
}
}
/*按键配置设备入网*/
void Key_Usart_Config()
{
uchar i;
checksum=0;
if(key_config_Ap==0) //SoftAp配置模式
{
Delay_ms(10);
if(key_config_Ap==0)
{
REN=0;
sn=0x00;
mcu_send_3[5]=sn;
for(i=2;i<9;i++)
{
checksum=checksum+mcu_send_3[i];
}
checksum=checksum%256;
// mcu_send_3[9]=checksum;
// send_3_config=1;
// mcu_send_3[8]=send_3_config;
Usart_SendArrang(mcu_send_3,10);
while(key_config_Ap==0);
REN=1;
}
}
if(key_config_Air==0) //AirLink配置模式
{
Delay_ms(100);
Delay_ms(100);
if(key_config_Air==0)
{
REN=0;
sn=0x00;
mcu_send_3[5]=sn;
for(i=2;i<9;i++)
{
checksum=checksum+mcu_send_3[i];
}
checksum=checksum%256;
mcu_send_3[9]=checksum;
// send_3_config=2;
// mcu_send_3[8]=send_3_config;
Usart_SendArrang(mcu_send_3,10);
while(key_config_Air==0);
REN=1;
}
}
}
void main()
{
uart_int();
while(1)
{
//定时器1:1S
if(mainclock==30)
{
mainsec = 1;
mainclock= 0;
}
switch(usartbuf[9])
{
case 0x01:
if(usartbuf[10]==0x01)
{
lcd0=0;
}
else if(usartbuf[10]==0)
{
lcd0=1;
}
break;
default:
break;
}
Key_Usart_Config();
Usart_Communication();
}
}
//定时器中断
void time0_int(void) interrupt 1 // 串行口1中断函数
{
mainclock++;
TL0 = 0xB0; //设置定时初值
TH0 = 0x3C; //设置定时重载值
}
//串口中断
void UART1(void) interrupt 4//串口中断接收函数
{
mainclock=0;
ET0=1;
usartbuf[usrtlen++]=SBUF;
if(usrtlen==2)
{
if(usartbuf[0]!=0xff||usartbuf[1]!=0xff)
{
usrtlen=0;
}
}
RI=0;
}
|
|