標題: stm32f407-hcsr04超聲波 [打印本頁]

作者: YD1633732177    時間: 2020-6-3 14:25
標題: stm32f407-hcsr04超聲波
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "voice.h"


int main()
{
    u16 times,i=5,all;
    long long truth;
  int  distance=0;

    uart_init(115200);//初始化串口波特率為115200
     voice_int();
     delay_init(168);
    while(1)
    {
        for(i=0;i<5;i++)
                    {

                    printf("2\r\n");
                        
                    GPIO_SetBits(GPIOB, GPIO_Pin_0);    //給20um的高電平   
                    delay_ms(10);
                    GPIO_ResetBits(GPIOB, GPIO_Pin_0);
                        
                    printf("3\r\n");
                TIM3->CNT = 0;
             while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)  ==0);    //等待echo->low               
                                //計數(shù)器清零,開始計數(shù)
                TIM_Cmd(TIM3, ENABLE);//開啟定時器        
        while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)  ==1);//等待echo->high
                    TIM_Cmd(TIM3, DISABLE);     //關(guān)閉定時器
printf("5\r\n");
                        
                        
                    times = TIM_GetCounter(TIM3);
                    distance = (times*340)/200;  //(1/200000)*344/2=0.00085m
                    all+=distance;
                     printf("************");
                    }
     
            truth = all/5;
            all=0;
          printf("距離為cm:%lld\r\n",truth);
            
                }
}     

super voice.7z

299.03 KB, 下載次數(shù): 35, 下載積分: 黑幣 -5


作者: kcl21    時間: 2021-9-19 09:00
測試過嗎




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1