Search code examples
timerstm32ledpwm

How to control the rate of LED blinking


I am using STM32 board to control the color and blinking rate of the RGB LED. For varying the color of RGB LED I have configured a timer in PWM mode and by varying the duty cycle of three signals on three channels of this timer, the LED changes the color. How can I control the blinking rate of LED with another timer? Which mode of second timer and technique should be used to control the on and off time? Thanks in advance.


Solution

  • There are many ways that you can do this.

    The simplest is in the main loop of your application to read some clock or free running timer. If it is time for the next LED colour then update the PWM duty cycle.

    A more complicated approach that does not require code in the main loop would use a DMA whose source is an array of PWM values and whose destination is the duty cycle register of the output timer. You can then use a different timer to periodically trigger the DMA.