cancel
Showing results for 
Search instead for 
Did you mean: 

PWM with multiple interrupts at different frequencies

Ricko
Senior II

Hi,

I am using a STM32L452RET6 (without any RTOS) and I would like to use the same timer for both:

- drive a sounder on PWM at 4KHz (but ideally also other changing frequencies above and below 1ms time period so it can make more elaborate sounds which)

- generate and also 1ms interrupt to check other tasks.

 

What would be the best way to configure and use the micro to implement that?

 

Thank you as always

1 ACCEPTED SOLUTION

Accepted Solutions

The systick is used for system jobs, like hal-delay, and you should not touch or modify it.

But you can use the 1ms interrupt and do something there.

The timer for this system clock is part of the ARM core, so read in arm documentation about the core and it's modules, like debug unit or system timer.

You cannot use it for any pwm generation or so, it's just a timer for the system (in windows or Linux system you also have this,1ms system clock...), so use any timer of the many you have available, to do the modulated frequency generator 

 

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

View solution in original post

7 REPLIES 7
TDK
Guru

Timers have a single counter register. You can't create PWMs or callbacks at independent frequencies on the same timer.

The chips typically have plenty of timers, should be able to use two different ones here, or use the SysTick interrupt to check other tasks.

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

As writed TDK why not second timer? But for your question yes you can create 1ms on one channel interrupt and some other freq on other , but some condition here...

AScha.3
Chief II

So why not just use the systick (1ms is standard) for the things to do every ms and use a / any timer to make the 2....7 kHz sounds?

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

Thank you all, rather obvious but being new to STM32 I did not think of that.

I can see the SysTick_Handler(void) has been generated by CubeMx. Could you please help/explain the following:

 

- I assume SysTick is the same as (i.e. the period of) the SysClk which in the diagram below (highlighted in red) is currently set to 72MHz, is that correct?

 

- That SysClk goes (only) to "To Power" and could not find any information as to what that is, so I don't know if changing that value changes other things in my code or generally in the micro configuration. What is that "To Power"?

 

- how do I set its time period (for example to 1ms) at runtime?

 

Thank you

 

Ricko_0-1723848390255.png

 

The systick is used for system jobs, like hal-delay, and you should not touch or modify it.

But you can use the 1ms interrupt and do something there.

The timer for this system clock is part of the ARM core, so read in arm documentation about the core and it's modules, like debug unit or system timer.

You cannot use it for any pwm generation or so, it's just a timer for the system (in windows or Linux system you also have this,1ms system clock...), so use any timer of the many you have available, to do the modulated frequency generator 

 

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

Your red mark is SYSCLK not systick. In normal HAL code set systick interrupt to every 1ms, not recommend to change is used for timeouts etc. But in handler you can place quick code you require run every 1ms.

For example toggle gpio and create some low speed PWM or blinky ...

For explain SYSTICK is somethink like hw TIM0 capable only create interrupt based on set tick count...

Ricko
Senior II

Thank you both.