Top.Mail.Ru

STM32 timer and interrupts configuration with STM32CubeMx utility.

Hi to everyone! Today we'll continue studying STM32CubeMx! The next stage is configuring STM32 timer parameters and using interrupts. Here are the previous posts:

Previous post has been devoted to GPIO configuration, we learned how to turn on/off LEDs with the help of STM32CubeMx. So, today we will also toggle the LED, but we'll do it through interrupt handler. Let's configure one STM32 timer to generate an interrupt every 500 ms.

Primarily we should create a new project, choose the MCU which we would like to use and set up the project as we usually do. After that we can proceed to the timer configuration. I'll use TIM3 unit:

STM32Cube timer

To enable the timer clock we should select the clock source for it (for example, internal clock). Furthermore, I've activated PD12, because one of the LEDs on my development board is connected to this pin. So, I have to set its mode to "General output".

We've already enabled the timer unit, but it also requires some extra configuration. In order to do this we should open Configuration tab and double-click on TIM3 button:

Timer configuration
STM32Cube timer configuration

TIM3 is connected to APB1 bus which default frequency is 16 MHz (in one of the future posts I'll describe how to set different frequencies of all busses). Let's set the prescaler value to 16000 (in counter settings we should enter (PSC - 1) value). Thus, to calculate timer frequency we should divide APB1 frequency by 16000 prescaler:

f_T = \frac{16\medspace MHz}{16000} = 1000\medspace Hz

Furthermore, we should set the proper value of counter period. If we set it to 500, we'll get the following value of timer period:

T = \frac{1}{f_T} \cdot 500 = 0.5\medspace s

And the final step of TIM3 configuration is enabling its interrupt. This can be done at the "Nvic Settings" tab. Finally, let's start code generation! As the result we get the new project with timer initialization function and interrupt handler. All interrupts are located in a file called stm32f4xx_it.c). What should also be done is starting the timer counting (HAL_TIM_Base_Start_IT() function) and adding some extra code into the interrupt handler in order to change the output state of PD12 (LED toggling):

int main(void)
{
	/* USER CODE BEGIN 1 */

	/* USER CODE END 1 */

	/* MCU Configuration----------------------------------------------------------*/

	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();

	/* Configure the system clock */
	SystemClock_Config();

	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_TIM3_Init();

	/* USER CODE BEGIN 2 */

	HAL_TIM_Base_Start_IT(&htim3);

	/* USER CODE END 2 */

	/* USER CODE BEGIN 3 */
	/* Infinite loop */
	while (1)
	{
	}
	/* USER CODE END 3 */
}
void TIM3_IRQHandler(void)
{
	/* USER CODE BEGIN TIM3_IRQn 0 */

	/* USER CODE END TIM3_IRQn 0 */
	HAL_TIM_IRQHandler(&htim3);
	/* USER CODE BEGIN TIM3_IRQn 1 */

	HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);

	/* USER CODE END TIM3_IRQn 1 */
}

Now we can program the MCU and see the green led toggling every 500 ms! 🙂

That concludes this post, thank you for your attention!

Подписаться
Уведомить о
guest

9 комментариев
Старые
Новые
Межтекстовые Отзывы
Посмотреть все комментарии
D
D
8 лет назад

Is that true?

Владислав
Владислав
Ответ на комментарий  D
7 лет назад

At least, it does work. 😉

FATFS
FATFS
7 лет назад

No. I think this tutolrial is not extractly.

Владислав
Владислав
Ответ на комментарий  FATFS
7 лет назад

"Extractly"?

Владислав
Владислав
7 лет назад

BTW, your russian and english posts differs somehow.
For example, in this one - there have been used graphical formulas. Why don't you like to keep the issue in both hands?

Hussien Al-haj Ahmad
Hussien Al-haj Ahmad
6 лет назад

I would to thank you very much...

Mete
Mete
5 лет назад

Thanks for your work and share, but what is inside MX_TIM3_INIT(); function? could you show whole code?
i have stm32f767 nucleo, i written my codes manualy and if i decrease the prescaler and periode to generate high speed for LED it doesnt change after ten LED blinking per second!

najme
najme
4 лет назад

thanks alot that was so clear

9
0
Оставьте комментарий! Напишите, что думаете по поводу статьи.x