找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机智能小车源程序-仅供参考

[复制链接]
跳转到指定楼层
楼主
ID:320288 发表于 2018-5-2 11:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是我个人根据学到知识所编辑的一个源程序,仅供参考,有用的话,拿去不谢。

单片机源程序如下:
  1. #include
  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. #define ulong unsigned long
  5. sbit you_2=P2^0; //红外探测端口定义
  6. sbit you_1=P2^1;
  7. sbit zhong=P2^2;
  8. sbit zuo_1=P2^3;
  9. sbit zuo_2=P2^4;
  10. sbit CG=P0^1;
  11. sbit DC=P0^0;
  12. uchar code L_F[8]= {0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; //左电机正转 uchar code L_B[8]= {0x90,0x80,0xc0,0x40,0x60,0x20,0x30,0x10}; //左电机反转 uchar code R_F[8]= {0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //右电机正转 uchar code R_B[8]= {0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //右电机反转 uchar code B_F[8]= {0x91,0x83,0xc2,0x46,0x64,0x2c,0x38,0x19}; //左反右正 uchar code F_B[8]= {0x19,0x38,0x2c,0x64,0x46,0xc2,0x83,0x91}; //左正右反 uchar code duandian[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //断电
  13. unsigned char code qianjin[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99}; //前进 unsigned char h[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99}; //
  14. uchar i;
  15. void delay(uint z)
  16. {
  17. uint k ;
  18. uint j;
  19. for(k=0; k
  20. for(j=0; j
  21. }
  22. void QJ(unsigned int i)
  23. {
  24. for(i=0;i
  25. {
  26. P1=h[i]=qianjin[i];
  27. delay(13);
  28. }
  29. }
  30. void DD()
  31. {
  32. P1=0x00;
  33. delay(300);
  34. }
  35. void wtj()
  36. {
  37. while(1)
  38. {
  39. if(P2==0xff)
  40. {
  41. DD();
  42. delay(1000);
  43. break;
  44. }
  45. else
  46. {
  47. QJ(8);}
  48. }
  49. }
  50. void YG_1()
  51. {
  52. unsigned char i; for(i=0;i
  53. }
  54. }
  55. void ZG_1()
  56. { unsigned char i; for(i=0;i
  57. {P1=h[i]=B_F[i]; delay(10);
  58. }
  59. }
  60. void byg()
  61. {
  62. while(1)
  63. {
  64. if(P2==0xfb)
  65. break;
  66. else if(P2==0xf9) break;
  67. else if(P2==0xfd) break;
  68. /* if(P2^0==0)
  69. break;
  70. else if(P2^1==0) break;
  71. /* else if(P2^2==0) break;
  72. else if(P2^3==0) break;
  73. else if(P2^4==0) break;*/
  74. else
  75. YG_1();
  76. }
  77. }
  78. void bzg()
  79. {
  80. while(1)
  81. {
  82. if(P2==0xfb)
  83. break;
  84. else if(P2==0xf3) break;
  85. else if(P2==0xf7) break;
  86. /* if(P2^0==0)
  87. break;
  88. else if(P2^1==0) break;
  89. else if(P2^2==0) break;
  90. if(P2^3==0)
  91. break;
  92. else if(P2^4==0)
  93. break;*/
  94. else
  95. ZG_1();
  96. }
  97. }
  98. void YG_2()
  99. {
  100. // unsigned char i; // unsigned char g;
  101. if(P2==0xfa)
  102. {//delay(4000);
  103. wtj();
  104. byg();
  105. /*for(g=0;g
  106. {
  107. for(i=0;i
  108. { P1=F_B[i];
  109. delay(20);
  110. }
  111. } */
  112. }
  113. else if(P2==0xf2)
  114. {
  115. //delay(4000);
  116. wtj();
  117. byg();
  118. /*for(g=0;g
  119. {
  120. for(i=0;i
  121. { P1=F_B[i];
  122. delay(20);
  123. }
  124. }*/
  125. }
  126. else if(P2==0xf6)
  127. {
  128. //delay(4000);
  129. byg();
  130. /*for(g=0;g
  131. {
  132. for(i=0;i
  133. { P1=F_B[i];
  134. delay(20);
  135. }
  136. }*/
  137. }
  138. else if(P2==0xf4) {
  139. //delay(4000);
  140. wtj();
  141. byg();
  142. /*for(g=0;g
  143. {
  144. for(i=0;i
  145. { P1=F_B[i];
  146. delay(20);
  147. }
  148. }*/
  149. }
  150. else ;
  151. }
  152. void ZG_2()
  153. {
  154. //unsigned char i;
  155. //unsigned char g;
  156. if(P2==0xeb)
  157. {//delay(4000);
  158. wtj();
  159. bzg();
  160. /*for(g=0;g
  161. {
  162. for(i=0;i
  163. {P1=B_F[i];
  164. delay(20);
  165. }
  166. }*/
  167. else if(P2==0xe9)
  168. {
  169. //delay(4000);
  170. wtj();
  171. bzg();
  172. /* for(g=0;g
  173. for(i=0;i
  174. {P1=B_F[i];
  175. delay(20);
  176. }
  177. }*/
  178. }
  179. else if(P2==0xed)
  180. {
  181. //delay(4000);
  182. wtj();
  183. bzg();
  184. /* for(g=0;g
  185. for(i=0;i
  186. {P1=B_F[i];
  187. delay(20);
  188. }
  189. }*/
  190. }
  191. else if(P2==0xe5) {
  192. //delay(4000);
  193. wtj();
  194. bzg();
  195. /* for(g=0;g
  196. for(i=0;i
  197. {P1=B_F[i];
  198. delay(20);
  199. }
  200. }*/
  201. }
  202. else ;
  203. }
  204. void ZG() {
  205. unsigned char i; for(i=0;i
  206. {
  207. P1=h[i]=R_F[i]; delay(11);
  208. }
  209. }
  210. void YG() {
  211. unsigned char i; for(i=0;i
  212. {
  213. P1=h[i]=L_F[i]; delay(11);
  214. }
  215. }
  216. /*void HT()
  217. {
  218. unsigned char i; for(i=0;i
  219. {
  220. P1=houtui[i]; delay(55);
  221. }
  222. }*/
  223. void zd()
  224. {
  225. while(1)
  226. {if(P2==0xe0)
  227. QJ(8);
  228. else if(P2==0xff) {
  229. while(1)
  230. {
  231. DD();
  232. }
  233. }
  234. else
  235. {
  236. QJ(80);
  237. if(P2==0xff) {
  238. while(1)
  239. {
  240. DD();
  241. }
  242. }
  243. else
  244. {DC=0;
  245. break;
  246. break;}
  247. }
  248. }
  249. }
  250. void zd1()
  251. {
  252. while(1)
  253. {if(P2==0xe0)
  254. QJ(8);
  255. else if(P2==0xff) {
  256. while(1)
  257. {
  258. DD();
  259. }
  260. }
  261. else
  262. {
  263. QJ(80);
  264. if(P2==0xff)
  265. while(1)
  266. {
  267. DD();
  268. }
  269. }
  270. else
  271. {DC=0;
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. void hy()
  278. {
  279. unsigned char i;
  280. for(i=0;i
  281. {
  282. P1=h[i];
  283. delay(10);
  284. }
  285. }
  286. void main()
  287. {
  288. DC=1;
  289. while(1)
  290. {
  291. QJ(8);
  292. if(P2!=0xff)
  293. {
  294. delay(9000); break;
  295. }
  296. }
  297. while(1)
  298. {
  299. DC=1;
  300. if(CG==1)
  301. {delay(9000);
  302. DC=1;
  303. while(1)
  304. {
  305. switch(P2)
  306. {
  307. case 0xfb:QJ(8);break;
  308. case 0xf1:QJ(8);break;
  309. case 0xf3:QJ(8);break; case 0xf7:ZG();break; case 0xe7:ZG_1();break; case 0xef:ZG_1();break; case 0xe3:ZG_1();break; case 0xe1:ZG_1();break;
  310. case 0xf9:QJ(8);break; case 0xfd:YG();break; case 0xfc:YG_1();break; case 0xfe:YG_1();break; case 0xf8:YG_1();break; case 0xf0:YG_1();break;
  311. case 0xfa:
  312. QJ(16);
  313. // delay(4000); YG_2();
  314. break;
  315. case 0xf2: //10010 //11011 //10001 //10011 //10111 //00111 //01111 //00011 //00001 //11001 //11101 //11100 //11110 //11000 //10000 //11010 //f7
  316. QJ(16);
  317. // delay(4000);
  318. YG_2();
  319. break;
  320. case 0xf6: //10110
  321. QJ(16);
  322. // delay(4000);
  323. YG_2();
  324. break;
  325. case 0xf4: //10100
  326. QJ(16);
  327. // delay(4000);
  328. YG_2();
  329. break;
  330. case 0xeb:
  331. QJ(16);
  332. // delay(4000);
  333. ZG_2();
  334. break;
  335. case 0xe9:
  336. QJ(16);
  337. // delay(4000);
  338. ZG_2();
  339. break;
  340. case 0xed:
  341. QJ(16);
  342. // delay(4000);
  343. ZG_2();
  344. break;
  345. case 0xe5:
  346. //01011 //fd //01001 //01101 //00101
  347. QJ(16);
  348. // delay(4000);
  349. ZG_2();
  350. break;
  351. case 0xe0:zd1();
  352. break; //00000
  353. case 0xff:hy();break;//11111
  354. default:QJ(8); break;
  355. }
  356. }
  357. }
  358. else
  359. {
  360. switch(P2)
  361. {
  362. case 0xfb:QJ(8);break; //11011
  363. case 0xf1:QJ(8);break; //10001
  364. case 0xf3:QJ(8);break; //10011 case 0xf7:ZG();break; //10111 case 0xe7:ZG_1();break; //00111 case 0xef:ZG_1();break; //01111 case 0xe3:ZG_1();break; //00011 case 0xe1:ZG_1();break; //00001
  365. case 0xf9:QJ(8);break; //11001 case 0xfd:YG();break; //11101 case 0xfc:YG_1();break; //11100 case 0xfe:YG_1();break; //11110 case 0xf8:YG_1();break; //11000
  366. case 0xf0:YG_1();break; //10000
  367. case 0xfa: //11010
  368. QJ(16);
  369. // delay(4000);
  370. YG_2();
  371. //f7
  372. break;
  373. case 0xf2: //10010
  374. QJ(16);
  375. // delay(4000);
  376. YG_2();
  377. break;
  378. case 0xf6: //10110
  379. QJ(16);
  380. // delay(4000);
  381. YG_2();
  382. break;
  383. case 0xf4: //10100
  384. QJ(16);
  385. // delay(4000);
  386. YG_2();
  387. break;
  388. case 0xeb:
  389. QJ(16);
  390. // delay(4000);
  391. ZG_2();
  392. break;
  393. case 0xe9:
  394. QJ(16);
  395. // delay(4000);
  396. ZG_2();
  397. break;
  398. case 0xed: //01011 //fd //01001 //01101
  399. QJ(16);
  400. // delay(4000);
  401. ZG_2();
  402. break;
  403. case 0xe5: //00101
  404. QJ(16);
  405. // delay(4000);
  406. ZG_2();
  407. break;
  408. case 0xe0:zd();
  409. break; //00000
  410. case 0xff:hy();break;//11111
  411. default:QJ(8); break;
  412. }
  413. }
  414. }
  415. }

复制代码

所有资料51hei提供下载:
2.docx (18.28 KB, 下载次数: 4)


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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