独闷闷网

 找回密码
 立即注册
搜索
查看: 12452|回复: 8
打印 上一主题 下一主题
收起左侧

[原创] 手把手教你做蓝牙小车

[复制链接]
跳转到指定楼层
楼主
发表于 2014-8-7 12:57:02 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
前些日子一直在看鸿哥的程序感觉光看也只是纸上谈兵,所以想通过做小车的方式深入的体会一下鸿哥的程序。想学习鸿哥的程序的可点击下面的连接
(连载中...)从业将近十年!手把手教你单片机程序框架。废话少说先上零件图

小车底盘

电机驱动


蓝牙模块HC-06


12V锂电池


烧写器


安卓app蓝牙小车


单片机最小系统

本来想自己画板子做模块的,但是老师给了我一大堆模块,没办法就用老师的模块吧
嘿嘿


模块都上了怎么连接?
那我就简单的说一下,

电机驱动:8个控制端接51单片机P0口,驱动PWM使能端接P2.0-P2.3
因为电机驱动上有5V输出电压所以可以恰好给单片机供电,所以很好的解决了单片机要与驱动共地的问题。


蓝牙模块:蓝牙模块的RXD接单片机的P3.1,蓝牙模块的TXD接单片机的P3.0.蓝牙模块的电源接单片机的电源。

12V锂电池:电池就接在电机驱动的输入端上。

哈哈简单吧


接下来上源代码
  1. #include "reg51.h"



  2. sbit PWM1 = P2^0;//右后轮电机驱动使能
  3. sbit PWM2 = P2^1;//左后轮电机驱动使能
  4. sbit PWM3 = P2^2;//右前轮电机驱动使能
  5. sbit PWM4 = P2^3;//左前轮电机驱动使能

  6. sbit motor_control_1 = P0^0;//右后轮后退
  7. sbit motor_control_2 = P0^1;//右后轮前进
  8. sbit motor_control_3 = P0^2;//左后轮后退
  9. sbit motor_control_4 = P0^3;//左后轮前进
  10. sbit motor_control_5 = P0^4;//右前轮后退
  11. sbit motor_control_6 = P0^5;//右前轮前进
  12. sbit motor_control_7 = P0^6;//左前轮后退
  13. sbit motor_control_8 = P0^7;//左前轮前进
  14. unsigned char ucBluetoothData = 230;//设置初始速度最大值为255最小为125

  15. unsigned char ucLock = 0;//互斥量,俗称原子锁


  16. unsigned int uiPWMCnt1 = 0;
  17. unsigned int uiPWM1 = 230;
  18. unsigned int uiPWMCnt2 = 0;
  19. unsigned int uiPWM2 = 230;
  20. unsigned int uiPWMCnt3 = 0;
  21. unsigned int uiPWM3 = 230;
  22. unsigned int uiPWMCnt4 = 0;
  23. unsigned int uiPWM4 = 230;

  24. unsigned char ucTempPWM;//设置中间变量



  25. void initial_myself();
  26. void initial_peripheral();
  27. void T0_time();
  28. void usart_service(void);
复制代码

哈哈看着代码是不是似曾相识的赶脚

接下来上软件





用手机与小车蓝牙匹配上就可以用了

密码是1234哦。
软件在这里:http://pan.baidu.com/s/1eQnEHoA


以是安装过程。

细心的朋友会发现这个软件发出来的数据怎么得到的呢?
接下来教大家怎么获得手机发出来的数据(很重要可以用在其他关于串口的项目上!):


用PL2303烧写器和蓝牙相连,
接法和单片机相连的接法一样。
TXD-RXD
RXD-TXD
注意电源别接反哦!!如果不行就再反过来接!!总有一个顺序可以
没有PL2303也可以用学习板哦!!

打开烧写软件:


烧写软件上面有串口助手哦!功能强大吧!!!
打开串口配置如图。

端口怎么找????这个就不要问了嘛???
右键计算机-设备管理器-端口


手机连接上蓝牙发数据,数据出来了吧!!哈哈就这么简单


晒张总图


玩小车去喽!!!



