Skip to main content
Associate III
October 13, 2023
Solved

Generated PWM resolution

  • October 13, 2023
  • 1 reply
  • 2096 views

Hi,

I am new to STM32 devices. I have a STM32F407 Discovery 1 board.

I wanted to generate a 1 KHZ signal with a 50% duty cycle.

The timer clock is configured to 84 MHZ

2.png

I am using TIM3 on channel 4. The prescaler is 210 and the counter period is 400 =>

84000000 / 210 / 400 is 1000 HZ

1.png

In my code I have the following:

 HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); // start PWM on TIM3 CH4 on PB1 pin
 PWM_Duty = 200; // set duty cycle to 50%
 __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_4, PWM_Duty); // set duty cycle of PWM pin, now PWM starts running

Here is what I am seeing on the scope:

PXL_20231013_173821654.jpg

As you can see the frequency is 992.9 HZ.

I am trying to understand the resolution and why it is not 1000 HZ?

Thanks for any info.

Brent

 

 

 

 

 

 

This topic has been closed for replies.
Best answer by Tesla DeLorean

Values for Prescaler and Period are N-1, you created  84000000 / 210+1 / 400+1 is  992.78 HZ

You want 210-1 and 400-1, the 50/50 point is still 200

For best resolution / granularity in width make the Prescaler as small as possible, and the Period as large as will fit in 16-bit TIM3

 

1 reply

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
October 13, 2023

Values for Prescaler and Period are N-1, you created  84000000 / 210+1 / 400+1 is  992.78 HZ

You want 210-1 and 400-1, the 50/50 point is still 200

For best resolution / granularity in width make the Prescaler as small as possible, and the Period as large as will fit in 16-bit TIM3

 

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