本人一直想做一个遥控车,终于今天实现了,跟大家分享一下。
一、元件清单
1、某宝购买一个智能小车底盘(本人车是四驱的,因此有四个电机,两块亚克力板和一些配件螺丝)
2、L298n模块 两个(一个298控制两个电机)
3、HC-06蓝牙模块
4、电池盒跟18650电池3个
5、单片机最小系统
6、杜邦线至少12根,当然越多越好
7、若干根导线(连接电机用的,以及电池盒连到L298N跟单片机)
二、制作过程
首先,先去了解一下L298N的接线,我当时也是懵逼了好久,反正摸索着也会了,一般店家都有资料。然后是蓝牙模块的使用,拿到蓝牙模块后,有开发板的话(没有就用单片机的烧录器),先按照TXD接TXD,RXD接RXD,打开电脑的串口助手与蓝牙构建通信,打开AT,发送AT指令集,比如设设波特率,改个名字,改个配对的密码。但通过蓝牙与单片机连接通信的时候,就是TXD接RXD了。最后一个就是,本人在接线的时候经常碰壁。比如,你以为接好的线他其实没接好,根本没通,还是拿万用表B了才知道。
因为本人忘了买电池盒,所以我是用12V的直流电接着的,导致遥控车拖着跟电线。
三、实物图
(拖着跟电线以及充电宝供电的车 不过我没通电)
资料内包括L298N的部分资料,以及蓝牙的说明书(看AT指令集就好)和源程序。
app:
单片机源程序如下:
- /*手机蓝牙遥控小车 APP可以在应用市场搜 蓝牙串口助手
- 左转右转大概是90度 我的是四驱的,如果是只有一个l298N的话就把带2的删了
- pwm有十级变速*/
- #include <reg52.h>
- #define Left_moto2_pwm P0_4 //接驱动模块ENA 使能端,输入PWM信号调节速度 左后轮
- #define Right_moto2_pwm P0_5 //接驱动模块ENB 右后轮
- #define Left_moto_pwm P1_4 //接驱动模块ENA 使能端,输入PWM信号调节速度 左前轮
- #define Right_moto_pwm P1_5 //接驱动模块ENB 右前轮
- #define uchar unsigned char
- #define uint unsigned int
- sbit P0_4=P0^4; //定义P0_4
- sbit P0_5=P0^5; //定义P0_5
- sbit P1_4=P1^4; //定义P1_4
- sbit P1_5=P1^5; //定义P1_5
- /*电机驱动IO定义*/
- sbit IN1 = P1^2; //为1 左电机反转 前轮
- sbit IN2 = P1^3; //为1 左电机正转 前轮
- sbit IN3 = P1^6; //为1 右电机正转 前轮
- sbit IN4 = P1^7; //为1 右电机反转 前轮
- /*sbit EN1 = P1^4; //为1 左电机使能
- sbit EN2 = P1^5; //为1 右电机使能 */
- sbit IN5 = P0^2; //为1 左电机反转 后轮
- sbit IN6 = P0^3; //为1 左电机正转 后轮
- sbit IN7 = P0^6; //为1 右电机正转 后轮
- sbit IN8 = P0^7; //为1 右电机反转 后轮
- /*sbit EN3 = P0^4; //为1 左电机使能 后轮
- sbit EN4 = P0^5; //为1 右电机使能 后轮 */
- bit Right_moto_stop=1;
- bit Left_moto_stop =1;
- unsigned int time=0;
- int pwm=1;
- #define left_motor_en EN1 = 1 //左电机使能
- #define left_motor_stops EN1 = 0 //左电机停止
- #define right_motor_en EN2 = 1 //右电机使能
- #define right_motor_stops EN2 = 0 //右电机停止
- #define left_motor2_en EN3 = 1 //左电机使能 后
- #define left_motor2_stops EN3 = 0 //左电机停止 后
- #define right_motor2_en EN4 = 1 //右电机使能 后
- #define right_motor2_stops EN4 = 0 //右电机停止 后
- #define left_motor_go IN1 = 0, IN2 = 1//左电机正传
- #define left_motor_back IN1 = 1, IN2 = 0//左电机反转
- #define right_motor_go IN3 = 1, IN4 = 0//右电机正传
- #define right_motor_back IN3 = 0, IN4 = 1//右电机反转
- #define left_motor2_go IN5 = 0, IN6 = 1//左电机正传
- #define left_motor2_back IN5 = 1, IN6 = 0//左电机反转
- #define right_motor2_go IN7 = 1, IN8 = 0//右电机正传
- #define right_motor2_back IN7 = 0, IN8 = 1//右电机反转
- unsigned char pwm_val_left =0;//变量定义
- unsigned char push_val_left =0;// 左电机占空比N/10
- unsigned char pwm_val_right =0;
- unsigned char push_val_right=0;// 右电机占空比N/10
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
- //蓝牙初始化
- void UART_INIT()
- {
- SM0 = 0;
- SM1 = 1;//串口工作方式1
- REN = 1;//允许串口接收
- EA = 1;//开总中断
- ES = 1;//开串口中断
- TMOD = 0x20;//8位自动重装模式
- TH1 = 0xfd;
- TL1 = 0xfd;//9600波特率
- TR1 = 1;//启动定时器1
- }
- /************************************************************************/
- void run(void) //pwm调速函数
- {
- push_val_left =pwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =pwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- if(pwm==10) pwm=0;
- if(pwm==0&&pwm<0) pwm=0;
- }
-
- /************************************************************************/
- /* PWM调制电机转速 */
- /************************************************************************/
- /* 左侧电机调速 */
- /*调节push_val_left的值改变电机转速,占空比 */
- void pwm_out_left_moto(void)
- {
- if(Left_moto_stop)
- {
- if(pwm_val_left<=push_val_left)
- { Left_moto_pwm=1;
- Left_moto2_pwm=1; }
- else
- { Left_moto_pwm=0;Left_moto2_pwm=0; }
- if(pwm_val_left>=10)
- pwm_val_left=0;
- }
- else { Left_moto_pwm=0;Left_moto2_pwm=0; }
- }
- /******************************************************************/
- /* 右侧电机调速 */
- void pwm_out_right_moto(void)
- {
- if(Right_moto_stop)
- {
- if(pwm_val_right<=push_val_right)
- { Right_moto_pwm=1;
- Right_moto2_pwm=1; }
- else
- {Right_moto_pwm=0;
- Right_moto2_pwm=0;}
- if(pwm_val_right>=10)
- pwm_val_right=0;
- }
- else {Right_moto_pwm=0;Right_moto2_pwm=0; }
- }
- /***************************************************/
- ///*TIMER0中断服务子函数产生PWM信号*/
- void timer0()interrupt 1 using 2
- {
- TH0=0XF8; //1Ms定时
- TL0=0X30;
- time++;
- pwm_val_left++;
- pwm_val_right++;
- pwm_out_left_moto();
- pwm_out_right_moto();
- }
- //小车前进
- void forward()
- {
- ET0 = 1;
- run(); //pwm 程序
- left_motor_go; //左电机前进
- right_motor_go; //右电机前进
- left_motor2_go; //左电机前进 后轮
- right_motor2_go; //右电机前进 后轮
- }
- void left_go() //左转
- {
- ET0 = 1;
- run();
- left_motor_back;
- right_motor_go;
- left_motor2_back;
- right_motor2_go;
- delay(700);
- forward();
- }
- //右转
- void right_go()
- {
- ET0 = 1;
- run();
- delay(50);
- right_motor_back;
- left_motor_go;
- right_motor2_back;
- left_motor2_go;
- delay(700);
- forward();
- }
- //小车左转圈
- void left()
- {
- ET0 = 1;
- run();
- delay(50);
- right_motor_go; // 右电机前进
- left_motor_back; // 左电机后退
- right_motor2_go; // 右电机前进
- left_motor2_back; // 左电机后退
- }
- //小车右转圈
- void right()
- {
- ET0 = 1;
- run();
- left_motor_go;
- right_motor_back;
- left_motor2_go;
- right_motor2_back;
- }
- //小车后退
- void back()
- {
- ET0 = 1;
- run();
- left_motor_back;
- right_motor_back;
- left_motor2_back;
- right_motor2_back;
- }
- //小车停止
- void stop()
- {
- ET0 = 0;
- P1=0;
- P0=0;
- }
-
- //串口中断
- void UART_SER() interrupt 4
- {
- if(RI)
- {
- RI = 0;//清除接收标志
- switch(SBUF)
- {
- case 'g': forward(); break;//前进
- case 'b': back(); break;//后退
- case 'l': left(); break;//左转圈
- case 'r': right(); break;//右转圈
- case 's': stop(); break;//停止
- case 'z': left_go(); break;//左转行驶
- case 'y': right_go(); break;//右转行驶
- case 'p': pwm++;break; //加速
- case 'c': pwm--;break; //减速
- }
- }
- }
- void main()
- {
- TMOD=0X01;
- TH0= 0XF8; //1ms定时
- TL0= 0X30;
- TR0= 1;
- ET0= 1;
- EA = 1;
- UART_INIT();//串口初始化
- while(1);
- }
复制代码
所有资料51hei提供下载:
资料.zip
(1.01 MB, 下载次数: 662)
|