找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32F103捕获功能实现超声波测距程序

[复制链接]
跳转到指定楼层
楼主
ID:82220 发表于 2020-11-7 20:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
学习stm32的捕获功能,能够实现2米内精确测距。
#include "stm32f10x.h"
#include "input_capture.h"
#include <stdio.h>

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
void delay_us(u16 time)
{
        u16 i;
        for(i=0;i<time*8;i++);
}



/* Private functions ---------------------------------------------------------*/
void start_measor_init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        GPIO_InitTypeDef GPIO_InitStruct;
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);
        GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
void start_measor_start(void)
{

        TIM_CCxCmd(TIM2, TIM_Channel_1, TIM_CCx_Enable);
        GPIO_SetBits(GPIOA, GPIO_Pin_1);
        delay_us(40);
        GPIO_ResetBits(GPIOA, GPIO_Pin_1);
        



}
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
         start_measor_init();
         input_capture_init();
                flag=0;
  while (1)
  {
         unsigned i;
                i++;
         if(i%100==0)
         {
                 start_measor_start();
         }
  }
}


#include "input_capture.h"
#include "stm32f10x_tim.h"
unsigned char flag;
float distance ;
void input_capture_init(void)
{

        
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        GPIO_InitTypeDef GPIO_InitStruct;
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruct);

        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
        TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
        TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
        TIM_TimeBaseInitStruct.TIM_Period=65535;
        TIM_TimeBaseInitStruct.TIM_Prescaler=7199;
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);

        
        TIM_ICInitTypeDef TIM_ICInitStruct;
  TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
  TIM_ICInitStruct.TIM_ICFilter=0xF;
  TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
        TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
        TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
        TIM_ICInit(TIM2, &TIM_ICInitStruct);
         
         NVIC_InitTypeDef NVIC_InitStruct;
         NVIC_InitStruct.NVIC_IRQChannel=TIM2_IRQn;
         NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
         NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
         NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
   NVIC_Init(&NVIC_InitStruct);
         
        // TIM_Cmd(TIM3,ENABLE );         
   //TIM_CCxCmd(TIM3, TIM_Channel_1, TIM_CCx_Enable);
         //TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
        
        TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
        TIM_Cmd(TIM2,ENABLE );
        
}

void TIM2_IRQHandler(void)
{
        u16 record_rising,record_falling;

        if( TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
        {
                if(flag==1)
                {        record_falling=TIM_GetCapture1        (        TIM2        );
                        distance=(float)record_falling*340/200.0;
                        //record_rising=TIM_GetCapture1        (        TIM2        );
      flag=0;
                        TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
                        
                }
                else
                {
                        record_rising=0;
                        record_falling=0;
                         TIM_SetCounter(TIM2,0);
                  TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling);                //CC1P=1 设置为下降沿捕获
                 flag=1;
                //        
                        
                }
        
        }
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}



template 基础超声波测距.7z

583.26 KB, 下载次数: 23, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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