找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1557|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32F103c8t6 測距

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:922394 發(fā)表于 2021-5-16 21:59 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
hs04.c
_____________________
#include "hsr04.h"
#include <string.h>
#include <stdio.h>
#include "delay.h"

#define Trig PBout(0) // PB0
#define Echo PBin(1) // PB1




void HCSR04_Init(u16 arr,u16 psc)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能TIM2時(shí)鐘
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA/GPIOB時(shí)鐘

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;//下拉輸入
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    GPIO_ResetBits(GPIOB,GPIO_Pin_1);//PB1 下拉

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);//PB0 推挽輸出

       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;//下拉輸入
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_ResetBits(GPIOA,GPIO_Pin_1);//PB1 下拉

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4 | GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);//PB0 推挽輸出
       
       
    //初始化定時(shí)器 2 TIM2
    TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)定計(jì)數(shù)器自動(dòng)重裝值
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //預(yù)分頻器
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設(shè)置時(shí)鐘分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //初始化 TIM2


}





//一號超聲波 前方
float hsr04_get(void)
{
        int rate_time =0;
        uint16_t cnt=0;
        float dis=0;

        //發(fā)出起始信號
        Trig=0;
        DelayUs(10);            
        Trig=1;
        DelayUs(15);                              //輸入一個(gè)10us的高電平
        Trig=0;
       
        while(Echo == 0);                          //超聲波發(fā)射出去
        TIM_Cmd(TIM2 ,ENABLE);                          //開啟定時(shí)器
        while(Echo == 1)                         //超聲波反射回來,來回時(shí)間不超過20ms,單程距離不超過3.4m
        {
                DelayUs(20);
                rate_time ++;
                if(rate_time>=1000)
                        break;       
        }
        TIM_Cmd(TIM2 ,DISABLE);                  //關(guān)閉定時(shí)器

       
        cnt = TIM_GetCounter(TIM2);                //獲取定時(shí)器的計(jì)數(shù)值
        dis = ((float)cnt) / 58 ;            //單位:cm
        TIM2->CNT = 0;

        return dis;               
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表