51單片機WiFi遙控小車是利用手機作為控制端,通過WIFI模塊與單片機串口通信,實現小車的前進、后退、左拐、右拐停止等功能。
下載:
51單片機WiFi遙控小車及APP.rar
(180.02 KB, 下載次數: 173)
2018-6-12 22:24 上傳
點擊文件名下載附件
WIFI遙控車及APP 下載積分: 黑幣 -5
遙控車_A.jpg (45.45 KB, 下載次數: 53)
下載附件
WIFI遙控車及APP
2018-6-12 22:23 上傳
遙控車及APP_B1 (1).jpg (78.24 KB, 下載次數: 55)
下載附件
WIFI遙控車及APP
2018-6-12 22:22 上傳
單片機源碼:
- #include <REGX51.H>
- #define uint unsigned int
- #define uchar unsigned char
- uchar Receive,i,qj,yz,zz,ht;
- uint n;
- uchar Recive_table[40];
- 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;
- TL1 = TH1;
- PCON = 0x00;
- EA = 1; //總中斷打開
- ES = 1; //開串口中斷
- TR1 = 1; //啟動定時器1
- }
- void Send_Uart(uchar value)
- {
- ES=0; //關閉串口中斷
- TI=0; //清發送完畢中斷請求標志位
- SBUF=value; //發送
- while(TI==0); //等待發送完畢
- TI=0; //清發送完畢中斷請求標志位
- ES=1; //允許串口中斷
- }
- void ESP8266_Set(uchar *puf) // 數組指針*puf指向字符串數組
- {
- while(*puf!='\0') //遇到空格跳出循環
- {
- Send_Uart(*puf);
- 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");
- ms_delay(2000);
- // ESP8266_Set("AT+RST");
- // ms_delay(2000);
- ESP8266_Set("AT+CWSAP=\"wifi\",\"123456789\",11,4"); //AT+CWSAP="wifi","123456789",11,4
- ms_delay(2000);
- ESP8266_Set("AT+CIPMUX=1");
- ms_delay(2000);
- 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'))
- {
- 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);
- }
- }
- }
- }
- void Uart_Interrupt() interrupt 4
- {
- static uchar i=0;
- if(RI==1)
- {
- RI=0;
- Receive=SBUF;
- Recive_table[ i]=Receive; if((Recive_table[ i]=='\n')){ i=0;} else i++; } else TI=0; }
復制代碼
|