|
51单片机WIFI模块的资料:
所有资料下载:
串口wifi模块51单片机代码.rar
(39.81 KB, 下载次数: 189)
主程序:
- /*************************************************
- Copyright (C), Shanghai xunzhan Electronic Technology Co., Ltd.
- File name: main.c
- Author: Jacky
- Mail: sscxyt_support@163.com
- Version: V1.0.0
- Description:
- Others:
- History:
- 1. Date:
- Author:
- Modification:
- 2. ...
- *************************************************/
- #include <string.h>
- #include <intrins.h>
- #include <stdio.h>
- #include "main.h"
- #include "wifi.h"
- #include "Msc51_Drive.h"
- #include "STC89C51RC_RD_PLUS.h"
- main()
- {
- HardWareInit(); // 硬件初始化
- // 设置wifi模块参数,使用的是不带应答的模式设置,没有判断模块返回,参数设置可以这样。
- // tcp数据通讯建议启用应答模式。
- Wifi_Para_set(TCP);
- u8LinkState = 0;
- printf("wifi module has linkup with a server!!");
- while(!u8LinkState) // 等待设置成功,这是死循环,用户自己可以按照要求修改
- {
- WifiLink(0); // 0是默认组号,第一组是默认组号。由于发射时候功耗大,模块快速发热,建议加上延时函数
- // delayS(60); // 比如1分钟扫描一次,直到扫描到AP.
- }
- /////// 主应用程序///////////////
- while(1)
- {
- if(gcReceComm1OK) // 从串口收到有效的wifi数据
- {
- // WIFI通讯数据处理
- ProcessWIFIData();
- gcReceComm1OK = 0;
- }
- }
- while(1) // 去掉编译未调用警告
- {
- ScanNetWork(0xffff,1);
- WIFIReset();
- WIFICloseAp(0,1);
- WIFILinkOrCloseServer(1,1);
- }
- }
- /*
- *********************************************************************************************************
- ** 函数名称 :HardWareInit(void)
- ** 函数功能 :硬件初始化
- ** 入口参数 :无
- ** 出口参数 :无
- *********************************************************************************************************
- */
- void HardWareInit(void)
- {
- RS0 = RS1 = 0;// 使用寄存器组0
- AUXR = 0x01;
- EA = 0;
- P1 = 0xFF;
- P2 = 0xFF;
- P3 = 0xFF;
- P4 = 0xFF;
- Uart_Init(); // wifi模块: 波特率19200/38400/57600/115200bps 数据位:8bits 校验位:无 停止位:1bits 流控 :无
- EA = 1;
-
- }
- /*
- *********************************************************************************************************
- ** 函数名称 :Void Wifi_Para_set(void)
- ** 函数功能 :WIFI 参数设置
- ** 入口参数 :无
- ** 出口参数 :无
- *********************************************************************************************************
- */
- void Wifi_Para_set(unsigned char u8DataType)
- {
- unsigned char parmid[23]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa};
- gcNetParaBuffer[0].M_id = WIFI_TCPIP;
- gcNetParaBuffer[0].cLength = 4;
- gcNetParaBuffer[0].cInfo[0] = 192;
- gcNetParaBuffer[0].cInfo[1] = 168;
- gcNetParaBuffer[0].cInfo[2] = 1;
- gcNetParaBuffer[0].cInfo[3] = 200;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1);
-
- gcNetParaBuffer[0].M_id = WIFI_TCPMASET;
- gcNetParaBuffer[0].cLength = 1;
- gcNetParaBuffer[0].cInfo[0] = 1; // 255,255,255,0
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1);
-
- gcNetParaBuffer[0].M_id = WIFI_TCPGATEWAY;
- gcNetParaBuffer[0].cLength = 4;
- gcNetParaBuffer[0].cInfo[0] = 192; // 192.168.1.1
- gcNetParaBuffer[0].cInfo[1] = 168;
- gcNetParaBuffer[0].cInfo[2] = 1;
- gcNetParaBuffer[0].cInfo[3] = 1;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1);
-
- gcNetParaBuffer[0].M_id = WIFI_TCPSERVERIP;
- gcNetParaBuffer[0].cLength = 4;
- gcNetParaBuffer[0].cInfo[0] = 192; // 192.168.1.20
- gcNetParaBuffer[0].cInfo[1] = 168;
- gcNetParaBuffer[0].cInfo[2] = 1;
- gcNetParaBuffer[0].cInfo[3] = 20;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1);
- gcNetParaBuffer[0].M_id = WIFI_TCPPORT;
- gcNetParaBuffer[0].cLength = 2;
- gcNetParaBuffer[0].cInfo[0] = 0x17; // 6000
- gcNetParaBuffer[0].cInfo[1] = 0x70;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1);
- gcNetParaBuffer[0].M_id = WIFI_ENCRYPTTYPE;
- gcNetParaBuffer[0].cLength = 1;
- gcNetParaBuffer[0].cInfo[0] = 1; //
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1); //
-
- gcNetParaBuffer[0].M_id = WIFI_ENKEY;
- gcNetParaBuffer[0].cLength = 64;
- gcNetParaBuffer[0].M_id = WIFI_BSSID;
- gcNetParaBuffer[0].cLength = 6;
- gcNetParaBuffer[0].cInfo[0] = 0x00; //
- gcNetParaBuffer[0].cInfo[1] = 0x1a;
- gcNetParaBuffer[0].cInfo[2] = 0x70;
- gcNetParaBuffer[0].cInfo[3] = 0x35;
- gcNetParaBuffer[0].cInfo[4] = 0xb9;
- gcNetParaBuffer[0].cInfo[5] = 0x32;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1);
- // channel
- gcNetParaBuffer[0].M_id = WIFI_CHANEL;
- gcNetParaBuffer[0].cLength = 1;
- gcNetParaBuffer[0].cInfo[0] = 6;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],0);
- // SSID // 推荐用00填充后面没有使用的空白区
- gcNetParaBuffer[0].M_id = WIFI_SSID;
- gcNetParaBuffer[0].cLength = 33;
- gcNetParaBuffer[0].cInfo[0] = 'H';
- gcNetParaBuffer[0].cInfo[1] = 'L';
- gcNetParaBuffer[0].cInfo[2] = 'K';
- memset(&gcNetParaBuffer[0].cInfo[2],0,30);
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],0); // 加载在第一组参数组号中
-
- // 数据类型 tcp udp raw
- gcNetParaBuffer[0].M_id = WIFI_DATATYPE;
- gcNetParaBuffer[0].cLength = 1;
- gcNetParaBuffer[0].cInfo[0] = u8DataType;
- WIFISetNetPara(0xb1,&gcNetParaBuffer[0],0); // 加载在第一组参数组号中
-
- // tcp 使能监听,可以做服务端器使用,可以做到双向随时发起通讯
- if(u8DataType == 2)
- {
- gcNetParaBuffer[0].M_id = WIFI_TCPMONITOR;
- gcNetParaBuffer[0].cLength = 1;
- gcNetParaBuffer[0].cInfo[0] = 1;
- WIFISetSysPara(&gcNetParaBuffer[0],0);
- ///WIFISetNetPara(0xb1,&gcNetParaBuffer[0],1); // 加载在第一组参数组号中
- }
- WifiSendAck();
- WifiSendNomalData(parmid,23,1);
-
- {
- ScanNetWork(0x3fff,1);
- WIFIRequerNetPara(0xb1,23,parmid,1);
- }
- }
- void WifiLink(unsigned char u8LinkGrup)
- {
- unsigned long u32TimeOut;
-
- u8LinkState = 0;
- WIFIJoinAp(u8LinkGrup,1);
- while( (gcReceComm1OK != 0) || (u32TimeOut < 0x2ffffff) );
- {
- if(gcReceComm1OK)
- {
- if( (ATReceBuffer[0].gcATReceData[5] == LINKNET_END) && (ATReceBuffer[0].gcATReceData[6] == 0) )
- {
- u8LinkState = 1;
- }
- }
- }
- }
复制代码
|
评分
-
查看全部评分
|