51單片機WiFi遙控小車程序
單片機源程序如下:
- /********************************************************************/
- #include <REGX51.H>
- #define uint unsigned int
- #define uchar unsigned char
- /*****************相關變量**************/
- uchar Receive,i,qj,yz,zz,ht;
- uint n;
- uchar Recive_table[40]; //用于接收wifi模塊反饋到MCU上的數據
- /*******************************************************************
- 名稱:延時函數 作用:毫秒級延時,微妙級延時函數,為數據收發完成作等待.......
- ********************************************************************/
- void ms_delay(uint t)
- {
- uint i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- void us_delay(uchar t)
- {
- while(t--);
- }
- void Uart_Init() //使用定時器1作為波特率發生器(STC89C52、STC89C51、AT89C51等均可)
- {
- TMOD = 0x20;
- SCON = 0x50; //設置串行方式
- TH1 = 0xFD; //波特率9600
- TL1 = TH1;
- PCON = 0x00;
- EA = 1; //總中斷打開
- ES = 1; //開串口中斷
- TR1 = 1; //啟動定時器1
- }
- /********************************************************************
- 名稱:串口發送函數 功能:MCU向無線WIFI模塊ESP8266發送數據
- ********************************************************************/
- void Send_Uart(uchar value)
- {
- ES=0; //關閉串口中斷
- TI=0; //清發送完畢中斷請求標志位
- SBUF=value; //發送
- while(TI==0); //等待發送完畢
- TI=0; //清發送完畢中斷請求標志位
- ES=1; //允許串口中斷
- }
- /********************************************************************
- 名稱:WIFI模塊設置函數 作用: 啟動模塊,以便可以實現無線接入和控制
- ********************************************************************/
- void ESP8266_Set(uchar *puf) // 數組指針*puf指向字符串數組
- {
- while(*puf!='\0') //遇到空格跳出循環
- {
- Send_Uart(*puf); //向WIFI模塊發送控制指令。
- us_delay(5);
- puf++;
- }
- us_delay(5);
- Send_Uart('\r'); //回車
- us_delay(5);
- Send_Uart('\n'); //換行
- }
- /********************************************************************
- 名稱:主函數 作用:程序的執行入口
- ********************************************************************/
- void main()
- {
- Uart_Init(); //波特率發生器
- ms_delay(2000);
- ESP8266_Set("AT+CWMODE=2"); //設置路由器模式1 station,模式2 AP,模式3 station+AP混合模式
- ms_delay(2000);
- // ESP8266_Set("AT+RST"); //重新啟動wifi模塊
- // ms_delay(2000);
- ESP8266_Set("AT+CWSAP=\"wifi\",\"123456789\",11,4"); //AT+CWSAP="wifi","123456789",11,4 設置模塊SSID:WIFI, PWD:密碼 及安全類型加密模式(WPA2-PSK)
- ms_delay(2000);
- ESP8266_Set("AT+CIPMUX=1"); //開啟多連接模式,允許多個各客戶端接入
- ms_delay(2000);
- ESP8266_Set("AT+CIPSERVER=1,5000"); //啟動TCP/IP 實現基于網絡//控制 ESP8266_Set("AT+CIPSERVER=1,5000");
- ms_delay(2000);
- ESP8266_Set("AT+CIPSTO=0"); //永遠不超時
- ES=1; //允許串口中斷
- qj=1;
- zz=1;
- yz=1;
- ht=1;
- while(1)
- {
- if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//MCU接收到的數據為+IPD時進入判斷控制0\1來使小燈亮與滅
- {
- if((Recive_table[9]=='G')&&(Recive_table[10]=='P'))
- {
- if(Recive_table[15]=='0')
- {
- qj=0; //前進
- zz=1;
- yz=1;
- ht=1;
- }
- else
- if (Recive_table[15]=='1')
- {
- qj=1;
- zz=1;
- yz=1;
- ht=0; // 后退
- }
- else
- if (Recive_table[15]=='2')
- {
- // qj=1; //
- zz=1;
- yz=0; //右轉
- // ht=1;
- }
- else
- if (Recive_table[15]=='3')
- {
- // qj=1; //
- zz=0; //左轉
- yz=1;
- // ht=1;
- }
- else
- if (Recive_table[15]=='4')
- {
- qj=1; //
- zz=1;
- yz=1;
- ht=1;
- }
- }
- }
- if(qj==0)
- {
- if((zz==0)|(yz==0))
- {
- ;
- }
- else
- {
- P2=0x11;
- ms_delay(10);
- P2=0x22;
- ms_delay(10);
- P2=0x44;
- ms_delay(10);
- P2=0x88;
- ms_delay(10);
- }
- }
- if(ht==0)
- {
- if((zz==0)|(yz==0))
- {
- ;
- }
- else
- {
- P2=0x88;
- ms_delay(10);
- P2=0x44;
- ms_delay(10);
- P2=0x22;
- ms_delay(10);
- P2=0x11;
- ms_delay(10);
- }
- }
- if(zz==0)
- {
- if(ht==1)
- {
- P2=0x01;
- ms_delay(10);
- P2=0x02;
- ms_delay(10);
- P2=0x04;
- ms_delay(10);
- P2=0x08;
- ms_delay(10);
- }
- else
- {
- P2=0x08;
- ms_delay(10);
- P2=0x04;
- ms_delay(10);
- P2=0x02;
- ms_delay(10);
- P2=0x01;
- ms_delay(10);
- }
- }
- if(yz==0)
- {
- if(ht==1)
- {
- P2=0x10;
- ms_delay(10);
- P2=0x20;
- ms_delay(10);
- P2=0x40;
- ms_delay(10);
- P2=0x80;
- ms_delay(10);
- }
- else
- {
- P2=0x80;
- ms_delay(10);
- P2=0x40;
- ms_delay(10);
- P2=0x20;
- ms_delay(10);
- P2=0x10;
- ms_delay(10);
- }
- }
- }
- }
- /*********************************************************************
- 名稱:串行通訊中斷 作用:發送或接收結束后進入該函數,對相應的標志位軟件清0,實現模塊對數據正常的收發。
- ********************************************************************/
- void Uart_Interrupt() interrupt 4
- {
- static uchar i=0;
- if(RI==1)
- {
- RI=0;
- Receive=SBUF; //MCU接收wifi模塊反饋回來的數據
- Recive_table[i]=Receive;
- if((Recive_table[i]=='\n'))
- {
- i=0;
- }
- else i++; //遇到換行 重新裝值
- }
- else TI=0;
- }
復制代碼
所有資料51hei提供下載:
51單片機WiFi遙控小車及APP.rar
(180.02 KB, 下載次數: 190)
2018-7-25 14:59 上傳
點擊文件名下載附件
|