Skip to main content
Dick Lin
Senior
November 8, 2018
Question

STM32F407 timer question?

  • November 8, 2018
  • 8 replies
  • 1649 views

Hi,

I am trying to use either TIM13 or TIM14 but the ISR never get trigger.

My questions are

  • Why the ISR routine never trigger? Is there anything else need be set?
  • how to control the timer? what's the prescaler, period mean?
  • If I want setup the timer trigger say every 10 ms how to?

Thx

****

startup_stm32f407xx.s

 .word   TIM8_UP_TIM13_IRQHandler     /* TIM8 Update and TIM13    */     

 .word   TIM8_TRG_COM_TIM14_IRQHandler   /* TIM8 Trigger and Commutation and TIM14 */

/* TIM13 init function */

void MX_TIM13_Init(void)

{

 htim13.Instance = TIM13;

 htim13.Init.Prescaler = 0;

 htim13.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim13.Init.Period = 0;

 htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 if (HAL_TIM_Base_Init(&htim13) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

}

/* TIM14 init function */

void MX_TIM14_Init(void)

{

 htim14.Instance = TIM14;

 htim14.Init.Prescaler = 0;

 htim14.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim14.Init.Period = 0;

 htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 if (HAL_TIM_Base_Init(&htim14) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

}

    This topic has been closed for replies.

    8 replies

    Tesla DeLorean
    Guru
    November 8, 2018

    Setting Prescaler and Period is going to result in the TIM not clocking...

    The Prescaler and Period are divisors. You write a value as N-1, so zero means divide by ONE

    TIMEBASE = (APBxTIMCLK / (Prescaler + 1)) / (Period + 1)

    For 10 ms, you want 100 Hz

    If the TIM is clocking at 84 MHz, that means 84,000,000 / 100 or 840,000 ticks to describe 10ms (0.01s)

    Suggest a Prescaler of 840-1 (839) and Period of 1000-1 (999)

     /*##-2- Start the TIM Base generation in interrupt mode ####################*/

     if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)

     {

       /* Starting Error */

       Error_Handler();

     }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Dick Lin
    Dick LinAuthor
    Senior
    November 8, 2018

    Doesn't seems working, seems going nowhere in HAL_TIM_Base_Start_IT(). After I pause the execution, see below message.

    Program received signal SIGINT, Interrupt.

    0x08000fbe in main () at ..\Src\main.c:101

    101  if (HAL_TIM_Base_Start_IT(&htim10) != HAL_OK)

    My main() is pretty simple, just testing the TIM.

    Any thoughts?

    Thx

    int main(void)

    {

     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

     HAL_Init();

     /* Configure the system clock */

     SystemClock_Config();

     MX_GPIO_Init();

     HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0 | GPIO_PIN_8 | GPIO_PIN_9);

     /* Initialize all configured peripherals */

     MX_TIM10_Init();

     /*##-2- Start the TIM Base generation in interrupt mode ####################*/

     if (HAL_TIM_Base_Start_IT(&htim10) != HAL_OK)

     {

      /* Starting Error */

      Error_Handler();

     }

     while (1)

     {

     }

    }

    Tesla DeLorean
    Guru
    November 8, 2018

    >>Any thoughts?

    You've got interrupt handler code?

    You've enabled the TIM clocks somewhere?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Dick Lin
    Dick LinAuthor
    Senior
    November 8, 2018

    I do have init code and ISR code. Anything else? Thx

    /* TIM2 init function */

    void MX_TIM2_Init(void)

    {

     TIM_ClockConfigTypeDef sClockSourceConfig;

     TIM_MasterConfigTypeDef sMasterConfig;

     htim2.Instance = TIM2;

     htim2.Init.Prescaler = 840 - 1;

     htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

     htim2.Init.Period = 1000 - 1;

     htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

     if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

     {

      _Error_Handler(__FILE__, __LINE__);

     }

     sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

     if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)

     {

      _Error_Handler(__FILE__, __LINE__);

     }

     sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

     sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

     if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

     {

      _Error_Handler(__FILE__, __LINE__);

     }

    }

    /* TIM10 init function */

    void MX_TIM10_Init(void)

    {

     htim10.Instance = TIM10;

     htim10.Init.Prescaler = 840-1;

     htim10.Init.CounterMode = TIM_COUNTERMODE_UP;

     htim10.Init.Period = 1000-1;

     htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

     if (HAL_TIM_Base_Init(&htim10) != HAL_OK)

     {

      _Error_Handler(__FILE__, __LINE__);

     }

    }

    /* TIM11 init function */

    void MX_TIM11_Init(void)

    {

     htim11.Instance = TIM11;

     htim11.Init.Prescaler = 840-1;

     htim11.Init.CounterMode = TIM_COUNTERMODE_UP;

     htim11.Init.Period = 1000-1;

     htim11.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

     if (HAL_TIM_Base_Init(&htim11) != HAL_OK)

     {

      _Error_Handler(__FILE__, __LINE__);

     }

    }

    void TIM1_UP_TIM10_IRQHandler()

    {

    uint32_t test = 0 ;

    }

    void TIM1_TRG_COM_TIM11_IRQHandler()

    {

    uint32_t test = 0 ;

    }

    void TIM2_IRQHandler()

    {

    uint32_t test = 0 ;

    }

    Tesla DeLorean
    Guru
    November 8, 2018

    The IRQ Handlers would need to clear the sources otherwise they'll keep re-entering.

    You configure the NVIC anywhere?

     /*##-2- Configure the NVIC for TIMx ########################################*/

     /* Set Interrupt Group Priority */

     HAL_NVIC_SetPriority(TIM2_IRQn, 4, 0);

     /* Enable the TIMx global Interrupt */

     HAL_NVIC_EnableIRQ(TIM2_IRQn);

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Dick Lin
    Dick LinAuthor
    Senior
    November 8, 2018

    Seems working now adding HAL_NVIC_EnableIRQ.

    Does the HAL_TIM_IRQHandler clear the interrupt source?

    void TIM1_UP_TIM10_IRQHandler()

    {

    HAL_TIM_IRQHandler(&htim10);

    }

    Tesla DeLorean
    Guru
    November 8, 2018

    Yes, the mechanics there is that the HAL manages things, and then uses a call-back you supply depending on the source of the interrupt.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Dick Lin
    Dick LinAuthor
    Senior
    November 9, 2018

    Thank you so much. I have it working now. Thx