#include"HC04.h"
#include"SysTick.h"
extern uint32_t L;
void HC04_Config1(void)
{
GPIO_InitTypeDef GPIO_InitStructure1;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure1;
TIM_ICInitTypeDef TIM_ICInitStructure1;
NVIC_InitTypeDef NVIC_InitStructure2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//调用库函数,开启了GPIOA的外设时钟,这句代码的意思应该是,开启了GPIOA的使用
GPIO_InitStructure1.GPIO_Pin=(HC04_Echo0)|(HC04_Echo1)|(HC04_Echo2)|(HC04_Echo3);
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开定时器TIM2的时钟
TIM_DeInit(TIM2);
TIM_TimeBaseStructure1.TIM_Period =0xFFFF;
TIM_TimeBaseStructure1.TIM_Prescaler =1-1;
TIM_TimeBaseStructure1.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseStructure1.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure1);
TIM_ICInitStructure1.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure1.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿触发,还有TIM_ICPolarity(输入活动沿),TIM_ICPolarity_Falling输入捕获下降沿可以选择 ;一旦IC1为上升沿触发,则IC2就为下降沿触发,因为两者触发极性相反
TIM_ICInitStructure1.TIM_ICSelection=TIM_ICSelection_DirectTI;//TIM_ICSelection选择输入;TIM_ICSelection_DirectTI:TIM输入2,3或4选择对应的IC1或IC2或IC3或IC4相连;TIM_ICSelection_IndirectTI:TIM输入2,3或4选的对应与IC2或IC1或IC4或IC3相连;TIM_ICSelection_TRC:TIM输入2,3或3选择与TRC相连
TIM_ICInitStructure1.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure1);
TIM_ICInit(TIM2, &TIM_ICInitStructure1);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_ClearFlag(TIM2, TIM_IT_CC2);
TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM2, ENABLE);
NVIC_InitStructure2.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1; //0为最高的优先级
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);
//这个函数中,加粗的代码对定时器TIM2进行了一次完整的配置
}
void HC04_Config2(void)
{
GPIO_InitTypeDef GPIO_InitStructure2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure2.GPIO_Pin=(HC04_Trig0)|(HC04_Trig1)|(HC04_Trig2)|(HC04_Trig3);
GPIO_InitStructure2.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure2.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure2);
GPIO_ResetBits(GPIOA,HC04_Trig0|HC04_Trig1|HC04_Trig2|HC04_Trig3);
}
void HC04_Config(void)
{
HC04_Config2();
}
uint32_t DistanceHDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig0);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig0);
HC04_Config1();
return(L);
}
uint32_t DistanceDDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig1);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig1);
HC04_Config1();
return(L);
}
uint32_t DistanceLDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig2);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig2);
HC04_Config1();
return(L);
}
uint32_t DistanceRDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig3);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig3);
HC04_Config1();
return(L);
}
#include"HC04.h"
#include"SysTick.h"
extern uint32_t L;
void HC04_Config1(void)
{
GPIO_InitTypeDef GPIO_InitStructure1;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure1;
TIM_ICInitTypeDef TIM_ICInitStructure1;
NVIC_InitTypeDef NVIC_InitStructure2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//调用库函数,开启了GPIOA的外设时钟,这句代码的意思应该是,开启了GPIOA的使用
GPIO_InitStructure1.GPIO_Pin=(HC04_Echo0)|(HC04_Echo1)|(HC04_Echo2)|(HC04_Echo3);
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开定时器TIM2的时钟
TIM_DeInit(TIM2);
TIM_TimeBaseStructure1.TIM_Period =0xFFFF;
TIM_TimeBaseStructure1.TIM_Prescaler =1-1;
TIM_TimeBaseStructure1.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseStructure1.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure1);
TIM_ICInitStructure1.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure1.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿触发,还有TIM_ICPolarity(输入活动沿),TIM_ICPolarity_Falling输入捕获下降沿可以选择 ;一旦IC1为上升沿触发,则IC2就为下降沿触发,因为两者触发极性相反
TIM_ICInitStructure1.TIM_ICSelection=TIM_ICSelection_DirectTI;//TIM_ICSelection选择输入;TIM_ICSelection_DirectTI:TIM输入2,3或4选择对应的IC1或IC2或IC3或IC4相连;TIM_ICSelection_IndirectTI:TIM输入2,3或4选的对应与IC2或IC1或IC4或IC3相连;TIM_ICSelection_TRC:TIM输入2,3或3选择与TRC相连
TIM_ICInitStructure1.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure1);
TIM_ICInit(TIM2, &TIM_ICInitStructure1);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_ClearFlag(TIM2, TIM_IT_CC2);
TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM2, ENABLE);
NVIC_InitStructure2.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1; //0为最高的优先级
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);
//这个函数中,加粗的代码对定时器TIM2进行了一次完整的配置
}
void HC04_Config2(void)
{
GPIO_InitTypeDef GPIO_InitStructure2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure2.GPIO_Pin=(HC04_Trig0)|(HC04_Trig1)|(HC04_Trig2)|(HC04_Trig3);
GPIO_InitStructure2.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure2.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure2);
GPIO_ResetBits(GPIOA,HC04_Trig0|HC04_Trig1|HC04_Trig2|HC04_Trig3);
}
void HC04_Config(void)
{
HC04_Config2();
}
uint32_t DistanceHDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig0);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig0);
HC04_Config1();
return(L);
}
uint32_t DistanceDDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig1);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig1);
HC04_Config1();
return(L);
}
uint32_t DistanceLDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig2);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig2);
HC04_Config1();
return(L);
}
uint32_t DistanceRDetect(void)
{
GPIO_SetBits(GPIOA,HC04_Trig3);
Delay_us(20);
GPIO_ResetBits(GPIOA,HC04_Trig3);
HC04_Config1();
return(L);
}
上一篇:微型四旋翼飞行器的设计与制作
下一篇:STM32的NVIC理解
推荐阅读最新更新时间:2024-03-16 14:30