STM32 timer 2 interrupt handler does not work

Hello,

I try to initialize an interrupt vector for timer 2 that TIM2_IRQHandler is executed. I use a STM32F401CE controller. With the code below the handler TIM2_IRQHandler is never triggered.

Does anybody have an idea which functions I have to execute further to actived a trigger for timer 2?

#include <Arduino.h>

#include "IWatchdog.h"

  /* Set TIMx instance */

  TIM_HandleTypeDef Tim2Handle;

 

/**

 * Blaue LED initialisieren

*/

void InitBlueLed()

{

   __HAL_RCC_GPIOC_CLK_ENABLE();

  GPIO_InitTypeDef BoardLEDs;

  BoardLEDs.Mode = GPIO_MODE_OUTPUT_PP;

  BoardLEDs.Pin = GPIO_PIN_13;

  HAL_GPIO_Init(GPIOC, &BoardLEDs);

 

}

static void InitTimer2(void)

{

  Tim2Handle.Instance = TIM2;

Tim2Handle.Init.Period = 32;

Tim2Handle.Init.Prescaler = 2500;

Tim2Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;

Tim2Handle.Init.RepetitionCounter = 0;

Tim2Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

// Timer & Interrupt initialisieren

HAL_TIM_Base_Init(&Tim2Handle);

HAL_TIM_Base_MspInit(&Tim2Handle);

HAL_TIM_Base_Start(&Tim2Handle);

HAL_TIM_Base_Start_IT(&Tim2Handle);

__HAL_RCC_SYSCFG_CLK_ENABLE();

  __HAL_RCC_PWR_CLK_ENABLE();

HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(TIM2_IRQn);

}

/**

 * Interrupt an Timer 2

*/

void TIM2_IRQHandler(void)

{

    // Interrupt triggered

    if (TIM2->SR & TIM_SR_UIF) 

    {

        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);

    }

    

     HAL_TIM_IRQHandler(&Tim2Handle);

}

int main()

{

  HAL_Init();

  SystemClock_Config();  

  InitBlueLed();

  InitTimer2();

int state = 0;

  while (1)

  {

    // LED toggeln

    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);

    

    HAL_Delay(1000);

  }

  return 0;

}

1 Like

This isn't an arduino question but I think the order of operations in your timer setup is significant

__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
HAL_TIM_Base_Init(&Tim2Handle);

HAL_TIM_Base_MspInit(&Tim2Handle);

HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(TIM2_IRQn);

HAL_TIM_Base_Start(&Tim2Handle);

HAL_TIM_Base_Start_IT(&Tim2Handle);

That's how I would order it, but don't take that as gospel.
You also need to set the clock source. Maybe you should find a good timer example online and follow that. ST Micro has lots of timer application notes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.