cancel
Showing results for 
Search instead for 
Did you mean: 

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

noobmaster69
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
gbm
Lead III

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

View solution in original post

10 REPLIES 10
gbm
Lead III

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

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

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

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...

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.

TDK
Guru

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".

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...

Yes I'm using CubeMX and HAL(I'm using STM32CubeIDE v1.9.0). But I'm not getting any interrupts. HAL_InitTick() returns HAL_OK. I set breakpoint in HAL_SYSTICK_IRQHandler() but I never hit it. Can you tell me if I'm doing something wrong? Is there anything I need to set up? (I'm using the default code generated by CubeMX).

Probably systick runs and generate 1ms interrupts and HAL_SYSTICK_IRQHandler() is not placed into systic IRQ routine. Look into SysTick_Handler().