I made a code to call Timer2 interrupt function like this :

timerx->TIMx_PSC    =42000;   // prescaler
timerx->TIMx_ARR    =2000;    // counting number
timerx->TIMx_CR1    |=0x90;   // auto reload, count down
timerx->TIMx_DIER   |=0x01;   // enable update interrupt
interruptsx->ISER[0]|= 1<<(TIMER2_INTERRUPT);   // enable timer_interrupt 
timerx->TIMx_CR1    |=0x01;   // enable counter  
timerx->TIMx_EGR    =0x01;    // update generation


void TIM2_IRQHandler(){
if(timer2->TIMx_SR & 0x1){

The problem is : it doesn't jump to TIM2_IRQHandler(). How can I solve this problem?

  • \$\begingroup\$ Assuming you're using the CMSIS startup_stm32f4xx.c file, and assuming that the interrupt is actually firing (if it is, the execution point should be jumping to the default handler in the startup file), try including the (void) argument reference in the function declaration, i.e. void TIM2_IRQHandler (void) instead of void TIM2_IRQHandler () \$\endgroup\$
    – markt
    Commented Jun 24, 2014 at 3:27
  • \$\begingroup\$ I used startup_stm32f4xx.s and also try including (void) argument but it doesn't work. Do you have other suggestion?? \$\endgroup\$
    – ios198
    Commented Jun 24, 2014 at 4:04
  • 1
    \$\begingroup\$ The suggestion by markt is completely irrelevant. The compiler doesn't differentiate between having void or not in the argument list. A few ideas: have you enable the clock for the timer using the RCC peripheral? Is the timer actually counting? Try reading the TIM2_CNT register a few times in sequence; if they're all identical, it is most certainly stopped. Do any of the flags in TIM2_SR get set after a while? \$\endgroup\$
    – swineone
    Commented Jun 24, 2014 at 4:47
  • 2
    \$\begingroup\$ Are you configuring the interrupt in the NVIC? \$\endgroup\$
    – Tut
    Commented Jun 24, 2014 at 11:55
  • 1
    \$\begingroup\$ Have you turned the clock on for the timer? \$\endgroup\$
    – Colin
    Commented Jun 11, 2017 at 9:48

2 Answers 2


The following works (i.e. triggers interrupts) on my STM32F4Discovery board under the Coocox environment using GCC:

#include    "stm32f4xx.h"

int main (void) {
NVIC->ISER[0] |= 0x10000000;
RCC->APB1ENR |= 1;
TIM2->CR1 = 0x0010;
TIM2->ARR = 0x8000;
TIM2->CR2 = 0;
TIM2->SMCR = 0;
TIM2->DIER = 0x0001;
TIM2->CR1 |= 0x0001;
while (1);

void TIM2_IRQHandler (void) {
TIM2->SR &= 0xfffe;

I'm pretty sure that the reason is that I don't actually start the timer (set bit 0 of TIM2->CR1) until after everything else is configured.

  • \$\begingroup\$ Funny, because the only thing he does after setting bit 0 of TIM2->CR1 is setting bit 0 of TIM2->EGR, which as I understand manually generates an update event, so it doesn't seem like that would be a problem. Maybe if you could try running the exact same initialization procedure that he asked in the question (just don't remove the call to RCC that he didn't include), in the same order that he followed. I don't have any hardware with me now, otherwise I'd try. If it works for you this way it's clear the error is somewhere else, probably in his non-standard initialization code. \$\endgroup\$
    – swineone
    Commented Jun 26, 2014 at 8:23
  • \$\begingroup\$ I checked my initialization code but can not find any problems. This is all my program github.com/ios198/ClockDigital \$\endgroup\$
    – ios198
    Commented Jun 27, 2014 at 8:18
  • \$\begingroup\$ @ios198 Try replacing your code fragment with mine (without the SystemInit() call) and see if that fixes the problem (yes, the prescaler etc will be wrong, but try it anyway). An outlier worth checking is maybe your use of 8-bit values is causing problems; try changing all the 8-bit values to 16-bit ones, and/or cast them explicitly as shorts, so e.g. TIM2->CR2 |= 0x90; becomes TIM2->CR2 |= (short)0x0090; Yes I know it shouldn't matter but shouldn't isn't the same as doesn't. \$\endgroup\$
    – markt
    Commented Jun 27, 2014 at 14:13
  • \$\begingroup\$ Finally I can call timer TIM2_IRQHandler().The problem is from startup_stm32f4xx.s. I use this equation to calculate when an update event occur: Update Event(HZ)=Timer Clock/((TIM_PSC+1)*(TIM_ARR+1). In register RCC_CFGR(clock configuration register), I set Bit31:30=00 ( system clock selected =168MHZ), bit15:13=101( AHB clock divided by 4), bit7:4=0xxx (not divided). There for timer clock is 84MHZ. For example if TIM_PSC=42000 and TIM_ARR=2, update event occur each 1ms. But update event doesn't occur as I want. Which problems I got here? Do I need to choose clock source first? \$\endgroup\$
    – ios198
    Commented Aug 14, 2014 at 7:22

I had the same problem and the solution was to manually call HAL_TIM_Base_Start_IT() after MX_TIM2_Init(). I'm using CubeMX and I thought it does that automatically, but it doesn't.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.