小结:之前做过小车本以为做个蓝牙小车手到擒来,但是还是遇到了几个问题
1.一开始不知道最小系统的晶振是12Mhz的,导致系统不正常,找了好长时间才找出来原因。所以一定要记得使用11。0592的晶振呀!这个晶振用在串口上最好了!
2.调小车轮子转动的方向一定要一个一个调,待所有的轮子都能正常转动了才能进行下面的步骤。
3.最后就是程序,调的时候一个功能一个功能的实现,没有一下子都把程序写出来的,切忌浮躁,还有就是,因为涉及到串口,有必要的话,还要编写一个发送串口数据的子函数,这样在电脑上可以随时看到你发送的数据了!上面的

后续还有小车pid算法,寻光,循迹的研究希望大家继续关注!!

有不懂的加我qq727142092


                                                                 河北科技师范学院
                                                                 电子爱好者协会
                                                                 邱增顺
                                                                 2014年8月7日

Screenshot_2014-08-07-09-16-56.png (222.21 KB, 下载次数: 308)

Screenshot_2014-08-07-09-16-56.png
乐于分享,勇于质疑!
沙发
 楼主| 发表于 2014-8-7 13:02:02 | 只看该作者
额。。。。。。为什么代码没有全部传上去
再加一次吧
  1. #include "reg51.h"



  2. sbit PWM1 = P2^0;//右后轮电机驱动使能
  3. sbit PWM2 = P2^1;//左后轮电机驱动使能
  4. sbit PWM3 = P2^2;//右前轮电机驱动使能
  5. sbit PWM4 = P2^3;//左前轮电机驱动使能

  6. sbit motor_control_1 = P0^0;//右后轮后退
  7. sbit motor_control_2 = P0^1;//右后轮前进
  8. sbit motor_control_3 = P0^2;//左后轮后退
  9. sbit motor_control_4 = P0^3;//左后轮前进
  10. sbit motor_control_5 = P0^4;//右前轮后退
  11. sbit motor_control_6 = P0^5;//右前轮前进
  12. sbit motor_control_7 = P0^6;//左前轮后退
  13. sbit motor_control_8 = P0^7;//左前轮前进
  14. unsigned char ucBluetoothData = 230;//设置初始速度最大值为255最小为125

  15. unsigned char ucLock = 0;//互斥量,俗称原子锁


  16. unsigned int uiPWMCnt1 = 0;
  17. unsigned int uiPWM1 = 230;
  18. unsigned int uiPWMCnt2 = 0;
  19. unsigned int uiPWM2 = 230;
  20. unsigned int uiPWMCnt3 = 0;
  21. unsigned int uiPWM3 = 230;
  22. unsigned int uiPWMCnt4 = 0;
  23. unsigned int uiPWM4 = 230;

  24. unsigned char ucTempPWM;//设置中间变量



  25. void initial_myself();
  26. void initial_peripheral();
  27. void T0_time();
  28. void usart_service(void);
  29. //void usart_send(unsigned char ucSendData);
  30. void delay_long(unsigned int uiDelayLong);
  31. void go_forward(void);//前进
  32. void fall_back(void);//后退
  33. turn_left(void);//左转
  34. void turn_right(void);//右转
  35. void stop();//刹车

  36. void main()
  37. {
  38.         initial_myself();
  39.         delay_long(100);
  40.         initial_peripheral();
  41.         while(1)
  42.         {
  43.                 usart_service();
  44.                
  45. //                delay_long(500);
  46.                
  47.        
  48.         }

  49. }
  50. //串口服务函数
  51. void usart_service()
  52. {
  53.                
  54.                 switch(ucBluetoothData)
  55.                 {
  56.                         case 0x04://前进
  57.                                 ucBluetoothData = 0x02;//避免一直触发
  58.                                 go_forward();
  59.                                 ucLock = 1;
  60.                                 uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
  61.                                 ucLock = 0;
  62.                                 break;
  63.                         case 0x05://左转
  64.                             ucBluetoothData = 0x02;//避免一直触发
  65.                                 turn_left();
  66.                                 ucLock = 1;
  67.                                 uiPWM2 = uiPWM4= ucTempPWM / 4;
  68.                                 uiPWM3 =uiPWM1 = ucTempPWM;
  69.                                 ucLock = 0;
  70.                                 break;
  71.                         case 0x06://右转
  72.                                 ucBluetoothData = 0x02;//避免一直触发
  73.                                 turn_right();
  74.                                 ucLock = 1;
  75.                                 uiPWM2 = uiPWM4 = ucTempPWM;
  76.                                 uiPWM3 =uiPWM1 = ucTempPWM / 4;
  77.                                 ucLock = 0;
  78.                                 break;
  79.                         case 0x07://后退
  80.                                 ucBluetoothData = 0x02;//避免一直触发
  81.                                 fall_back();
  82.                                 ucLock = 1;
  83.                                 uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
  84.                                 ucLock = 0;
  85.                                 break;
  86.                         case 0x01:
  87.                                 ucBluetoothData = 0x02;//避免一直触发
  88.                                 stop();       
  89.                                 ucLock = 1;
  90.                                 uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
  91.                                 ucLock = 0;
  92.                                 break;
  93.                         case 0x00:
  94.                                 ucBluetoothData = 0x02;//避免一直触发
  95.                                 stop();
  96.                                 ucLock = 1;
  97.                                 uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;
  98.                                 ucLock = 0;
  99.                                 break;
  100.                         default :
  101.                                 break;
  102.        
  103.                 }
  104.                 delay_long(100);
  105. //                usart_send(ucBluetoothData);
  106.                 //速度调节的数据
  107.                 if(ucBluetoothData!=0x00&&ucBluetoothData!=0x01 && ucBluetoothData!=0x02 &&  ucBluetoothData!=0x04 && ucBluetoothData!=0x05 && ucBluetoothData!=0x06 && ucBluetoothData!=0x07)
  108.                 {
  109.                         ucLock = 1;
  110.                         ucTempPWM = uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucBluetoothData;
  111.                         ucLock = 0;
  112.                 }

  113. }
  114. void T0_time() interrupt 1
  115. {
  116.         TF0 = 0;//清除中断标志
  117.         TR0 = 0;//关定时器
  118.         uiPWMCnt1 ++;
  119.         uiPWMCnt2 ++;
  120.         uiPWMCnt3 ++;
  121.         uiPWMCnt4 ++;
  122.         if(ucLock == 0)
  123.         {
  124.                 if(uiPWMCnt1 > 255)
  125.                 {
  126.                         uiPWMCnt1 = 0;
  127.                 }
  128.                 if(uiPWMCnt1 < uiPWM1)
  129.                 {
  130.                         PWM1 = 1;
  131.                        
  132.                 }
  133.                 else
  134.                 {
  135.                         PWM1 = 0;
  136.                 }
  137.        
  138.                 if(uiPWMCnt2 > 255)
  139.                 {
  140.                         uiPWMCnt2 = 0;
  141.                 }
  142.                 if(uiPWMCnt2 < uiPWM2)
  143.                 {
  144.                         PWM2 = 1;
  145.                        
  146.                 }
  147.                 else
  148.                 {
  149.                         PWM2 = 0;
  150.                 }
  151.        
  152.                 if(uiPWMCnt3 > 255)
  153.                 {
  154.                         uiPWMCnt3 = 0;
  155.                 }
  156.                 if(uiPWMCnt3 < uiPWM3)
  157.                 {
  158.                         PWM3 = 1;
  159.                        
  160.                 }
  161.                 else
  162.                 {
  163.                         PWM3 = 0;
  164.                 }
  165.        
  166.                 if(uiPWMCnt4 > 255)
  167.                 {
  168.                         uiPWMCnt4 = 0;
  169.                 }
  170.                 if(uiPWMCnt4 < uiPWM4)
  171.                 {
  172.                         PWM4 = 1;
  173.                        
  174.                 }
  175.                 else
  176.                 {
  177.                         PWM4 = 0;
  178.                 }
  179.        
  180.        
  181.         }

  182.         TH0 = 0xff;
  183.         TL0 = 0x28;
  184.         TR0 = 1;///开定时器

  185. }

  186. void initial_myself()
  187. {
  188.         TMOD = 0x01;//设置定时器0为工作方式1
  189.         TH0 = 0xff;
  190.         TL0 = 0x28;

  191.         //配置串口
  192.         SCON = 0x50;
  193.         TMOD = 0x21;
  194.         TH1 = TL1 = -(11095200L/12/32/9600);
  195.         IP = 0x10;
  196.         stop();
  197.         PWM1 = 1;
  198.         PWM2 = 1;
  199.         PWM3 = 1;
  200.         PWM4 = 1;

  201. }

  202. void initial_peripheral()
  203. {
  204.         EA = 1;//开总中断
  205.         ES = 1;//允许串口中断
  206.         ET0 = 1;//允许定时器中断
  207.         TR0 = 1;//启动定时器
  208.         TR1 = 1;//
  209. }
  210. void usart_receive(void) interrupt 4
  211. {

  212.         if(RI == 1)
  213.         {
  214.                 RI = 0;
  215.                 ucBluetoothData = SBUF;
  216. //                uiSendCnt = 0;
  217.         }

  218.         else
  219.         {
  220.                 TI = 0;
  221.         }

  222. }
  223. //void usart_send(unsigned char ucSendData)
  224. //{
  225. //        ES = 0;
  226. //        TI = 0;
  227. //        SBUF = ucSendData;
  228. //        TI = 0;
  229. //        ES = 1;
  230. //
  231. //}
  232. void delay_long(unsigned int uiDelayLong)
  233. {
  234.         unsigned int i;
  235.         unsigned int j;
  236.         for(i = 0 ; i < uiDelayLong ; i++)
  237.         {
  238.                 for(j = 0; j < 500; j++);
  239.        
  240.         }

  241. }
  242. void stop()//停止
  243. {
  244.         motor_control_1 = 0;
  245.         motor_control_2 = 0;
  246.         motor_control_3 = 0;
  247.         motor_control_4 = 0;
  248.         motor_control_5 = 0;
  249.         motor_control_6 = 0;
  250.         motor_control_7 = 0;
  251.         motor_control_8 = 0;
  252. }
  253. void fall_back()
  254. {
  255.         motor_control_1 = 1;
  256.         motor_control_2 = 0;
  257.         motor_control_3 = 1;
  258.         motor_control_4 = 0;
  259.         motor_control_5 = 1;
  260.         motor_control_6 = 0;
  261.         motor_control_7 = 1;
  262.         motor_control_8 = 0;
  263. }
  264. void go_forward()
  265. {
  266.         motor_control_1 = 0;
  267.         motor_control_2 = 1;
  268.         motor_control_3 = 0;
  269.         motor_control_4 = 1;
  270.         motor_control_5 = 0;
  271.         motor_control_6 = 1;
  272.         motor_control_7 = 0;
  273.         motor_control_8 = 1;
  274. }
  275. void turn_left()//左转
  276. {
  277.         motor_control_1 = 0;
  278.         motor_control_2 = 1;
  279.         motor_control_3 = 0;
  280.         motor_control_4 = 1;
  281.         motor_control_5 = 0;
  282.         motor_control_6 = 1;
  283.         motor_control_7 = 0;
  284.         motor_control_8 = 1;
  285.        
  286. }
  287. void turn_right()//右转
  288. {
  289.         motor_control_1 = 0;
  290.         motor_control_2 = 1;
  291.         motor_control_3 = 0;
  292.         motor_control_4 = 1;
  293.         motor_control_5 = 0;
  294.         motor_control_6 = 1;
  295.         motor_control_7 = 0;
  296.         motor_control_8 = 1;
  297. }
