如题,定时器3的超声波输入捕获可以正常工作,定时器5的不能捕获成功。程序如下
定时器3的程序
TIM_ICInitTypeDef TIM3_ICInitStructure;
void TIM3_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //2M
GPIO_Init(GPIOC, &GPIO_InitStructure);
//3õê¼»ˉ¶¨ê±Æ÷3 TIM3
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM3_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM3,ENABLE );
}
u16 TIM3CH3_CAPTURE_STA,TIM3CH3_CAPTURE_VAL;
void Read_Distance1(void)
{
PCout(2)=1;
delay_us(15);
PCout(2)=0;
delay_ms(100);
if(TIM3CH3_CAPTURE_STA&0X80)
{
Distance1=TIM3CH3_CAPTURE_STA&0X3F;
Distance1*=65536;
Distance1+=TIM3CH3_CAPTURE_VAL;
Distance1=Distance1*170/1000;
//printf("%d
",Distance1);
TIM3CH3_CAPTURE_STA=0;
}
TIM3_DelayFinish_FLAG=1;
}
void TIM3_IRQHandler(void)
{
if((TIM3CH3_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
if(TIM3CH3_CAPTURE_STA&0X40)
{
if((TIM3CH3_CAPTURE_STA&0X3F)==0X3F)
{
TIM3CH3_CAPTURE_STA|=0X80;
TIM3CH3_CAPTURE_VAL=0XFFFF;
}
else TIM3CH3_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
{
if(TIM3CH3_CAPTURE_STA&0X40)
{
TIM3CH3_CAPTURE_STA|=0X80;
TIM3CH3_CAPTURE_VAL=TIM_GetCapture3(TIM3);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising);
}else
{
TIM3CH3_CAPTURE_STA=0;
TIM3CH3_CAPTURE_VAL=0;
TIM3CH3_CAPTURE_STA|=0X40;
TIM_Cmd(TIM3,DISABLE );
TIM_SetCounter(TIM3,0);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling);
TIM_Cmd(TIM3,ENABLE );
}
}
}
TIM3->SR=0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
定时器5的程序如下
TIM_ICInitTypeDef TIM5_ICInitStructure;
void TIM5_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM5,ENABLE );
}
u16 TIM5CH3_CAPTURE_STA=0,TIM5CH3_CAPTURE_VAL=0;
void Read_Distance2(void)
{
PCout(7)=1;
delay_us(15);
PCout(7)=0;
delay_ms(100);
if(TIM5CH3_CAPTURE_STA&0X80)//3é1|2¶»ñμ½áËò»′θßμçƽ
{
Distance2=TIM5CH3_CAPTURE_STA&0X3F;
Distance2*=65536;
Distance2+=TIM5CH3_CAPTURE_VAL;
Distance2=Distance2*170/1000;
//printf("%d
",Distance2);
TIM5CH3_CAPTURE_STA=0;
}
TIM5_DelayFinish_FLAG=1;
}
void TIM5_IRQHandler(void)
{
if((TIM5CH3_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH3_CAPTURE_STA&0X40)
{
if((TIM5CH3_CAPTURE_STA&0X3F)==0X3F)
{
TIM5CH3_CAPTURE_STA|=0X80;
TIM5CH3_CAPTURE_VAL=0XFFFF;
}
else TIM5CH3_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC3) != RESET)
{
if(TIM5CH3_CAPTURE_STA&0X40)
{
TIM5CH3_CAPTURE_STA|=0X80;
TIM5CH3_CAPTURE_VAL=TIM_GetCapture3(TIM5);
TIM_OC3PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}else
{
TIM5CH3_CAPTURE_STA=0;
TIM5CH3_CAPTURE_VAL=0;
TIM5CH3_CAPTURE_STA|=0X40;
TIM_Cmd(TIM5,DISABLE );
TIM_SetCounter(TIM5,0);
TIM_OC3PolarityConfig(TIM5,TIM_ICPolarity_Falling);
TIM_Cmd(TIM5,ENABLE );
}
}
}
TIM5->SR=0;
}
一周热门 更多>