Skip to main content
noobmaster69
Associate III
April 14, 2022
Solved

How can I use timer (TIM22) in STM32L031K6 to get interrupt on OC on channel 1 and generate PWM signal on channel 2?

In my application I want to use timer to turn on LED for around 300ms and simultaneously generate PWM signal of freq 2.7kHz (for buzzer) on PA10 pin. Is there any way I can achieve this?

(NOTE: I have already used TIM2 and TIM21 timers. TIM21 is also running at the same time as I trun on LED and buzzer)

No matter what configurations I set on channel 1, I always get around 400us wave as shown in screenshot below.

This topic has been closed for replies.
gbm
gbmBest answer
Super User
April 14, 2022

You don't need hardware timer for LED blinking - control the LED in software using any timer interrupt routine. SysTick, with default 1 ms period should be a good candidate for this.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

10 replies

gbm
gbmBest answer
Super User
April 14, 2022

You don't need hardware timer for LED blinking - control the LED in software using any timer interrupt routine. SysTick, with default 1 ms period should be a good candidate for this.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
noobmaster69
Associate III
April 14, 2022

I dont want it running continuously. I'll turn on LED and Buzzer on only when I receive valid data from UART. WIll it be possible to do so using SysTick? If so then can you recommend me some resources for reference?

Thanks & Regards,

SBhon.1

noobmaster69
Associate III
April 14, 2022

I dont want it running continuously. I'll turn on LED and Buzzer on only when I receive valid data from UART. WIll it be possible to do so using SysTick? If so then can you recommend me some resources for reference?

Thanks & Regards,

SBhon.1

Michal Dudka
Lead
April 14, 2022

Like gbm writes. Use timer to generate 2.7kHz for buzzer. And use Systick (like "millis") to turn on and off LED or buzzer (timer).

Of course it is possible to generate periodic 2.7kHz signal by one timer and 300ms pulses by another timer. But you waste timer for simple LED control. Of courste there are ways how to manage both tasks by one timer (enable interrupt from overflow, count interrupts until about 300ms) but they are more complicated then first mentioned way...

noobmaster69
Associate III
April 14, 2022

Thanks for the reply. But I dont want 300ms pulses. My application is access control system. So, only when someone does access, I have to turn on LED and Buzzer for around 300ms indicating access granted. And after 300ms, I have to trun it off and go in low power mode until next access happens. And SysTick gives periodic interrupts. So I wanted to know if I can start SysTick timer when access is done, and after 300ms stop it.

Michal Dudka
Lead
April 14, 2022

You can start and stop systick if you need (and systick is disabled in most sleep modes). If you are using CubeMX and HAL, you have probably systick enabled and running with 1ms periodic interrupt...

TDK
April 14, 2022

You can use another timer in one-shot mode to output a 300ms pulse.

Using SysTick should be straightforward as well. Set the pin high and start a counter. Increment counter in SysTick. When counter reaches 300, set pin low.

"If you feel a post has answered your question, please click ""Accept as Solution""."