...
Let’s go back to the fan example. Turning a fan on and off very slowly evidently shows us the average fan speed is going to fluctuate around, so if we graphed this it would be very “jittery”. This tells us our PWM frequency being too low will lead to “stutters” and unpleasant results. However, too high frequencies can cause excessive transistor switching losses(Gate capacitance and non ideal properties of a switching component).
Firmware Implementation:
In our firmware we use output compare registers to toggle the state of the timer pin. In a bare-metal application, the timer continuously compares the counter value with the output compare register. When a match occurs, a hardware interrupt occurs and it automatically toggles the GPIO pin. The hardware manages this operation without the need for coded ISRs. To learn more about timers here are resources:
https://www.linkedin.com/pulse/introduction-stm32-timer-basics-library-kuke-electronics/