51单片机WiFi遥控小车是利用手机作为控制端,通过WIFI模块与单片机串口通信,实现小车的前进、后退、左拐、右拐停止等功能。
下载:
51单片机WiFi遥控小车及APP.rar
(180.02 KB, 下载次数: 173)
单片机源码:
- #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; }
复制代码
|