cancel
Showing results for 
Search instead for 
Did you mean: 

Newbie question about PWM output accuracy

SZony.1
Associate II

Hi,

I'm testing various PWM scenarios and found that output frequency does not match with expected.

I'm using formula Period = (PSC+1)*(ARR+1) / TmerClockFreq in seconds Period = 1000 * (PSC+1)*(ARR+1) / TmerClockFreq in milliseconds. The board is Nucleo-144, MCU STM32H745Z.

My calculus :

PSC=0

ARR=1

TimerClockFreq (MHz)=80

TimerClockFreq (s) =0,00008

Period (ms) =25000

Frequency MHz= 40

So I expect close to 40 Mhz on output pin. GPIO setting on pin for speed is Very High.

But my measures shows me, that real frequency is 32,15 Mhz on scope, and 33.33 on 100 Mhz longing device. Documentation explains, that timers are free-running hardware parts, so any interrupts may not have impact, and all depends only on system clock. Attached system clock screen.

Where I'm wrong? Thanks in advance !

Regards,

Steponas

10 REPLIES 10
TDK
Guru

You're probably just missing some edges due to the bandwidth limitations of the scope. What is a "100MHz longing device" ? Logging?

Slow things down by an order of magnitude and see if it matches expectations.

Can you show the scope plots?

> TimerClockFreq (s) =0,00008

What is this?

> Period (ms) =25000

This is incorrect, but probably just a typo.

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

Sorry, for some mistakes. English isn't native.

Scope Siglent 2102X 100 Mhz bandwidth, Logging device Kingst LA1010 100 Mhz. I added screens.

I'm using Excel for counting and my logic is (some variables is for formula and self check) as above. Because TimerClockFreq mus be in seconds it is =0,00008 s, and Period I've got is in micoseconds (yes, it's typo error). 25000 us convert to frequency = 1/25000*10^6 = 40 Mhz.

SZony.1
Associate II
 
TDK
Guru

Well that's definitely not close to 40MHz.

The CSI RC should be considerably more accurate than that.

What could be happening is that HAL is messing up the value in CSITRIM within the RCC->CSICFGR register which is causing the clock to be off by quite a bit. Check the value immediately after reset and ensure HAL doesn't change it. Just a guess.

You should average many readings together to get a more accurate reading in your logic analyzer, but that's not the issue here.

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

It is measured in debug mode, right after command HAL_TIM_PWM_Start(); Well, maybe debug is problem I'll try flashed MCU. Thanks.

More probably you're running from the 64 MHz HSI clock.

Get your RCC/PLL settings right first.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Do you mean those settings?

gregstm
Senior III

You could use the MCO output and its various configurations to help with your debugging - I often find it handy.

SZony.1
Associate II

I received Blue Pill from Aliexpress today, I’m not sure even the original chip. But it works as expected.