zoukankan      html  css  js  c++  java
  • 《SLAM机器人基础教程》第三章 单片机与STM32:超声测距实验

    3.10节 超声测距实验

    本节介绍超声测距传感器数据的采集

    a.实验准备:超声波传感器,USB转串口模块,ST-Llink下载器,CHEAPX机器人控制板

    b.实验目的:STM32实现超声测距传感器数据的采集

    c.相关知识点:

    本次碰撞模块使用开关按键式碰撞,开关按键常常伴有抖动,因此需要软件消除抖动。

    d.编程及运行

    (1)初始化

    void initSonar(void)
    {
     
            GPIO_InitTypeDef  GPIO_InitStruct;
          TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
        
          //Config Clock 配置时钟
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);     
            RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
        
            //TRIG IO
            GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;                
            GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出     
            GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;         
            GPIO_Init(GPIOC, &GPIO_InitStruct);//传入结构体GPIO_InitStruct,配置GPIOC                                              
    
            //ECHO IO
            GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;                
            GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; //下拉输入     
            GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;         
            GPIO_Init(GPIOC, &GPIO_InitStruct);                                              
             
            //timer
            TIM_TimeBaseStruct.TIM_ClockDivision = 0;               //时钟分割,不分割,即为该定制器时钟最大值72M
            TIM_TimeBaseStruct.TIM_Prescaler     = 719;             //预分频值,719+1分频,即计数频率100K频率
            TIM_TimeBaseStruct.TIM_Period        = 10000;           //最大计数值,最长计时10000/100K s=0.1s
        TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);            //传入结构TIM_TimeBaseStruct,配置TIM4
            
        TIM_ARRPreloadConfig(TIM2, ENABLE);//开启自动装载
        TIM_Cmd(TIM2, DISABLE);            //关闭TIM2
        
    }

    (2)更新函数

    u16 updateSonar(void)
    {
        u16 time;     
        u16 distance;    
    
    /* 超声传感器工作时序图:
                ___                                                                                                ___
     TRIG   ___|   |______________________________________________________________________________________________|   |______
               >10us _   _   _   _   _   _   _   _                      间隔至少 10ms 
     发射头 _________| |_| |_| |_| |_| |_| |_| |_| |_________________________________________________________________________
                      发送 8 个40KHz 超声波
        
                                                                 _   _   _   _   _   _   _   _          
     接收头 _____________________________________________________| |_| |_| |_| |_| |_| |_| |_| |_____________________________
                                                                 接收到回波
    
                      ___________________________________________
     ECHO   _________|                                           |___________________________________________________________
    */
        
        //Trig拉高>10us,发射头发射超声
        GPIO_SetBits(GPIOC,GPIO_Pin_8);
        delay_us(20);
        GPIO_ResetBits(GPIOC,GPIO_Pin_8);
        
        while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)==0);//等待ECHO脚高电平(最多等待10ms)        
        TIM2->CNT=0;TIM_Cmd(TIM2, ENABLE);//开始计时
      while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)==1);//等待ECHO脚高电平(最多等待10ms)    
        TIM_Cmd(TIM2, DISABLE);//停止计时
        
        time=TIM2->CNT;      //unit:10us                      
        distance =time*0.17; //distance=V*time/2 =340m/s * time/2 = 0.340cm/10us *time/2 = time*0.17 cm
        
        return distance;
    }

    (3)主函数

    //超声测距实验
    int main(void)
    {
        u8 i;
        u16 adcx=0;
        u16 volt=0;
        
         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //PriorityGroupConfig NVIC中断分组设置 组2(2位抢占优先级,2位响应优先级)
        initDebugSerial(500000);//初始化调试串口USART1,波特率500000
        initSysTick();//初始化滴答定时器和TIM4定时器
        showVersion();//显示版本
        initBattery();//初始化电池检测
        initSonar();  //初始化超声传感器
        
        while(1)
        {
            printf("sonar:%3u
    ",updateSonar());
            delay_ms(100);
        }
        
    }

    (4)实验结果

  • 相关阅读:
    Flutter Android ERROR: ensureInitializationComplete must be called after startInitialization
    MySQL 创建用户表和好友表
    Android 广播的使用 文档使用的是 kotlin版本
    Flutter 分段器的使用
    Flutter 计算两个日期之间相差多少天,生成区间随机数
    Flutter 多环境
    Android ListView 的使用 Kotlin
    Android 自定义控件----基础
    Flutter 学习(3)------------通过List或者map循环数据渲染页面。。
    vs2015 编译报错:The project references NuGet package(s) that are missing on this computer...
  • 原文地址:https://www.cnblogs.com/Baron-Lu/p/13378729.html
Copyright © 2011-2022 走看看