1.前期准备 准备好杜邦线、HC-06蓝牙模块、单片机(我用的是粤嵌的51单片机,自己做一个最小系统也行)、小车底盘、电源、L298N、螺柱(固定零件用的)。如果想做的话请自己买这些零件,做出来的效果图片如下(我加了寻迹模块)。
2.开工
///8个LED灯
sbit led1 = P1^0;
sbit led2 = P1^1;
sbit led3 = P1^2;
sbit led4 = P1^3;
sbit led5 = P1^4;
sbit led6 = P1^5;
sbit led7 = P1^6;
sbit led8 = P1^7;
sbit bell = P0^7;
//电机引脚
sbit La = P0^0;
sbit Lb = P0^1;
sbit Ra = P0^2;
sbit Rb = P0^3; 2.用到的主要知识就是中断了。我本来想做一个寻迹和蓝牙控制一体的小车(但是寻迹并没有做好,所以这里就只给大家写蓝牙模块怎么实现了),想了想当寻迹模式立即转到蓝牙控制模式时(或者蓝牙模式立即转换成寻迹模式)好像也只有中断可以,所以就加了个中断来扫描看有没有收到转换信号。我的代码如下。 include <reg52.h> sbit led1 = P1^0;
sbit led2 = P1^1;
sbit led3 = P1^2;
sbit led4 = P1^3;
sbit led5 = P1^4;
sbit led6 = P1^5;
sbit led7 = P1^6;
sbit led8 = P1^7;
sbit bell = P0^7;
sbit K1 = P3^3;
sbit K2 = P3^4;
sbit K3 = P3^6;
sbit K4 = P3^7;
sbit La = P0^0;
sbit Lb = P0^1;
sbit Ra = P0^2;
sbit Rb = P0^3;
sbit right = P0^4;
sbit left = P0^5;
sbit down_right = P0^6;
sbit down_left = P2^7; unsigned char a = 0;
unsigned char num = 0;
unsigned char temp = 0; bit auto_run = 0;
bit lanya_run = 1; void uart_init();
void delay(unsigned int time);
void go_up();
void go_down();
void turn_left();
void turn_right();
void bell_on();
void bell_off();
void led_all_on();
void led_all_off();
void go_around_shun();
void go_around_li();
void stop();
void change_name();
void init_T0(); void main() {
uart_init();
init_T0();
change_name();
while(1) {
if(lanya_run == 1) {
while(1) {
if(lanya_run != 1) {
break;
}
if (num != 0) {
ES = 0;
num = 0;
SBUF = a;
while (!TI);
TI = 0;
ES = 1;
}
switch (a)
{
case '0': go_up(); bell_on(); break;
case '1': go_down(); bell_on(); break;
case '2': turn_left(); bell_on(); break;
case '3': turn_right(); bell_on(); break;
case '5': go_around_shun(); bell_on(); break;
case '6': led_all_on(); break;
case '7': led_all_off(); break;
case '8': go_around_li(); bell_on(); break;
default: stop(); bell_off(); break;
}
}
}
if(auto_run == 1) {
while(1) {
if(auto_run != 1) {
break;
}
if (num != 0) {
ES = 0;
num = 0;
SBUF = a;
while (!TI);
TI = 0;
ES = 1;
}
go_up();
if (!left && right) {
go_around_shun();
bell_on();
delay(50);
bell_off();
delay(1000);
}
if (!right && left) {
go_around_li();
bell_on();
delay(50);
bell_off();
delay(1000);
}
if(!right && !left) {
go_down();
bell_on();
delay(50);
bell_off();
delay(1000);
}
if(left && right) {
go_up();
delay(100);
}
}
}
}
} void uart_fun() interrupt 4 {
RI = 0;
a = SBUF;
num++;
} void T0Int() interrupt 1 {
TH0 = 0xD8;
TL0 = 0xF0; temp++; if(temp == 10) {
temp = 0;
if(a == '9' && lanya_run == 1) {
lanya_run = 0;
auto_run = 1;
}
if(a == '9' && lanya_run == 0) {
lanya_run = 1;
auto_run = 0;
}
}
} void uart_init() {
TMOD = 0x20; //设置定时器设置
TH1 = 0xFD; //设置初始波特率9600
TL1 = 0xFD; //
TR1 = 1; //开启定时器1
SM0 = 0; //设置串行工作方式
SM1 = 1; //
REN = 1; //允许传兴接受位
EA = 1; //开启总中断
ES = 1; //开启串口中断口
} void delay(unsigned int time) {
int i = 121;
while(time--)
while(i--);
} void stop()
{
La = 0;
Lb = 0;
Ra = 0;
Rb = 0;
} void go_around_shun() {
La = 1;
Lb = 0;
Ra = 0;
Rb = 1;
} void go_around_li() {
La = 0;
Lb = 1;
Ra = 1;
Rb = 0;
} void go_down() {
La = 0;
Lb = 1;
Ra = 0;
Rb = 1;
} void go_up() {
La = 1;
Lb = 0;
Ra = 1;
Rb = 0;
} void turn_right() {
La = 1;
Lb = 0;
Ra = 1;
Rb = 0;
delay(5);
Ra = 0;
delay(5);
} void turn_left() {
Ra = 1;
Rb = 0;
La = 1;
Lb = 0;
delay(5);
La = 0;
delay(5);
} void change_name() {
char name[] = "AT+NAME=StarSea\r\n";
char i;
for(i = 0; i < sizeof(name); i++) {
SBUF = name[ i];
while(!TI);
TI = 0;
}
delay(500);
} [ i]void bell_on() {
bell = 0;
} void bell_off() {
bell = 1;
} void led_all_on() {
P0 = 0x00;
} [ i]void led_all_off() {
P0 = 0x11;
} void init_T0() {
TMOD = 0x21;
TH0 = 0xD8;
TL0 = 0xF0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
- 把程序烧入单片机,再把收到的小车底盘零件拼起来,其他配件用杜邦线和对应引脚连接起来。
- 下载蓝牙串口这个程序。以编辑前进按钮为例:我的代码中,收到字符0就前进收到字符4就停下。所以这里按下就发送0,松开就发送4。我的所有按钮如图(自动驾驶就是寻迹,但是没有做好)。
- 通过蓝牙串口连接HC-05(默认的蓝牙名字)即可开始用自制的蓝牙小车了。
|