注意定时器管脚与模块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)
|