找回密码
 立即注册

QQ登录

只需一步,快速开始

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

XB-4WD-STM32智能小车超声波避障实验程序和教学视频教程

  [复制链接]
跳转到指定楼层
楼主

压缩包里面有视频教程:


单片机源程序如下:
  1. /*************************** XB-4WD-STM32智能小车参考程序 *******************************
  2. * 实 验 名 :XB-4WD-STM32智能小车超声波避障实验
  3. * 连接方式 :请参考interface.h文件
  4. ****************************************************************************************/
  5. #include "stm32f10x.h"
  6. #include "interface.h"
  7. #include "LCD1602.h"
  8. #include "IRCtrol.h"
  9. #include "motor.h"
  10. #include "UltrasonicCtrol.h"

  11. /**********************************************************************************************
  12. **                                 全局变量定义
  13. **********************************************************************************************/
  14. unsigned int speed_count=0;//占空比计数器 50次一周期
  15. char front_left_speed_duty=SPEED_DUTY;
  16. char front_right_speed_duty=SPEED_DUTY;
  17. char behind_left_speed_duty=SPEED_DUTY;
  18. char behind_right_speed_duty=SPEED_DUTY;

  19. unsigned char tick_5ms = 0;//5ms计数器,作为主函数的基本周期
  20. unsigned char tick_1ms = 0;//1ms计数器,作为电机的基本计数器
  21. unsigned char tick_200ms = 0;//刷新显示

  22. char ctrl_comm = COMM_STOP;//控制指令
  23. char ctrl_comm_last = COMM_STOP;//上一次的指令
  24. unsigned char continue_time=0;

  25. unsigned char duoji_count=0;
  26. unsigned char zhuanjiao = 11;

  27. void InitIO()
  28. {
  29.         GPIO_InitTypeDef GPIO_InitStructure;

  30.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE , ENABLE);//使能LED使用的GPIO时钟
  31.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  32.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  33.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34.        
  35. GPIO_Init(GPIOE , &GPIO_InitStructure);//将使用LED灯相关的GPIO初始化
  36. GPIO_SetBits(GPIOE , GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10);//关闭所有的LED指示灯
  37. }

  38. void DuojiMid()
  39. {
  40.         zhuanjiao = 11;
  41.         Delayms(150);//延时1s
  42. }

  43. void DuojiRight()
  44. {
  45.         zhuanjiao = 5;
  46.         Delayms(150);//延时1s
  47. }

  48. void DuojiLeft()
  49. {
  50.         zhuanjiao = 18;
  51.         Delayms(150);//延时1s
  52. }

  53. ///获取三个方向的距离,进来前舵机方向为向前
  54. void GetAllDistance(unsigned int *dis_left,unsigned int *dis_right,unsigned int *dis_direct)
  55. {
  56.         CarStop();
  57.         GetDistanceDelay();
  58.         *dis_direct = distance_cm;
  59.        
  60.         DuojiRight();
  61.         Delayms(100);
  62.         GetDistanceDelay();//获取右边距离
  63.         *dis_right = distance_cm;
  64.        
  65.         DuojiMid();
  66.         DuojiLeft();
  67.         Delayms(100);
  68.         GetDistanceDelay();//获取左边距离
  69.         *dis_left = distance_cm;
  70.        
  71.         DuojiMid();//归位
  72. }

  73. void BarrierProc()
  74. {
  75.                 if(distance_cm < 30)//前方有障碍物
  76.         {
  77.                 unsigned int dis_left;//左边距离
  78.                 unsigned int dis_right;//右边距离
  79.                 unsigned int dis_direct;//右边距离
  80.                 if(distance_cm < 15)
  81.                 {
  82.                         CarBack();
  83.                         Delayms(400);
  84.                 }
  85.                
  86.                 while(1)
  87.                 {
  88.                         GetAllDistance(&dis_left,&dis_right,&dis_direct);
  89.                         if(dis_direct < 10)
  90.                         {
  91.                                 CarBack();
  92.                                 Delayms(300);
  93.                                 continue;
  94.                         }
  95.                         else if((dis_left < 10) || (dis_right < 10))
  96.                         {
  97.                                 CarBack();
  98.                                 Delayms(300);
  99.                                 continue;
  100.                         }
  101.                         else if(dis_direct >= dis_left && dis_direct >= dis_right)//前方距离最远
  102.                         {
  103.                                 CarGo();
  104.                                 Delayms(600);
  105.                                 return;
  106.                         }
  107.                         else if(dis_left <= dis_right)//右转
  108.                         {
  109.                                 CarRight();
  110.                                 Delayms(500);
  111.                         }
  112.                         else if(dis_right < dis_left)
  113.                         {
  114.                                 CarLeft();
  115.                                 Delayms(500);
  116.                         }
  117.                 }
  118.         }
  119.         else
  120.         {
  121.                 CarGo();
  122.         }
  123. }


  124. int main(void)
  125. {
  126.         delay_init();
  127.         LCD1602Init();
  128.         //IRCtrolInit();
  129.         TIM2_Init();
  130.         MotorInit();
  131.         UltraSoundInit();
  132. InitIO();
  133.         //RedRayInit();
  134.         ServoInit();

  135. while(1)
  136. {         
  137.                          if(tick_5ms >= 5)
  138.                 {
  139.                         tick_5ms = 0;
  140.                         tick_200ms++;
  141.                         if(tick_200ms >= 40)
  142.                         {
  143.                                 tick_200ms = 0;
  144.                                 LEDToggle(GPIO_Pin_8);
  145.                                 LCD1602WriteDistance(distance_cm);//更新距离
  146.                         }
  147.                         Distance();//计算距离
  148.                         BarrierProc();
  149.                 }
  150. }
  151. }
