|
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "hcsr04.h"
int main(void)
{
float length;
delay_init(); //延时函数初始化
uart_init(115200); //串口初始化为115200
HCSR04_Init();
printf("超声波初始化成功");//检测超声波是否初始化
length=gettime();
printf("距离为:%.3f cm\n",length);//打印总的距离
}
//以上是主函数模块
#include "hcsr04.h"
#include "delay.h"
u16 msHcCount =0;
void HCSR04_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //PB.13 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //PB.14 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.14
//定时器TIM3初始化
TIM_TimeBaseStructure.TIM_Period =10000-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =7200-1; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
hcsr04_NVIC();
TIM_Cmd(TIM3,ENABLE);
}
static void opentimerhc()
{
TIM_SetCounter(TIM2,0);
msHcCount=0;
TIM_Cmd (TIM2,ENABLE);
}
static void closeimerhc()
{
TIM_Cmd(TIM2,DISABLE);
}
void hcsr04_NVIC(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
}
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
msHcCount++;
}
}
u32 gettime(void)//时间获取函数
{
u32 t=0;
t=msHcCount*1000;
t+=TIM_GetCounter(TIM2);
TIM2->CNT=0;
delay_ms(50);
}
float hcgetlength(void)
{
u32 t=0;
int i=0;
float lengthTemp=0;
float sum=0;
while(i!=5)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_13); //预拉低trig引脚
delay_ms(20);
GPIO_SetBits(GPIOB,GPIO_Pin_13);
delay_ms(20);
GPIO_ResetBits(GPIOB,GPIO_Pin_13);//发出脉冲
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==0)//等待信号发出,信号发出,计数器置0,同时变为高电平
{
opentimerhc();
i=i+1;
}
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==1)//等待信号接收,信号发出时,引脚一直置1
{
closeimerhc();
t=gettime();
lengthTemp=((float)t*170/1000);
sum=lengthTemp+sum;
}
lengthTemp=sum/5.0;
return lengthTemp;
}
}
这是超声波模块
是程序写错了吗?
如果有人能指出错误,请私我
|
|