embeddedstm32

STM32 how to use the same timer to generate PWM (at varying frequencies) and a fixed 1ms interrupt


I’m using an STM32L452RET (no RTOS) and would like to use the same timer (timer 2) for two purposes:

  1. Generate a 1 ms interrupt to handle other tasks.

What would be the best way to do that?

I tried setting the PWM to 1ms for the interrupt to check other tasks and drive the buzzer pin directly (instead of the PWM peripheral) but then it would not create frequencies higher than 500Hz for the buzzer.

Thank you. 😊


Solution

  • The ARM Cortex-M4 CPU inside your STM32 MCU has a Systick timer which is specifically designed for generating periodic interrupts at a fixed frequency, or for other timing-related purposes. This would be perfect for your 1ms tick.

    Then you could use the other timer for your PWM signal, and vary its frequency, duty cycle, or whatever, as you see fit, without disturbing the 1ms tick.