cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Cube MX to configure timer 3 for 40ms interrupts in STM32L476

JBonn
Associate III

Are there example MX projects? The IDE has xcellent example projects?

1 ACCEPTED SOLUTION

Accepted Solutions

Hello @JBonn​ ,

Thanks for having reported.

In fact, STM32CubeMX tool comes with the Example selector feature allowing you to browse a large set of examples and to start a new project from a selected example for some STM32 MCUs/MPUs. Thanks to the filter panel it is possible to filter down the example list (you can use TIM/ timer as a keyword):

0693W00000DprCTQAZ.png Otherwise, you'll find a table resuming the available examples in the STM32Cube MCU Package examples for STM32L4 Serie Application Note:

0693W00000DpqwuQAB.png 

Trying to directly answer your question; configure timer 3 for 40ms interrupts in STM32L476, you'll find hereafter some simple steps to de the configuration using CubeMX:

  • Launch your project using STM32L476 MCU ( for instance NUCLEO-L476RG and initialize all peripherals to their default mode in order to set pins correctly).

0693W00000DprRnQAJ.png 

  • Select Internal Clock as TIM3 clock source.
  • The output time interval is determined by the Prescaler value, the clock frequency, and the timer Counter Period (ARR) register’s value. In order to get an output time interval equal to 40mSec ( for instance, toggle the LED once each 40mSec) and ) and set up the timer interrupt to give this time interval, we can assume the FCLK to be 16MHz, let the Prescaler be 1000 and the Counter Period to 640. You can set these parameters through CubeMX interface > TIM3 Configuration Panel > Parameter Settings window tab.

0693W00000DprS7QAJ.png 

  • Enable auto-reload preload as well as The Timer Interrupt Signal In NVIC Tab.
  • Generate The Project Initialization Code.
  • Once the code is generated you should add the following lines of code:
/* USER CODE BEGIN 2 */
 HAL_TIM_Base_Start_IT(&htim3);
  /* USER CODE END 2 */
 
//....
 
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
/* USER CODE END 4 */

(I've used LD2 as the PA5 is the output for green Led)

Normally following those steps everything will work correctly.

Furthermore, you can refer to the STM32 Timers representation covering the main features of Timers for more details.

Hope this helps you.

Khouloud.

View solution in original post

2 REPLIES 2

Hello @JBonn​ ,

Thanks for having reported.

In fact, STM32CubeMX tool comes with the Example selector feature allowing you to browse a large set of examples and to start a new project from a selected example for some STM32 MCUs/MPUs. Thanks to the filter panel it is possible to filter down the example list (you can use TIM/ timer as a keyword):

0693W00000DprCTQAZ.png Otherwise, you'll find a table resuming the available examples in the STM32Cube MCU Package examples for STM32L4 Serie Application Note:

0693W00000DpqwuQAB.png 

Trying to directly answer your question; configure timer 3 for 40ms interrupts in STM32L476, you'll find hereafter some simple steps to de the configuration using CubeMX:

  • Launch your project using STM32L476 MCU ( for instance NUCLEO-L476RG and initialize all peripherals to their default mode in order to set pins correctly).

0693W00000DprRnQAJ.png 

  • Select Internal Clock as TIM3 clock source.
  • The output time interval is determined by the Prescaler value, the clock frequency, and the timer Counter Period (ARR) register’s value. In order to get an output time interval equal to 40mSec ( for instance, toggle the LED once each 40mSec) and ) and set up the timer interrupt to give this time interval, we can assume the FCLK to be 16MHz, let the Prescaler be 1000 and the Counter Period to 640. You can set these parameters through CubeMX interface > TIM3 Configuration Panel > Parameter Settings window tab.

0693W00000DprS7QAJ.png 

  • Enable auto-reload preload as well as The Timer Interrupt Signal In NVIC Tab.
  • Generate The Project Initialization Code.
  • Once the code is generated you should add the following lines of code:
/* USER CODE BEGIN 2 */
 HAL_TIM_Base_Start_IT(&htim3);
  /* USER CODE END 2 */
 
//....
 
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
/* USER CODE END 4 */

(I've used LD2 as the PA5 is the output for green Led)

Normally following those steps everything will work correctly.

Furthermore, you can refer to the STM32 Timers representation covering the main features of Timers for more details.

Hope this helps you.

Khouloud.

Thanks for this answer. This answers my question.
Please close the question.