cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 TImer 2 Channel 3 ICU Config

krithik
Associate II
Posted on May 12, 2015 at 21:42

I'm trying to configure the STM32F4 MCU to use Timer 2 Channel 3 as an Input Capture Unit to measure frequency on an external signal. But so far, I'm not able to trigger the interrupt:

static void TIM_Config(void)

{

    TIM_ICInitTypeDef  TIM_ICInitStructure;

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    NVIC_InitTypeDef NVIC_InitStructure;

    //Enable and Set Timer 2 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);

    //GPIO Config

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(1));

    //Timer 2 Config

    TIM_DeInit(TIM2);

    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

    TIM_TimeBaseStructure.TIM_Prescaler = (STM32_SYSCLK / 1000000) - 1; // 1 MHz, from 48 MHz

    TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal, TIM2 is 32-bit counter

    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // Rising/Falling/BothEdge

    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

    TIM_ICInitStructure.TIM_ICFilter = 0x0;

    TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;

    TIM_ICInit(TIM2, &TIM_ICInitStructure);

    TIM_Cmd(TIM2, ENABLE);

    TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);

}

void TIM2_IRQHandler(void) {

    if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) {

        /* Clear TIM2_CH1 Capture compare interrupt pending bit */

        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

    }

}

#icu #stm32 #mcu #timer
4 REPLIES 4
Posted on May 13, 2015 at 03:00

I'd probably order some of this differently.

How are you determining the interrupt is not occurring?

Not familiar with your pin configuration technique/library.

Your IRQ Handler is looking for CC1 not CC3.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
krithik
Associate II
Posted on May 16, 2015 at 01:47

So, I know I have the settings correct because I'm able to trigger the interrupt when I have 

PA2 (commented below) to do the same thing and it seems to be working perfectly fine. I've set a breakpoint on the interrupt routine and pumping in a 10 Hz Signal on the pin. 

Using a scope, I was able to determine that the PIN seems to be pulling down the (5V) signal to around 1.2 V, but I have not noticed the same behavior on PA2.

TIM_ICInitTypeDef  TIM_ICInitStructure;

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

NVIC_InitTypeDef NVIC_InitStructure;

//Enable and Set Timer 2 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);

//GPIO Config

// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

// palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(1));

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(1));

//Timer 2 Config

TIM_DeInit(TIM2);

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Prescaler = 0xFFFF;

TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;

TIM_TimeBaseStructure.TIM_ClockDivision = 0x00;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // Rising/Falling/BothEdge

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0x0;

TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;

TIM_ICInit(TIM2, &TIM_ICInitStructure);

// enable counter

TIM_Cmd(TIM2, ENABLE);

// enable cc3 interrupt request

TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);

Posted on May 16, 2015 at 02:35

You've not mentioned a board, or provided any design information.

Review what the pins you're complaining about are connected too. Or what your unspecified library code is actually doing to their configuration, and if the syntax is correct.

Think about how to present your problem in a manner that someone who hasn't worked on your design might need to understand why it's not working as expected.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
krithik
Associate II
Posted on May 20, 2015 at 01:30

MCU: STM32F407VET6

Library: ChibiOS and Peripheral Driver

Pin: 47/PB10/TIM2_CH3

Custom Board

I'm trying to measure the frequency on an external signal sent from a signal generator. The frequency ranges from 15 ~ 90 Hz. With a scope, the 5V peak seems to be pulled down to 1.2V.

When I'm debugging, all the registers seem to be set correctly.

Like I mentioned before, I've tied the signal to PA2 which also has the TIM2_CH3 functionality and that seems to be working just fine.

Happy to provide additional details, apologies for not being clear earlier on.