标题: 基于单片机无线控制小车设计 [打印本页]
作者: 翁珹 时间: 2019-5-11 11:33
标题: 基于单片机无线控制小车设计
在综合考量了单片机的使用性能、成本和时间等问题,本次课题主要旨在实现以下性能指标:
1)根据力学结构完成小车整体框架的搭建,稳固其整体重心;
2)完成底层运动系统的搭建,保证小车能够正常行进;
3)完成手机遥控功能,通过手机控制底层小车的工作方式。
整体小车车身采用学校提供基本车身框架材料,通过自己的设计和搭建,完成底层硬件系统的搭载,再通过上层安卓手机APP遥控,通过无线蓝牙通信协议,实现小车能够遵从指令实现在室内的自主避障的巡航功能与手机遥控功能
#include <reg52.h>
sbit PWM1 = P2^0;//PWM波产生的端口
sbit PWM2 = P2^1;
sbit motor_control_1 = P2^7;//左轮前进
sbit motor_control_2 = P2^6;//左轮后退
sbit motor_control_3 = P2^5;//右轮前进
sbit motor_control_4 = P2^4;//右轮后退
sbit LED0 = P0^0;//前灯
sbit LED1 = P0^1;//左后灯
sbit LED2 = P0^2;//右后灯
unsigned int PWMCnt1 = 0;
unsigned int cntPWM1 = 60;
unsigned int PWMCnt2 = 0;
unsigned int cntPWM2 = 60;
unsigned char bluetoothdata;
void initial_myself();
void initial_interrupt();
void usart_service(void);
void delay_long(unsigned int time);
void go_forward(void);//
void stop();
void turn_right();
void turn_left();
void back();
void main()
{
initial_myself();
delay_long(100);
initial_interrupt();
stop();
while(1)
{
LED1=1;LED2=1;
usart_service();
}
}
void usart_service()
{
switch(bluetoothdata)
{
case 'f':go_forward();
delay_long(100);
SBUF = 'f';//返回数据到手机
bluetoothdata = 'a';
break;
case 's':stop();
delay_long(100);
SBUF = 's';
bluetoothdata = 'a';
break;
case 'r':turn_right();
delay_long(100);
SBUF = 'r';
bluetoothdata = 'a';
break;
case 'l':turn_left();
delay_long(100);
SBUF = 'l';
bluetoothdata = 'a';
break;
case 'b':back();
delay_long(100);
SBUF = 'b';
bluetoothdata = 'a';
break;
case '1':cntPWM1 = 60;
cntPWM2 = 60;
delay_long(100);
SBUF = '1';
bluetoothdata = 'a';
break;
case '2':cntPWM1 = 100;
cntPWM2 = 100;
delay_long(100);
SBUF = '2';
bluetoothdata = 'a';
break;
case '3':cntPWM1 = 140;
cntPWM2 = 140;
delay_long(100);
SBUF = '3';
bluetoothdata = 'a';
break;
case '4':LED0=0;
delay_long(100);
SBUF = '4';
bluetoothdata = 'a';
break;
case '5':LED0=1;
delay_long(100);
SBUF = '5';
bluetoothdata = 'a';
break;
}
}
void usart_receive(void) interrupt 4 //串口中断程序
{
if(RI == 1)//收到字符
{
RI = 0;//软件清零
bluetoothdata = SBUF;//读取数据
}
if(TI == 1)//发送数据
{
TI = 0;//清零
}
}
void T0_time() interrupt 1 //定时器0中断程序
{
PWMCnt1++;
PWMCnt2++;
if(PWMCnt1 >= 200)
{
PWMCnt1 = 1;
}
if(PWMCnt1 <= cntPWM1) //230
{
PWM1 = 1;
}
else
{
PWM1 = 0;
}
if(PWMCnt2 >= 200)
{
PWMCnt2 = 1;
}
if(PWMCnt1 <= cntPWM2) //230
{
PWM2 = 1;
}
else
{
PWM2 = 0;
}
TH0 = (65536 - 50) / 256;
TL0 = (65536 - 50) % 256;
}
void initial_myself()
{
TMOD = 0x01;//
TH0 = (65536 - 50) / 256;//定时50微秒
TL0 = (65536 - 50) % 256;
SCON = 0x50;
TMOD = 0x21;
TH1 = TL1 = 0xfd;//设置波特率9600
IP = 0x10;//设置串口中断为高优先级
}
void initial_interrupt()
{
EA = 1;// 开总中断
ES = 1;// 开串口中断
ET0 = 1;//开定时器0中断?
TR0 = 1;//开定时器0
TR1 = 1;// 开定时器1
}
void delay_long(unsigned int time)
{
unsigned int i;
unsigned int j;
for(i = 0 ; i < time ; i++)
{
for(j = 0; j < 500; j++);
}
}
void go_forward()
{
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void stop()
{
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 0;
}
void turn_left()
{
int i=6000;
for(;i>=0;i--)
{
if(i)
{
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 0;
LED1=0;
}
else
{
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 0;
}
}
LED1=1;
}
void turn_right()
{
int i=6000;
for(;i>=0;i--)
{
if(i)
{
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 1;
LED2=1;
}
else
{
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 0;
}
}
LED2=1;
}
void back()
{
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
LED1=0;LED2=0;
}
-
-
蓝牙小车.rar
24.16 KB, 下载次数: 9, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |