As an application of the timer, let's try dimming LED lights using the PMW output. Normally, LEDs can be turned on and off simply by switching MCU output ON/OFF, which is typical of digital outputs.
How can we dim LEDs using the digital output of MCU? Using the digital output of MCU to continuously control the brightness of a dimmer in an analog manner is like a magic trick, but it can be done easily with MCU. Now, let's reveal the secret.
Normally, LEDs light up when MCU output is turned on, connecting the circuit. Pulse output is used to light LEDs in order to adjust the brightness of LED products to save power, or to adjust the light intensity of LEDs built into sensors, for example.
Even when the output to the LED is pulsed, the LED appears to light up almost continuously at about 100 Hz. The advantage of this pulse output is that the brightness intensity can be adjusted by changing the ON/OFF period ratio (duty ratio) of the pulse, and power can be saved by adjusting the OFF time.
The PWM output of the timer function has been explained so far, and the dimming of LEDs is an application of this PWM. In this application example, the brightness of the LED is adjusted by adjusting the ON/OFF period of the 100 Hz output pulse.
In this application example, the RTOS is used to perform the tasks of turning the LED on and off every 1500ms and setting the PWM duty ratio every 10ms.
PWM of the STM32 timer is explained in PWM mode output of Timer/Counter[STM32 Advanced Functional and General-Purpose Timers Details].
Blinking every 1.5s
Lights up only when user SW is pressed.