Skip to main content
ferraridamiano
Associate II
Nov 19, 2020 10:28 AM UTC
Solved

Extremely slow timer

0693W000005BwGTQA0.jpg0693W000005BwHMQA0.jpgHi, I'm new in stm32 world. I recently bougth a stm32L4R5ZI nucleo board and started a project using timer/counter. I tried to do a very simple project that generate a PWM of 1 Hz with d%=50%. So I setup the timer clock to 30 MHz. And in the parameter settings I setup: PWM generation on CHx, prescaler to 29999, counter period of 999 and pulse at 499. What I get is an LED that blinks at a very low frequency, at about 1/7 Hz. instead of 1 Hz.

My program just do this (I just enabled the timer in the main).

I tried to change timer, and use timer as a counter but I always get similar results. I have no idea of what can it be.

Thanks in advance!

This topic has been closed for replies.
KnarfB
KnarfBBest answer
Super User
Nov 19, 2020 4:30 PM UTC

If you generated the code by STM32CubeIDE, the line SystemClock_Config(); in main() will setup the clocks. Beforehand, the MCU runs on a internal clock at 4MHz. See reference manual RM0394 6.2 Clocks "The MSI is used as system clock source after startup from Reset, configured at 4 MHz."

10 replies

Uwe Bonnes
Chief
Nov 19, 2020 11:25 AM UTC

I guess you are still running on the 4 Mhz MSI , as 4/30 ~= 1/7. Check your clock setting.

ferraridamiano
Associate II
Nov 19, 2020 3:25 PM UTC

Thanks, I've done some test and I noticed that if I consider the timer frequence = 4MHz and I compute the prescaler and ARR, I get 1 hz PWM. What is strange is that whatever timer frequency I set in clock configuration the real timer frequence is 4MHz. Why this?

KnarfB
KnarfBBest answer
Super User
Nov 19, 2020 4:30 PM UTC

If you generated the code by STM32CubeIDE, the line SystemClock_Config(); in main() will setup the clocks. Beforehand, the MCU runs on a internal clock at 4MHz. See reference manual RM0394 6.2 Clocks "The MSI is used as system clock source after startup from Reset, configured at 4 MHz."

KnarfB
Super User
Nov 19, 2020 11:36 AM UTC

I wouldn't change APB clocks accoring to a single timer requirement, but use the default SYSCLK values and adjust the prescaler & period.

Like for my STM32L32KC with 80 MHz SYCLK I use a prescaler of 40000-1, a counter period of 2000-1, and a period of 1000-1.

ferraridamiano
Associate II
Nov 19, 2020 3:27 PM UTC

Thanks for the tip! I discovered that changing APB clock doesn't change nothing for the timer. Its frequency is 4Mhz but I don't understand why

gregstm
Senior II
Nov 19, 2020 12:18 PM UTC

Initialise the MCO pin to check that Sysclock etc. is what you expect.

ferraridamiano
Associate II
Nov 19, 2020 3:28 PM UTC

Thanks for you reply! Unfortunately, I have not an oscilloscope with me right now. (thanks covid)

gregstm
Senior II
Nov 19, 2020 9:02 PM UTC

oh. The MCO has a prescaler so you can divide it by (up to) 16 - and maybe you have a multimeter with a frequency counter? Besides that, I'm not sure what I would do if I didn't have access to an oscilloscope.....