- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "led.h"
- #include "key.h"
- //超声波硬件接口定义
- #define HCSR04_PORT GPIOB
- #define HCSR04_CLK RCC_APB2Periph_GPIOB
- #define HCSR04_TRIG GPIO_Pin_11
- #define HCSR04_ECHO GPIO_Pin_10
-
- //超声波计数
- u16 msHcCount = 0;
- //定时器4设置
- void hcsr04_NVIC()
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- //IO口初始化 及其他初始化
- void Hcsr04Init()
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(HCSR04_CLK, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin =HCSR04_TRIG;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);
- GPIO_ResetBits(HCSR04_PORT,HCSR04_TRIG);
-
- GPIO_InitStructure.GPIO_Pin = HCSR04_ECHO;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);
- GPIO_ResetBits(HCSR04_PORT,HCSR04_ECHO);
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
-
- TIM_DeInit(TIM4);
- TIM_TimeBaseStructure.TIM_Period = (999);
- TIM_TimeBaseStructure.TIM_Prescaler =(71);
- 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);
- TIM_Cmd(TIM4,DISABLE);
- }
- //打开定时器4
- static void OpenTimerForHc()
- {
- TIM_SetCounter(TIM4,0);
- msHcCount = 0;
- TIM_Cmd(TIM4, ENABLE);
- }
- //关闭定时器4
- static void CloseTimerForHc()
- {
- TIM_Cmd(TIM4, DISABLE);
- }
- //定时器4终中断
- void TIM4_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
- {
- msHcCount++;
- TIM_ClearITPendingBit(TIM4, TIM_IT_Update );
-
- }
- }
-
- //获取定时器4计数器值
- u32 GetEchoTimer(void)
- {
- u32 t = 0;
- t = msHcCount*1000;
- t += TIM_GetCounter(TIM4);
- TIM4->CNT = 0;
- delay_ms(50);
- return t;
- }
-
- //通过定时器4计数器值推算距离
- float Hcsr04GetLength(void )
- {
- u32 t = 0;
- int i = 0;
- float lengthTemp = 0;
- float sum = 0;
- while(i!=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();
- lengthTemp = ((float)t*0.034/2);//cm
- sum = lengthTemp + sum ;
-
- }
- lengthTemp = sum/5.0;
- return lengthTemp;
- }
-
- //测试主函数
- int main(void)
- {
- float length;
- delay_init(); //延时函数初始化
- //NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- hcsr04_NVIC();
- uart_init(9600); //串口初始化为9600
- Hcsr04Init();
- LED_Init();
-
- // Hcsr04Init();
-
- while(1)
- {
- length = Hcsr04GetLength();
- printf("距离为:%.3fcm\n",length);
- if(length>100)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_0);
- }
- else
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- }
- delay_ms(50);
- }
- }
复制代码
代码:
超声波(用).7z
(219.91 KB, 下载次数: 36)
|