cancel
Showing results for 
Search instead for 
Did you mean: 

No interrupt with high frequency signal from PWM input capture

ji0328
Associate
Posted on May 21, 2014 at 05:21

Hi

I try to measure the frequency and duty cycle from timer 2 channel 2 with input from another timer output. The chip is STM32F103 and the TIMxCLK is set 32 Mhz. The interrupt handler is periodically triggered with a 1 Hz input signal. However, when the input signal runs around 5 Hz or higher, the interrupt handler is not triggered. Can anyone help me out? Thanks for your attention. Here is how the code looks like

void pwm_set_input
(
void
)
{
uint16 duty_cycle = 0;
TIM_TypeDef* timer;
TIM_ICInitTypeDef ic_init;
NVIC_InitTypeDef timer_intr;
TIM_TimeBaseInitTypeDef time_base;
NVIC_InitTypeDef NVIC_InitStructure;
/* Initialize timer*/
timer = (TIM_TypeDef*)hwm_TIM2;
/* Initialize RCC*/
//it has been done somewhere else
/* Initialize GPIO*/
//it has been done somewhere else
/* Initialize input timer parameter*/
memset( &time_base, 0, sizeof( time_base ) );
time_base.period = TIMER_COUNTER_MAX_VALUE;
time_base.prescaler = TIMER_COUNTER_MAX_VALUE;
time_base.counter_mode = hwm_TIMER_COUNTERMODE_UP;
TIM_TimeBaseInit( timer, &time_base, duty_cycle );
/* Init interrupt */
NVIC_InitStructure.NVIC_IRQChannel = hwm_TIM2_IRQ_CHANNEL;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = TRUE;
NVIC_Init(&NVIC_InitStructure);
/* Initialize input capture parameter*/
ic_init.ic_channel = hwm_TIMER_IN_Channel_2;
ic_init.ic_polarity = hwm_TIMER_ICPOLARITY_RISING;
ic_init.ic_selection = hwm_TIMER_ICSELECTION_DIRECTTI;
ic_init.ic_prescaler = hwm_TIMER_ICPSC_DIV1;
ic_init.ic_filter = 0x0;
TIM_PWMIConfig( timer, &ic_init );
/* Select the TIMx Input Trigger: TI2FP2 */
TIM_SelectInputTrigger( timer, hwm_TIMER_TI2FP2 );
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode( timer, hwm_TIMER_SLAVEMODE_RESET );
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode( timer, hwm_TIMER_MASTERSLAVEMODE_ENABLE );
/* TIM enable counter */
TIM_Cmd( timer, TRUE );
/* Enable the CC2 Interrupt Request */
TIM_ITConfig( timer, hwm_TIMER_IT_CC2, TRUE );
}
void pwm_tim2_isr
(
void
)
{
uint16 ic1 = 0;
uint16 ic2 = 0;
TIM_TypeDef* hwm_tim_x = (TIM_TypeDef*)hwm_TIM2;
/* Clear TIM2 Capture compare interrupt pending bit */
hwm_tim_x->SR = (uint16)~hwm_TIMER_FLAG_CC2;
hwm_tim_x->SR = (uint16)~hwm_TIMER_FLAG_CC2OF;
/* Get the Input Capture value */
ic1 = hwm_tim_x->CCR1;
ic2 = hwm_tim_x->CCR2;
illum_duty_cycle = ic1;
illum_freq = ic2;
}

0 REPLIES 0