亚洲春色中文字幕久久久-三上亚,一吻二脱三床四吻胸,国产真实伦对白视频全集,在线毛片观看,精品成品入口黄网,国产毛aⅴ片久久久,亚洲AV色香蕉一区二区三区老师,萧皇后A级艳片,色情日本视频更新,99久久亚洲精品日本无码

標(biāo)題: 基于stm32f4的超聲波測距代碼 [打印本頁]

作者: sabo1655    時(shí)間: 2022-3-31 20:50
標(biāo)題: 基于stm32f4的超聲波測距代碼
通過使用定時(shí)器的計(jì)數(shù)來實(shí)現(xiàn)HCSR04超聲波模塊測距的功能。

單片機(jī)源程序如下:
  1. #include "timer.h"
  2. #include "hc.h"
  3. #include "sys.h"
  4. #include "usart.h"
  5. #include "delay.h"
  6. #include "stdio.h"
  7. //PA9為模塊trig引腳接口
  8. //PA10為Echo引腳接口

  9. int main(void)
  10. {
  11.         static u8 time;//單程運(yùn)行時(shí)間,單位s
  12.         static u8 length;//路程,單位m
  13.         uart_init(115200);        //串口初始化波特率為115200
  14.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置系統(tǒng)中斷優(yōu)先級分組2
  15.         delay_init(168);  //初始化延時(shí)函數(shù)
  16.         while(1)
  17.         {
  18.   TIM3_Int_Init(100-1,16800-1);   //初始化TIM3定時(shí)器,計(jì)數(shù)周期為0.02s
  19.   
  20.                 if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中斷
  21.                 {
  22.         while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)==1);//echo為高電平時(shí),則等待至低電平,才啟動(dòng)超聲波
  23.         GPIO_SetBits(GPIOA,GPIO_Pin_9);    //TRIG置高
  24.         delay_us(20);
  25.         TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
  26.                 }
  27.                
  28.                 while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 0)//等待echo的高電平到來        
  29.            TIM_SetCounter(TIM2,0); //清零計(jì)數(shù)器
  30.                
  31.                 TIM_Cmd(TIM3, ENABLE);  //使能定時(shí)器3,開始計(jì)數(shù)
  32.                
  33.                 while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 1);//等待echo的高電平結(jié)束
  34.                 TIM_Cmd(TIM3, DISABLE);        //失能定時(shí)器3,截止計(jì)數(shù)
  35.                
  36.                 TIM_GetCounter(TIM2);//得到計(jì)數(shù)器的值
  37.                 time=TIM_GetCounter(TIM2)/20000;//單程時(shí)間
  38.                 length=time*344;//計(jì)算出長度
  39.                
  40.                 printf("\r\n當(dāng)前距離為:\r\n");
  41.         }
  42. }
復(fù)制代碼

Keil代碼下載:
代碼.7z (314.21 KB, 下載次數(shù): 29)







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