2020-11-19 02:28 AM
Hi, 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!
Solved! Go to Solution.
2020-11-19 08:30 AM
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."
2020-11-19 03:25 AM
I guess you are still running on the 4 Mhz MSI , as 4/30 ~= 1/7. Check your clock setting.
2020-11-19 03:36 AM
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.
2020-11-19 04:18 AM
Initialise the MCO pin to check that Sysclock etc. is what you expect.
2020-11-19 07:25 AM
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?
2020-11-19 07:27 AM
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
2020-11-19 07:28 AM
Thanks for you reply! Unfortunately, I have not an oscilloscope with me right now. (thanks covid)
2020-11-19 08:30 AM
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."
2020-11-19 08:49 AM
Thanks! Didn't know it! So, the timer clock in configuration clock means nothing??
2020-11-19 10:02 AM
You probably don't want to run the board at bare 4 MHz. Therefore you make a clock configuration in the GUI which ends up in the .ioc file, and in the generated code for SystemClock_Config();. The SYSCLCK variable in the clock configuration GUI is the SystemCoreClock variable in the code. The value should be 80 or 120 MHz or so (don't have your board) after SystemClock_Config(); The APB timer clock is the input clock to a timer when Clock Source == Internal Clock is selected. So, I wouldn't agree that "configuration clock means nothing".
If you are in doubt, read SystemCoreClock when debugging after SystemClock_Config(). If everything else failes, you may make a screen dump of your clock configuration.