cancel
Showing results for 
Search instead for 
Did you mean: 

Timer frequency is way off

NRumm.1
Associate III

My STM32F303RE is running with an 8MHz crystal feeding into HSE:

NRumm1_0-1705434819174.png

Using TIM4 I want to generate 700 interrupts per second, but I only get 318.

As can be seen TIM4 runs at 64MHz and the following configuration should trigger about 318 interrupts:

NRumm1_1-1705434933781.png

NRumm1_2-1705434959841.png

Using the following formula:

(edit note - had a typo in the formula)

interrupts_per_second := clock_frequency / (prescaler + 1) / (counter_period + 1)

64 MHz / 3 / 30476 = 700

The 318 interrupts are way off the expected 700 and I just can't figure why. Any ideas are highly appreciated.

Thx in advance !

Woyzeck

 

13 REPLIES 13

My SYSCLK is fed from PLLCLK and both are at the correct frequency, measured using MCO and an oscilloscope an a counter.

Still, the interrupt frequency stays at about 0.45 of what it should be, independent of the counter settings. And the problem is not caused by any kind of race condition or CPU bottleneck, as the 0.45 can be observed even at low frequencies.

Now it's getting really weird.

The ISR only gets triggered if I use the PLLCLK as the clock source for SYSCLK. Once I switch to HSE or HSI nothing happens.

Screenshot 2024-01-19 at 23.45.25.png

Let's stay objective here. Interrupts still work with HSI or HSE input. Perhaps they don't work in your program, but this isn't a hardware limitation and must be explained by something in your code.

Perhaps post enough of your code to show the problem.

> One thing that I observed is that the interrupt pulses are somehow irregular

Would suggest the clock is not stable, or there are competing resources for the cpu, or your measurements are wrong. Perhaps the interrupt blocks too long.

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

*** me - it was a bug. Deep in my code I disabled the interrupt and restarted the timer.

Thx for your support, guys - I learned a lot !