stm32定时器有输入捕获形式,PWM输入形式,强输出形式,输出比较形式这几种。
#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);
}