Skip to main content
ferraridamiano
Associate II
November 19, 2020
Solved

Extremely slow timer

  • November 19, 2020
  • 10 replies
  • 6591 views

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.
Best answer by KnarfB

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
November 19, 2020

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

ferraridamiano
Associate II
November 19, 2020

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
November 19, 2020

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
November 19, 2020

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
November 19, 2020

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
November 19, 2020

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

ferraridamiano
Associate II
November 19, 2020

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

gregstm
Senior II
November 19, 2020

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