WIFI EDP协议的点灯和上传温湿度
这个程序我自己改的是利用双串口例程EDP协议的例程改成的
单片机源程序如下:
#define MAIN_Fosc 22118400L //定义主时钟#include "STC15Fxxxx.H" #include <string.h> #define Baudrate1 115200UL #define Baudrate2 115200UL bit B_TX1_Busy; //发送忙标志 bit B_TX2_Busy; //发送忙标志 void UART1_config(u8 brt); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率. void UART2_config(u8 brt); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效. void PrintString1(u8 *puts); void PrintString2(u8 *puts); bit Haertbeat_Flag; bit Haertbeat_Detection; u8 Rec_Char; void PrintString2(u8 *puts); void PrintString_Text(u8 *puts); void wifi_Inti(); void Delay10000ms() ; void Delay3000ms(); void RH(void); void COM(void); void Delay18ms(); void Delay_10us() ; void PrintString_Data(u8 *puts); void Up_temperature(); void Up_humidity(); //----------------定义区--------------------// //----------------------------------------------// u8 U8FLAG; u8 U8count,U8temp; u8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata; u8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp; u8 U8comdata; u8 disbuf_h[]={0,0}; u8 disbuf_t[]={0,0}; u8 code temperature_Data1[]={0X80,0X15,0X00,0X05,0X00,0X11,0X2C,0X3B,0X74,0X65,0X6D,0X70,0X65,0X72,0X61,0X74,0X75,0X72,0X65,0X20,0X2C,0xff}; u8 code humidity_Data2[]={0X80,0X12,0X00,0X05,0X00,0X0E,0X2C,0X3B,0X68,0X75,0X6D,0X69,0X64,0X69,0X74,0X79,0X20,0X2C,0xff}; bit Haertbeat_Flag; bit Haertbeat_Detection; u8 Rec_Char; u8 Rec_Num; u8 Com_Turn_Light ; u8 Rec_Fax_Text[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; unsigned char code Heartbeat[]={0xC0,0x00,0xff}; u8 code Connect_Com[]={0X10,0X32,0X00,0X03,0X45,0X44,0X50,0X01,0X40,0X01,0X2C,0X00,0X09,0X35,0X31,0X38,0X39,0X38,0X38,0X32,0X37, 0X31,0X00,0X1C,0X63,0X52,0X6E,0X67,0X4F,0X50,0X4B,0X3D,0X33,0X6C,0X4D,0X65,0X78,0X32,0X41,0X75,0X45,0X69,0X32,0X41,0X37,0X63,0X6D,0X58,0X58,0X38,0X63,0X3D,0xff}; u8 code Save_Data[]={0X80,0X63,0X00,0X01,0X00,0X5F,0X7B,0X0A,0X09,0X22,0X64,0X61,0X74,0X61,0X73,0X74,0X72,0X65,0X61,0X6D,0X73,0X22,0X3A,0X09,0X5B,0X7B,0X0A,0X09,0X09,0X09, 0X22,0X69,0X64,0X22,0X3A,0X09,0X22,0X74,0X65,0X6D,0X70,0X65,0X72,0X61,0X74,0X75,0X72,0X65,0X22,0X2C,0X0A,0X09,0X09,0X09,0X22,0X64,0X61,0X74,0X61,0X70,0X6F,0X69,0X6E, 0X74, 0X73,0X22,0X3A, 0X09,0X5B,0X7B,0X0A,0X09,0X09,0X09,0X09,0X09,0X22,0X76,0X61,0X6C,0X75,0X65,0X22,0X3A,0X09,0X38,0X30,0X0A,0X09,0X09,0X09,0X09,0X7D,0X5D,0X0A,0X09,0X09,0X7D,0X5D,0X0A,0X7D,0xff}; void Haertbeat_Response() { unsigned char j; for(j=0;j<3;j++) { PrintString_Text(Heartbeat); Delay3000ms(); Delay3000ms(); if( Haertbeat_Flag == 1) break; } if( Haertbeat_Flag == 1) Haertbeat_Flag = 0; else { wifi_Inti(); Delay3000ms(); PrintString_Text(Connect_Com); Delay3000ms(); } } //======================================================================== // 函数: void main(void) // 描述: 主函数。 // 参数: none. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== int main(void) { P0M1 = 0; P0M0 = 0; //设置为准双向口 P1M1 = 0; P1M0 = 0; //设置为准双向口 P2M1 = 0; P2M0 = 0; //设置为准双向口 P3M1 = 0; P3M0 = 0; //设置为准双向口 P4M1 = 0; P4M0 = 0; //设置为准双向口 P5M1 = 0; P5M0 = 0; //设置为准双向口 P6M1 = 0; P6M0 = 0; //设置为准双向口 P7M1 = 0; P7M0 = 0; //设置为准双向口 UART1_config(1); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率. UART2_config(2); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效. EA = 1; //允许全局中断 wifi_Inti(); PrintString_Text(Connect_Com); // Delay10000ms(); // PrintString_Text(Save_Data); Delay3000ms(); while(1) { Haertbeat_Response(); Delay18ms(); RH(); Delay_10us() ; Up_temperature(); Delay_10us() ; Up_humidity(); Delay_10us() ; } } //======================================================================== // 函数: void PrintString1(u8 *puts) // 描述: 串口1发送字符串函数。 // 参数: puts: 字符串指针. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void PrintString1(u8 *puts) { for (; *puts != 0; puts++) //遇到停止符0结束 { SBUF = *puts; B_TX1_Busy = 1; while(B_TX1_Busy); } } //======================================================================== // 函数: void PrintString2(u8 *puts) // 描述: 串口2发送字符串函数。 // 参数: puts: 字符串指针. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void PrintString2(u8 *puts) { for (;*puts != 0;puts++) //遇到停止符0结束 { S2BUF = *puts; B_TX2_Busy = 1; while(B_TX2_Busy); } } void PrintString_Text(u8 *puts) { for (;*puts != 0xff;puts++) //遇到停止符0结束 { S2BUF = *puts; B_TX2_Busy = 1; while(B_TX2_Busy); } } void PrintString_Data(u8 *puts) { for (;*puts != 0xff;puts++) //遇到停止符0结束 { S2BUF = *puts; B_TX2_Busy = 1; while(B_TX2_Busy); } } //======================================================================== // 函数: SetTimer2Baudraye(u16 dat) // 描述: 设置Timer2做波特率发生器。 // 参数: dat: Timer2的重装值. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void SetTimer2Baudraye(u16 dat) { AUXR &= ~(1<<4); //Timer stop AUXR &= ~(1<<3); //Timer2 set As Timer AUXR |= (1<<2); //Timer2 set as 1T mode TH2 = dat / 256; TL2 = dat % 256; IE2 &= ~(1<<2); //禁止中断 AUXR |= (1<<4); //Timer run enable } //======================================================================== // 函数: void UART1_config(u8 brt) // 描述: UART1初始化函数。 // 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void UART1_config(u8 brt) { /*********** 波特率使用定时器2 *****************/ if(brt == 2) { AUXR |= 0x01; //S1 BRT Use Timer2; SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate1); } /*********** 波特率使用定时器1 *****************/ else { TR1 = 0; AUXR &= ~0x01; //S1 BRT Use Timer1; AUXR |= (1<<6); //Timer1 set as 1T mode TMOD &= ~(1<<6); //Timer1 set As Timer TMOD &= ~0x30; //Timer1_16bitAutoReload; TH1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) / 256); TL1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) % 256); ET1 = 0; //禁止中断 INT_CLKO &= ~0x02; //不输出时钟 TR1 = 1; } /*************************************************/ SCON = (SCON & 0x3f) | 0x40; //UART1模式, 0x00: 同步移位输出, 0x40: 8位数据,可变波特率, 0x80: 9位数据,固定波特率, 0xc0: 9位数据,可变波特率 // PS = 1; //高优先级中断 ES = 1; //允许中断 REN = 1; //允许接收 P_SW1 &= 0x3f; //P_SW1 |= 0x00; //P_SW1 |= 0x80; //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7 (必须使用内部时钟) // PCON2 |= (1<<4); //内部短路RXD与TXD, 做中继, ENABLE,DISABLE B_TX1_Busy = 0; } //======================================================================== // 函数: void UART2_config(u8 brt) // 描述: UART2初始化函数。 // 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void UART2_config(u8 brt) // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效. { if(brt == 2) { SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate2); S2CON &= ~(1<<7); // 8位数据, 1位起始位, 1位停止位, 无校验 IE2 |= 1; //允许中断 S2CON |= (1<<4); //允许接收 P_SW2 &= ~0x01; //P_SW2 |= 1; //UART2 switch to: 0: P1.0 P1.1, 1: P4.6 P4.7 B_TX2_Busy = 0; } } //======================================================================== // 函数: void UART1_int (void) interrupt UART1_VECTOR // 描述: UART1中断函数。 // 参数: nine. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void UART1_int (void) interrupt UART1_VECTOR { if(RI) { RI = 0; } if(TI) { TI = 0; B_TX1_Busy = 0; } } //======================================================================== // 函数: void UART2_int (void) interrupt UART2_VECTOR // 描述: UART2中断函数。 // 参数: nine. // 返回: none. // 版本: VER1.0 // 日期: 2014-11-28 // 备注: //======================================================================== void UART2_int (void) interrupt UART2_VECTOR { if((S2CON & 1) != 0) { S2CON &= ~1; //Clear Rx flag Rec_Char = S2BUF; SBUF = Rec_Char; //SBUF = S2BUF; } if(Rec_Char == 0xD0) { Haertbeat_Detection = 1; } else { if ((Rec_Char == 0x00) && (Haertbeat_Detection == 1)) { Haertbeat_Flag=1; } else Haertbeat_Detection = 0; Rec_Fax_Text[Rec_Num] = Rec_Char; if (Rec_Char=='{') { Com_Turn_Light = 1; } if (Com_Turn_Light == 1) { if (Rec_Fax_Text[Rec_Num] == '}') { Com_Turn_Light = 0; Rec_Num=0; if(strstr((const char*)Rec_Fax_Text,"LED01")!=NULL) { P00 = 0; } else if (strstr((const char*)Rec_Fax_Text,"LED00")!=NULL) { P00 = 1; } } else Rec_Num++; } } if((S2CON & 2) !=0) { S2CON &= ~2; //Clear Tx flag B_TX2_Busy = 0; } } //======================================================================== // 函数: // 备注: //======================================================================== void wifi_Inti() { PrintString2("+++"); Delay10000ms(); Delay10000ms(); PrintString2("AT\r\n"); Delay10000ms(); PrintString2("AT+CWMODE=3\r\n"); Delay10000ms(); PrintString2("AT+RST\r\n"); Delay10000ms(); PrintString2("AT+CWJAP="QQ_MI","12345678"\r\n"); // PrintString2("AT+CWJAP="iPhoneSE","12345678"\r\n"); Delay10000ms(); Delay10000ms(); Delay10000ms(); Delay10000ms(); Delay10000ms(); Delay10000ms(); PrintString2("AT+CIPSTART="TCP","183.230.40.39",876\r\n"); Delay10000ms(); Delay10000ms(); PrintString2("AT+CIPMODE=1\r\n"); Delay10000ms(); PrintString2("AT+CIPSEND\r\n"); Delay10000ms(); } //======================================================================== // 函数: // 备注: //======================================================================== void Delay10000ms() //@11.0592MHz { unsigned char i, j, k; _nop_(); _nop_(); i = 165; j = 59; k = 28; do { do { while (--k); } while (--j); } while (--i); } void Delay3000ms() //@11.0592MHz { unsigned char i, j, k; _nop_(); _nop_(); i = 127; j = 18; k = 107; do { do { while (--k); } while (--j); } while (--i); } void Delay_10us() //@22.1184MHz { unsigned char i; _nop_(); _nop_(); _nop_(); i = 52; while (--i); } void Delay18ms() //@22.1184MHz { unsigned char i, j, k; _nop_(); i = 2; j = 132; k = 67; do { do { while (--k); } while (--j); } while (--i); } //======================================================================== // 函数: // 备注: //======================================================================== void COM(void) { u8 i; for(i=0;i<8;i++) { U8FLAG=2; while((!P20)&&U8FLAG++); Delay_10us(); Delay_10us(); Delay_10us(); U8temp=0; if(P20)U8temp=1; U8FLAG=2; while((P20)&&U8FLAG++); //超时则跳出for循环 if(U8FLAG==1)break; //判断数据位是0还是1 //如果高电平高过预定0高电平值则数据位为 1 U8comdata<<=1; U8comdata|=U8temp; //0 }//rofc } //======================================================================== // 函数: // 备注: //======================================================================== void RH(void) { //主机拉低18ms P20=0; Delay18ms(); P20=1; //总线由上拉电阻拉高 主机延时20us Delay_10us(); Delay_10us(); Delay_10us(); Delay_10us(); //主机设为输入 判断从机响应信号 P20=1; //判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行 if(!P20) //T ! { U8FLAG=2; //判断从机是否发出 80us 的低电平响应信号是否结束 while((!P20)&&U8FLAG++); U8FLAG=2; //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态 while((P20)&&U8FLAG++); //数据接收状态 COM(); U8RH_data_H_temp=U8comdata; COM(); U8RH_data_L_temp=U8comdata; COM(); U8T_data_H_temp=U8comdata; COM(); U8T_data_L_temp=U8comdata; COM(); U8checkdata_temp=U8comdata; P20=1; //数据校验 U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp); if(U8temp==U8checkdata_temp) { U8RH_data_H=U8RH_data_H_temp; U8RH_data_L=U8RH_data_L_temp; U8T_data_H=U8T_data_H_temp; U8T_data_L=U8T_data_L_temp; U8checkdata=U8checkdata_temp; disbuf_h[0]=U8RH_data_H/10+0x30; disbuf_h[1]=U8RH_data_H%10+0x30; disbuf_t[0]=U8T_data_H/10+0x30; disbuf_t[1]=U8T_data_H%10+0x30; }//fi }//fi } //======================================================================== // 函数: // 备注: (u8 *puts) //======================================================================== void Up_humidity() { u8 i; PrintString_Data(humidity_Data2); for (i=0;i<2;i++) { S2BUF=disbuf_h[i]; B_TX2_Busy = 1; while(B_TX2_Busy); } } //======================================================================== // 函数: // 备注: (u8 *puts) //======================================================================== void Up_temperature() { u8 i; PrintString_Data(temperature_Data1); for (i=0;i<2;i++) { S2BUF=disbuf_t[i]; B_TX2_Busy = 1; while(B_TX2_Busy); } }
复制代码