我用PA7口复用TIM3_CH2 做的一个输入捕获实验,程序一开始就不断进入TIM3的中断,除了第一次能清除中断标志位,后面就无法清除中断标志位。程序如下:
void Tim3_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; // 50HZ
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_1);
TIM_DeInit(TIM3);
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535; //
TIM_TimeBaseInitStruct.TIM_Prescaler = 47; //
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
TIM_ICInitStruct.TIM_ICFilter = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICSelection = 0;
TIM_ICInit(TIM3,&TIM_ICInitStruct);
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPriority = 2;
NVIC_Init(&NVIC_InitStruct);
TIM_ITConfig(TIM3,TIM_IT_Update ,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC2 ,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2|TIM_IT_Update); //Çå³ýÖжϱê־λ
if((TIM3CH2_CAPTURE_STA&0X80)==0)//»¹Î´³É¹¦²¶»ñ
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//Òç³ö
{
if(TIM3CH2_CAPTURE_STA&0X40)//ÒѾ²¶»ñµ½¸ßµçƽÁË
{
if((TIM3CH2_CAPTURE_STA&0X3F)==0X3F)//¸ßµçƽ̫³¤ÁË
{
TIM3CH2_CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñÁËÒ»´Î
TIM3CH2_CAPTURE_VAL=0XFFFFFFFF;
end_flag = 1;
}else TIM3CH2_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//²¶»ñ1·¢Éú²¶»ñʼþ
{
if(TIM3CH2_CAPTURE_STA&0X40) //²¶»ñµ½Ò»¸öϽµÑØ
{
TIM3CH2_CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñµ½Ò»´Î¸ßµçƽÂö¿í
TIM3CH2_CAPTURE_VAL=TIM_GetCapture1(TIM3);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ.
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ
}else //»¹Î´¿ªÊ¼,µÚÒ»´Î²¶»ñÉÏÉýÑØ
{
TIM3CH2_CAPTURE_STA=0; //Çå¿Õ
TIM3CH2_CAPTURE_VAL=0;
TIM3CH2_CAPTURE_STA|=0X40; //±ê¼Ç²¶»ñµ½ÁËÉÏÉýÑØ
TIM_Cmd(TIM3,DISABLE ); //¹Ø±Õ¶¨Ê±Æ÷5
TIM_SetCounter(TIM3,0);
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 ÉèÖÃΪϽµÑز¶»ñ
TIM_Cmd(TIM3,ENABLE ); //ʹÄܶ¨Ê±Æ÷5
}
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2|TIM_IT_Update); //Çå³ýÖжϱê־λ
}
int main(void)
{
Tim3_Init();
while(1)
{
}
}
|