這是借鑒的一篇程序
0.png (47.35 KB, 下載次數: 20)
下載附件
2017-7-31 16:37 上傳
單片機源程序如下:
- /*************************************************
- Copyright (C), Shanghai xunzhan Electronic Technology Co., Ltd.
- File name: main.c
- Author: Jacky
- Mail: sscxyt_support@163.com
- Version: V1.0.0
- Date: 2010-01-26
- 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);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
串口wifi模塊51單片機代碼.rar
(39.81 KB, 下載次數: 10)
2017-7-31 10:30 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|