2022-01-16 10:23 PM
hey there
I have a project where I need to control light of a LED and dim it. usually we use TIMER PWM GENERATION. for example in CubeMX I set a pin to TIMx CHy and by choosing "PWM Generating CHy" I can easily control LED light using PWM signal.
but in my case this LED pin has no TIM Channel connected to it. what should I do to dim the LED?
2022-01-16 10:36 PM
You can use GPIO with delay loops.
2022-01-16 10:38 PM
sure! but this occupies CPU and also is not efficient.
is there a way to generate an internal PWM signal and control LED with it?
2022-01-17 02:17 AM
Set a timer as timebase at 1000Hz, and in the ISR toggle the LED GPIO.
This will generate your "PWM"
You should be able to manage about 16 light levels
2022-01-17 02:30 AM
So you need to implement a softpwm.
There is three ways:(that i can think of now)
If your system can spare those interrution times i would choose the second option.
I had to implement a bunch of pins at the same time and my system couldnt spare lots of processing time so i had to do the third option
https://www.hackster.io/javier-munoz-saez/all-pins-as-pwm-at-the-same-time-baremetal-stm32-1df86f
2022-01-17 02:33 AM
You could also use the DAC if that function is available in your pin (overkill)
2022-01-17 02:44 AM
this one sounds great! but it affects the entire process, as ISR happens fast, though I might end up doing such...
2022-01-17 02:47 AM
You can set the timer to 500Hz, and the ISR is quite short.
2022-01-17 02:52 AM
I actually ended up doing something similar to that... and yeap! it worked! thanks many times @Javier Muñoz
2022-01-17 03:23 AM
yes but also I can detect LED being ON/OFF