用stm32的定时器捕获功能 发现只能进一下捕获中断 然后不停的进到更新中断里面去,更新中断和捕获中断标志位都清掉了 附上代码,求大神指导!!!
定时器初始化:
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, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
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_2;
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 = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
中断服务处理函数:
void TIM3_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET )
{
i1++;
}
if((TIM_GetITStatus(TIM3,TIM_IT_CC2) == SET))
{
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 1)
{
i2++;
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling);
}
else if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0)
{
i3++;
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising);
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_Update|TIM_IT_CC2);
}
while循环打印 i的值结果是:
int i = 0;
for(i = 0;i<100;i++)
{
printf("i1 = %d.\r\n",i1);
printf("i2 = %d.\r\n",i2);
printf("i3 = %d.\r\n",i3);
}
打印结果是:
i3 = 0.
i1 = 35.
i2 = 1.