复制代码

所有资料51hei提供下载:
下载链接:
游客,本帖隐藏的内容需要积分高于 55 才可浏览,您当前积分为 0

评分

参与人数 1黑币 +1 收起 理由
不跳的马小跳 + 1 赞一个!

查看全部评分

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

使用道具 举报

沙发
ID:277190 发表于 2018-4-11 11:12 | 只看该作者
我有硬件,请发一份资料,是51单片机控制的
回复

使用道具 举报

板凳
ID:317867 发表于 2018-5-3 21:33 | 只看该作者
这个看起来会对我的毕业设计比较有帮助。
回复

使用道具 举报

地板
ID:205327 发表于 2018-5-6 12:40 | 只看该作者
谢谢楼主大大
回复

使用道具 举报

5#
ID:386514 发表于 2018-8-26 15:26 | 只看该作者
楼主资料可以发我一份吗
回复

使用道具 举报

6#
ID:298593 发表于 2018-9-6 11:24 | 只看该作者
过期啦,大佬
回复

使用道具 举报

7#
ID:267330 发表于 2018-10-11 14:22 | 只看该作者
我有硬件,请发一份资料,是51单片机控制的
回复

使用道具 举报

8#
ID:485912 发表于 2019-3-6 22:11 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

9#
ID:266092 发表于 2019-3-29 11:23 | 只看该作者
楼主能分享一个源代码吗。邮箱742937692@qq.com
回复

使用道具 举报

10#
ID:500189 发表于 2019-3-29 15:53 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

11#
ID:503097 发表于 2019-4-2 11:12 来自手机 | 只看该作者
谢谢。。。。。。
回复

使用道具 举报

12#
ID:240307 发表于 2019-10-19 17:29 | 只看该作者
谢谢分享
回复

使用道具 举报

13#
ID:349178 发表于 2020-1-1 20:33 来自手机 | 只看该作者
ecpc 发表于 2018-4-11 11:12
我有硬件,请发一份资料,是51单片机控制的

失效了、能重发一份不

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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