找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4433|回复: 4
打印 上一主题 下一主题
收起左侧

科技立项--蓝牙小车

[复制链接]
跳转到指定楼层
楼主
ID:52130 发表于 2014-4-17 09:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include<reg52.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define uint unsigned int
  5. #define uchar unsigned char
  6. #define ulong unsigned long
  7. #define t1n 100

  8. /*sbit L_on=P1^4;
  9. sbit L1=P0^1;
  10. sbit L2=P0^2;
  11. sbit L3=P0^3;
  12. sbit L4=P0^4;
  13. sbit L5=P0^5;
  14. sbit L6=P0^6; */

  15. //循迹红外对管信号反馈
  16. sbit Leftled = P3^2;
  17. sbit Rightled = P3^4;
  18. sbit Leftled2 = P3^3;
  19. sbit Rightled2 = P3^5;

  20. //超声波管脚定义
  21. sbit Trig = P2^0; //产生脉冲引脚
  22. sbit Echo = P2^1; //回波引脚

  23. //左边轮子
  24. sbit in1=P0^0;
  25. sbit in2=P0^1;
  26. sbit in3=P0^2 ;
  27. sbit in4=P0^3 ;
  28. //右边轮子
  29. sbit in11=P0^4 ;
  30. sbit in22=P0^5 ;
  31. sbit in33=P0^6 ;
  32. sbit in44=P0^7 ;

  33. sbit Beef = P3^6;
  34. uint ZKB = 70;
  35. uchar BK =0;

  36. uchar z=0,time=0; //计数器
  37. char Buffer;
  38. char BufferFunction = 0x63; //默认模式限定蓝牙控制
  39. char BufferSpeed;
  40. char BufferDirection;

  41. bit flag=0; //用于超声波
  42. bit flag_b=0;
  43. bit flag1=0;
  44. ulong t0,count,tc;
  45. ulong distance;
  46. ulong C;

  47. void Bluetooth_Move();
  48. void BTControl();
  49. void xunji();
  50. void bizhang();

  51. //超声波
  52. void delay(uint z);
  53. void delay1(void);
  54. void DelayMs(uint t);
  55. void DelayUs(uint t);
  56. void celiang();
  57. void zhuanxiang();

  58. /********************************************************************
  59. * 名称 : Delay_1ms()
  60. * 功能 : 延时子程序,延时时间为 1ms * x
  61. * 输入 : x (延时一毫秒的个数)
  62. * 输出 : 无
  63. ***********************************************************************/
  64. void Delay_1ms(int i)//1ms延时
  65. {
  66. int x,j;
  67. for(j=0;j<i;j++)
  68. for(x=0;x<=148;x++);
  69. }
  70. /********************************************************************
  71. * 名称 : Com_Int()
  72. * 功能 : 串口中断子函数
  73. * 输入 : 无
  74. * 输出 : 无
  75. ***********************************************************************/
  76. //当IO口有数据流入时会自动执行
  77. void Com_Int(void) interrupt 4
  78. {
  79. //ES = 0; //总中断关闭
  80. if(RI == 1) //当硬件接收到一个数据时,RI会置位串口有发送和接收两种不同的情况
  81. //(不论是发送还是接收,只要发送或接收完成,都会置位串口中断标志位(发送就置位TI,接收则置位RI),
  82. //所以=1表示的是两个标志位只要有一个为1(即是两种情况有一种发生)且此时系统允许串口中断,
  83. //则会转向中断服务程序,在服务程序里判断中断是TI/RI哪个为1触发的,相应的做出处理,如可以把缓冲区的数据读出来。)
  84. {
  85. Buffer=SBUF;//SBUF:51串口通信寄存器
  86. if(0x62<=Buffer&&Buffer<=0x69){
  87. BufferFunction = Buffer;
  88. B = 1;
  89. }
  90. if(0x30<=Buffer&&Buffer<=0x39) BufferSpeed = Buffer;
  91. if(0x41<=Buffer&&Buffer<=0x61) BufferDirection = Buffer;
  92. RI = 0; //关闭串口中断
  93. }
  94. //ES = 1;
  95. }


  96. /********************************************************************
  97. * 名称 : Com_Init()
  98. * 功能 : 串口初始化,晶振11.0592,波特率9600,使串口中断
  99. * 输入 : 无
  100. * 输出 : 无
  101. ***********************************************************************/
  102. void Com_Init(void)
  103. {
  104. TMOD = 0x21; //选择使用8位定时器1
  105. PCON = 0x00; //是设置波特率不加倍
  106. SCON = 0x50; //选择8位异步通信方式,波特率可变,由定时器控制;允许接受;发送中断标志、接收中断标志分别为0、0
  107. TH1 = 0xfd; //设置波特率 9600= 11.0592*1000000/12/(0x100-0xfd)/32
  108. TL1 = 0xfd;
  109. TH0 = (65536-100)/256;
  110. TL0 = (65536-100)/256;
  111. TR0 = 0; //先不启动定时器0
  112. TR1 = 1; //启动定时器1
  113. ES = 1; //开串口中断
  114. EA = 1; //开总中断

  115. //超声波管脚
  116. Trig=0;
  117. Echo=0;
  118. }

  119. //避障超声波定时器
  120. void timer0()interrupt 1 using 2
  121. {
  122. TH0 = (65536-100)/256; //定时器0无法溢出
  123. TL0 = (65536-100)/256;
  124. }

  125. void timer1()interrupt 2 using 2
  126. {
  127. TH1 = (65536-100)/256;
  128. TL1 = (65536-100)/256;
  129. z++;
  130. if(z>=100) z=0;
  131. }


  132. /**********速度变化*******************/
  133. void SpeedChoice()
  134. {
  135. switch(BufferSpeed){
  136. case '0': ZKB = 10;break;
  137. case '1': ZKB = 20;break;
  138. case '2': ZKB = 30;break;
  139. case '3': ZKB = 40;break;
  140. case '4': ZKB = 50;break;
  141. case '5': ZKB = 60;break;
  142. case '6': ZKB = 70;break;
  143. case '7': ZKB = 80;break;
  144. case '8': ZKB = 90;break;
  145. case '9': ZKB = 100;break;
  146. }
  147. }


  148. /*******左边轮驱动***************/
  149. void pwm_out_left_moto(uchar a,uchar b,uchar c,uchar d )
  150. {
  151. if(z<a) in1=1;
  152. else in1=0;
  153. if(z<b) in2=1;
  154. else in2=0;
  155. if(z<c) in3=1;
  156. else in3=0;
  157. if(z<d) in4=1;
  158. else in4=0;
  159. }

  160. /*********右边轮驱动*******************/
  161. void pwm_out_right_moto(uchar a,uchar b,uchar c,uchar d )
  162. {
  163. if(z<a) in11=1;
  164. else in11=0;
  165. if(z<b) in22=1;
  166. else in22=0;
  167. if(z<c) in33=1;
  168. else in33=0;
  169. if(z<d) in44=1;
  170. else in44=0;
  171. }

  172. /************超声波********************/
  173. //*************************启动超生波模块***************************************
  174. void startmodule()
  175. {
  176. count=0;
  177. Trig=1;
  178. DelayUs(18);
  179. Trig=0;

  180. while(!Echo) // 等待高电平
  181. {
  182. count++; //超时检测
  183. if(count>26000)
  184. {count=0;
  185. flag1=1;break;}
  186. }
  187. if(!flag1)
  188. {
  189. TR0=1; //启动定时器0
  190. while(Echo); //beep=0;
  191. TR0=0; //关闭定时器0

  192. }
  193. else
  194. {flag1=0;
  195. TH0=30000;
  196. TL0=30000;
  197. }
  198. }

  199. //*******************测距模块*********************************************
  200. void celiang()
  201. {
  202. t0=TH0*256+TL0;
  203. TH0=0;
  204. TL0=0;
  205. distance=(t0*1.7)/100; //算出来是CM
  206. if((distance>=700)||(flag==1)) //超出测量范围
  207. {
  208. flag=0;distance=700;
  209. }

  210. }
  211. void zhuanxiang()
  212. {
  213. startmodule();
  214. celiang();
  215. if(distance<25)
  216. {
  217. if(distance<15)
  218. {
  219. pwm_out_left_moto(0,100,0,100); //后退
  220. pwm_out_right_moto(0,100,0,100);
  221. delay1();
  222. pwm_out_left_moto(0,100,0,100); //左转
  223. pwm_out_right_moto(100,0,100,0);

  224. delay1();
  225. }
  226. else
  227. {
  228. pwm_out_left_moto(0,100,0,100); //左转
  229. pwm_out_right_moto(100,0,100,0);
  230. delay1();
  231. }
  232. }
  233. else
  234. pwm_out_left_moto(100,0,100,0);
  235. pwm_out_right_moto(100,0,100,0);
  236. }

  237. /*******************延时函数*********************************************/
  238. //***************us延时函数
  239. void delay(uint z)
  240. {
  241. uint x,y;
  242. for(x=z;x>0;x--)
  243. for(y=110;y>0;y--);
  244. }
  245. void DelayUs(uint t)
  246. {
  247. while(--t);
  248. }
  249. void DelayMs(uint t)
  250. {
  251. while(t--) //大致延时mS
  252. {
  253. DelayUs(250);
  254. DelayUs(250);
  255. }
  256. }
  257. void delay1(void)
  258. {
  259. DelayMs(60);DelayMs(60);
  260. }


  261. /********主函数********************/
  262. void main()
  263. {
  264. // L_on = 0;
  265. Com_Init();
  266. while(1)
  267. {
  268. SpeedChoice();
  269. Bluetooth_Move();
  270. }
  271. }

  272. /*****************************/
  273. void Bluetooth_Move(){
  274. switch(BufferFunction){
  275. case 0x62: //遥控
  276. BTControl();
  277. break;
  278. case 0x63: //循迹
  279. xunji();
  280. break;
  281. case 0x64: //红外
  282. break;
  283. case 0x65: //金探
  284. break;
  285. case 0x66: //测温
  286. break;
  287. case 0x67: //测湿
  288. break;
  289. case 0x68: //测速
  290. break;
  291. case 0x69: //避障
  292. bizhang();
  293. break;


  294. }
  295. }

  296. /*************各种小车功能***********************/
  297. //蓝牙遥控
  298. void BTControl(){
  299. switch(BufferDirection){
  300. case 0x41: pwm_out_left_moto(20,0,20,0);
  301. pwm_out_right_moto(20,0,20,0);
  302. break; //前进
  303. case 0x42: pwm_out_left_moto(0,ZKB,0,ZKB);
  304. pwm_out_right_moto(0,ZKB,0,ZKB);
  305. break; //后退
  306. case 0x43: pwm_out_left_moto(0,80,0,80);
  307. pwm_out_right_moto(80,0,80,0);
  308. break; //左上
  309. case 0x44: pwm_out_left_moto(80,0,80,0);
  310. pwm_out_right_moto(0,80,0,80);
  311. break; //右上
  312. case 0x45: pwm_out_left_moto(80,0,80,0);
  313. pwm_out_right_moto(0,80,0,80);
  314. break; //左下
  315. case 0x46: pwm_out_left_moto(0,80,0,80);
  316. pwm_out_right_moto(80,0,80,0);
  317. break; //右下
  318. case 0x61: pwm_out_left_moto(0,0,0,0);
  319. pwm_out_right_moto(0,0,0,0);
  320. break; //手离开屏幕停止
  321. default : pwm_out_left_moto(0,0,0,0);
  322. pwm_out_right_moto(0,0,0,0);
  323. break;
  324. }
  325. }

  326. //循迹
  327. void xunji(){
  328. //有信号为0 没有信号为1(碰黑)
  329. if(Leftled==0&&Rightled==0&&Leftled2==0&&Rightled2==0)
  330. {
  331. pwm_out_left_moto(20,0,20,0);
  332. pwm_out_right_moto(20,0,20,0);
  333. }
  334. else
  335. {
  336. if(Rightled==1||Rightled2==1) //右边检测到黑线
  337. {
  338. if(Leftled==0||Leftled2==0)
  339. {
  340. pwm_out_left_moto(100,0,100,0);
  341. pwm_out_right_moto(0,100,0,100);
  342. }
  343. }
  344. if(Leftled==1||Leftled2==1) //左边检测到黑线
  345. {
  346. if(Rightled==0||Rightled==0)
  347. {
  348. pwm_out_left_moto(0,100,0,100);
  349. pwm_out_right_moto(100,0,100,0);
  350. }
  351. }
  352. if(Leftled==1&&Rightled==1&&Leftled2==1&&Rightled2==1) //两边检测到黑线
  353. {
  354. pwm_out_left_moto(0,0,0,0);
  355. pwm_out_right_moto(0,0,0,0);
  356. }
  357. }
  358. }
  359. void bizhang() //避障
  360. {
  361. zhuanxiang();
  362. }




复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:60882 发表于 2014-4-19 19:56 | 只看该作者
最近也要做这个,能告诉我你QQ号吗?我qq1519051636
回复

使用道具 举报

板凳
ID:114191 发表于 2016-6-2 14:36 | 只看该作者
无敌大神,这就是我的毕业设计内容,但是要答辩了还是有很多没做出来,现在只能看看你给的这些程序了。。
回复

使用道具 举报

地板
ID:121541 发表于 2016-9-3 14:30 | 只看该作者
为什么会对两个轮子的分开编译
回复

使用道具 举报

5#
ID:79544 发表于 2016-11-24 20:02 | 只看该作者
楼主你好,手机APP可以分享一下吗?谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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