STM32芯片定時(shí)器輸入捕獲模式
#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);//調(diào)用庫(kù)函數(shù),開(kāi)啟了GPIOA的外設(shè)時(shí)鐘,這句代碼的意思應(yīng)該是,開(kāi)啟了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); //打開(kāi)定時(shí)器TIM2的時(shí)鐘
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;//上升沿觸發(fā),還有TIM_ICPolarity(輸入活動(dòng)沿),TIM_ICPolarity_Falling輸入捕獲下降沿可以選擇 ;一旦IC1為上升沿觸發(fā),則IC2就為下降沿觸發(fā),因?yàn)閮烧哂|發(fā)極性相反
TIM_ICInitStructure1.TIM_ICSelection=TIM_ICSelection_DirectTI;//TIM_ICSelection選擇輸入;TIM_ICSelection_DirectTI:TIM輸入2,3或4選擇對(duì)應(yīng)的IC1或IC2或IC3或IC4相連;TIM_ICSelection_IndirectTI:TIM輸入2,3或4選的對(duì)應(yīng)與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為最高的優(yōu)先級(jí)
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);
//這個(gè)函數(shù)中,加粗的代碼對(duì)定時(shí)器TIM2進(jìn)行了一次完整的配置
}
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);//調(diào)用庫(kù)函數(shù),開(kāi)啟了GPIOA的外設(shè)時(shí)鐘,這句代碼的意思應(yīng)該是,開(kāi)啟了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); //打開(kāi)定時(shí)器TIM2的時(shí)鐘
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;//上升沿觸發(fā),還有TIM_ICPolarity(輸入活動(dòng)沿),TIM_ICPolarity_Falling輸入捕獲下降沿可以選擇 ;一旦IC1為上升沿觸發(fā),則IC2就為下降沿觸發(fā),因?yàn)閮烧哂|發(fā)極性相反
TIM_ICInitStructure1.TIM_ICSelection=TIM_ICSelection_DirectTI;//TIM_ICSelection選擇輸入;TIM_ICSelection_DirectTI:TIM輸入2,3或4選擇對(duì)應(yīng)的IC1或IC2或IC3或IC4相連;TIM_ICSelection_IndirectTI:TIM輸入2,3或4選的對(duì)應(yīng)與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為最高的優(yōu)先級(jí)
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);
//這個(gè)函數(shù)中,加粗的代碼對(duì)定時(shí)器TIM2進(jìn)行了一次完整的配置
}
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);
}
評(píng)論