cancel
Showing results for 
Search instead for 
Did you mean: 

Generated PWM resolution

BAW
Associate III

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

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

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
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

1 REPLY 1

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
Up vote any posts that you find helpful, it shows what's working..