复制代码
乐于分享,勇于质疑!
板凳
发表于 2014-8-8 13:23:29 | 只看该作者
好资料,感谢分享。
乐于分享,勇于质疑!
地板
发表于 2014-8-8 15:19:50 | 只看该作者
感谢楼主分享。楼主威武。
乐于分享,勇于质疑!
5#
发表于 2014-8-8 15:20:25 | 只看该作者
看花开花落 发表于 2014-8-7 13:02
额。。。。。。为什么代码没有全部传上去
再加一次吧


感谢楼主分享。楼主威武。
乐于分享,勇于质疑!
6#
发表于 2014-8-9 13:43:26 | 只看该作者
太好了,实在是太好了。
乐于分享,勇于质疑!
7#
发表于 2014-8-10 23:03:44 | 只看该作者
顶,威武,谢楼主,学习了
乐于分享,勇于质疑!
8#
发表于 2014-9-9 08:28:27 | 只看该作者
不错啊!给个赞........
乐于分享,勇于质疑!
9#
发表于 2014-9-29 21:01:16 | 只看该作者
谢谢楼主分享!!!
乐于分享,勇于质疑!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|独闷闷网 ( 粤ICP备12007667号-2 )

GMT+8, 2024-4-27 08:48 , Processed in 0.202750 second(s), 19 queries .

快速回复 返回顶部 返回列表