Skip to main content
DOSS FOF
Associate II
May 15, 2017
Question

Timer2 Configuration

  • May 15, 2017
  • 2 replies
  • 1945 views
Posted on May 15, 2017 at 10:47

I'm working on STM32 nucleo432lkc. I'm trying to divide the MSI frequency for the timer by 50 but when I try this , i get nothing on the ouput of the pin. Below is the timer 2 code :

void TIM2_init(void)

{

      TIM_ClockConfigTypeDef sClockSourceConfig;

      TIM_MasterConfigTypeDef sMasterConfig;

    __TIM2_CLK_ENABLE();

    TIM_init_struct.Instance = TIM2;

    TIM_init_struct.Init.Prescaler = 0 ;//40000;

    TIM_init_struct.Init.CounterMode = TIM_COUNTERMODE_UP;

    TIM_init_struct.Init.Period = 0 ;//491;

    TIM_init_struct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

    TIM_init_struct.Init.RepetitionCounter = 0;

    //HAL_TIM_Base_Init(&TIM_init_struct);

    if(HAL_TIM_Base_Init(&TIM_init_struct)== HAL_OK)

      {

        /* Start the TIM time Base generation in interrupt mode */

        HAL_TIM_Base_Start_IT(&TIM_init_struct);

      }

    HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);

    HAL_NVIC_EnableIRQ(TIM2_IRQn);

     sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

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

      {

          TIM_Error_Handler();

      }

      sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

      sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

      {

          TIM_Error_Handler();

      }

}

void TIM2_IRQHandler()

{

    //}

     HAL_TIM_IRQHandler(&TIM_init_struct);

}

#include 'stdbool.h'

bool test = false;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

    int timerValue =  HAL_RCC_GetSysClockFreq();

    if (test) {

        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);

        test = false;

    } else {

        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);

        test = true;

    }

//    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);

    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);

}

    This topic has been closed for replies.

    2 replies

    RomainR.
    ST Employee
    May 15, 2017
    Posted on May 15, 2017 at 17:08

    Hello,

    It would seem in your code that you do not configure the value of your timer period?

    TIM_init_struct.Init.Period = 0; // 491;�?

    What is your requirement in your application for the interruptions period?

    Could you verify ?

    Best regards

    To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    DOSS FOF
    DOSS FOFAuthor
    Associate II
    May 16, 2017
    Posted on May 16, 2017 at 09:17

    Hello Romain,

    In fact I forgot and pasted previous version. But the real values were this:

        TIM_init_struct.Init.Prescaler = 1 ;

        TIM_init_struct.Init.CounterMode = TIM_COUNTERMODE_UP;

        TIM_init_struct.Init.Period = 50 ;

        TIM_init_struct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

        TIM_init_struct.Init.RepetitionCounter = 0;

    The clock frequency is 38.4 MHz and tried to divided by  2x51 in that way. But when I run the programme 

    , I trully get the call back function 'void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)',

    but the GPIO didn't work. In contrast I realized that another function inside the callback works.

    That is the GPIO intialization :

             GPIO_InitTypeDef GPIO_init_struct;

            GPIO_init_struct.Pin = GPIO_PIN_3 |GPIO_PIN_7;

            GPIO_init_struct.Mode = GPIO_MODE_OUTPUT_PP;

            GPIO_init_struct.Pull = GPIO_NOPULL;

            GPIO_init_struct.Speed =  GPIO_SPEED_FREQ_HIGH 

            HAL_GPIO_Init(GPIOB, &GPIO_init_struct);

    Notice: the values greater than 100 (TIM_init_struct.Init.Period = 50) work.

    RomainR.
    ST Employee
    May 17, 2017
    Posted on May 17, 2017 at 16:53

    Hello,

    Assuming your Timer 2 input clock is 4MHz, this means that with the following settings:

    TIM_init_struct.Init.Prescaler = 1;
    TIM_init_struct.Init.Period = 50;�?�?

    The counting resolution of your timer is about 26ns.

    With a value of period = 50, you will get an overflow interrupt every 1.3μs (26ns x 50) For a value of period = 100, you will get an interrupt every 2.6μs.

    period = (Timer Clock frequency / Timer prescaler x period frequency) - 1

    How do you visualize the GPIO that is supposed to make the toggle in the Timer Callback function with such a fast period of 1.3μs? If you use an oscilloscope you should be able to see the change of state of this output?

    Again, I ask you the same question, what are your specificity in your application for Timer 2 interrupt point of view ?

    You should fix this point and then configure your timer according.

    Regards

    To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    DOSS FOF
    DOSS FOFAuthor
    Associate II
    May 19, 2017
    Posted on May 19, 2017 at 10:29

    Hello Romain,

    Another notice , I have managed to generate 48KHz for GPIO and 4.8 KHz for the DACs. Whatever the SPI configuration (Baudrate ) I always have this differences : Dacs signal 10 times lower than Timer interruption. I don't understand this problem. 

    Dac maximum speed is 50MHz in the datasheet. Concerning SPI_DMA I have tried it but no success, may be the configuaration is not well done. I will bring the code next time

    Thanks

    DOSS FOF
    DOSS FOFAuthor
    Associate II
    May 19, 2017
    Posted on May 19, 2017 at 11:04

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6sE&d=%2Fa%2F0X0000000bx3%2FV1JY9.rDQ0.kkr5U_kYYanpWThlyVIRwNvNpK_VG_vg&asPdf=false