我的项目中用到TIM2 的 ch4做为外部波形频率监测,我的代码如下,但是程序始终无法进入中断,同样的代码,用到TIM4 的 CH2上功能就是正常的,无语。再此求助各位,望大家给分析一下原因,谢谢!
/**
* @ 名 称:TIM2_Configuration
* @ 功 能:Timer4 初始化,ch4 作为脉冲捕获通道
* @ 入口参数:none
* @ 出口参数:none
*/
void TIM2_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOB clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* TIM4 channel 2 pin (PB.07) configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_TIM2);
/* TIM4 configuration: Input Capture mode ---------------------
The external signal is connected to TIM4 CH2 pin (PB.07)
The Rising edge is used as active edge,
The TIM4 CCR2 is used to compute the frequency value
------------------------------------------------------------ */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_Cmd(TIM2, ENABLE); // TIM enable counter
/* Enable the CC2 Interrupt Request */
// TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE); // Enable Timer4 interruption
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); // Clear TIM4 Capture compare interrupt pending bit
if(CaptureNumber == 0)
{
IC4ReadValue1 = TIM_GetCapture2(TIM2); // Get the Input Capture value
CaptureNumber = 1;
}
else if(CaptureNumber == 1)
{
IC4ReadValue2 = TIM_GetCapture2(TIM2); // Get the Input Capture value
if (IC4ReadValue2 > IC4ReadValue1) // Capture computation
{
Capture += (IC4ReadValue2 - IC4ReadValue1) - 1;
count++;
}
else if (IC4ReadValue2 < IC4ReadValue1)
{
Capture += ((0xFFFF - IC4ReadValue1) + IC4ReadValue2) - 1;
count++;
}
else
{
Capture += 0;
}
CaptureNumber = 0;
}
}
}
|