注意定時器管腳與模塊Ech腳相連,由于時間緊迫,沒試過不使用定時器對應的腳與模塊Ech腳相連,會不會達到同樣的現象。
單片機源程序如下:
- #include "Ultrasonicwave.h"
- #include "sys.h"
- //#define HCSR04_PORT GPIOA
- //#define HCSR04_CLK RCC_APB2Periph_GPIOA
- //#define HCSR04_TRIG GPIO_Pin_5
- //#define HCSR04_ECHO GPIO_Pin_1
- #define TRIG_Send PAout(5)
- #define ECHO_Reci PBin(6)
- u16 msHcCount = 0;
- void Hcsr04Init()
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通輸出模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,GPIO_Pin_5);
-
-
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
-
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB,GPIO_Pin_6);
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
-
- TIM_DeInit(TIM4);
- TIM_TimeBaseStructure.TIM_Period = (1000-1);
- TIM_TimeBaseStructure.TIM_Prescaler =(84-1);
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- TIM_ClearFlag(TIM4, TIM_FLAG_Update);
- TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
- hcsr04_NVIC();
- TIM_Cmd(TIM4,DISABLE);
- }
- //
- static void OpenTimerForHc()
- {
- TIM_SetCounter(TIM4,0);
- msHcCount = 0;
- TIM_Cmd(TIM4, ENABLE);
- }
-
- static void CloseTimerForHc()
- {
- TIM_Cmd(TIM4, DISABLE);
- }
-
-
- void hcsr04_NVIC()
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void TIM4_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM4, TIM_IT_Update );
- msHcCount++;
- }
- }
-
- u32 GetEchoTimer(void)
- {
- u32 t = 0;
- t = msHcCount*1000;
- t += TIM_GetCounter(TIM4);
- TIM4->CNT = 0;
- delay_ms(50);
- return t;
- }
-
- float Hcsr04GetLength(void )
- {
- u32 t = 0;
- int i = 0;
- float lengthTemp = 0;
- float sum = 0;
- while(i!=5)//循環5次消除余震
- {
- TRIG_Send = 1;
- delay_us(20);
- TRIG_Send = 0;
- while(ECHO_Reci == 0);
- OpenTimerForHc();
- i = i + 1;
- while(ECHO_Reci == 1);
- CloseTimerForHc(); //關閉定時器
- t = GetEchoTimer(); //獲取US級時間
- lengthTemp = ((float)t/58.0);//cm
- sum = lengthTemp + sum ;
-
- }
- lengthTemp = sum/5.0;
- return lengthTemp;
- }
復制代碼
所有資料51hei提供下載:
基于stm32f4zgt6超聲波模塊測距.7z
(303.96 KB, 下載次數: 55)
2019-3-18 22:41 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|