# Help with obtaining μs level PWM frequency setting for a STM32 board

Using a timer, I'm generating PWM 1kHz(1ms period) with the following clock and pre-scaler settings:

So to obtain 1kHz PWM freq. I use pre-scaler as 640-1 and counter period as 100:

So the math becomes PWM_freq = clock_freq/(psc * counter_per)

PWM_freq = 64000000 / (640 * 100) = 1kHz

The above corresponds to PWM period of 1000μs.

But I want to be able to modify the period between 600μs and 655μs with 1μs error tolerable.

How is that possible? Or if 1μs precision not possible, what is the best I can achieve? An example for achieving 655μs of similar value would be nice.

• I mean the actual period of the PWM not duty. Now I can set it to 1000μs because it is possible to obtain by division. But the number 655 is not easy to achive. Or is it? Jan 29, 2022 at 16:29
• @DKNguyen Can you explain this in an answer in more detail or illustarte. Im beginner in this. Jan 29, 2022 at 17:42
• Yes prescaler divides the clock freq. So lets say we have 16-bit timer. If the prescaler is set to 64, this means the timer will use 1MHz clock. After that Im a bit confused. So if I set "Counter Period" to 655 wouldnt the period be 655us? Jan 29, 2022 at 17:50
• I just need to set the freq 655us or 649us ect. It can be with 1us or 2us error. I dont need to change this period when during MCU running. At the beginning only. Jan 29, 2022 at 17:53
• I mean I need to set it a fixed value between 600 and 655us. So not during run time. Lets say I set it today to 655 and uploaded the code to the chip. And another day I might set it to 633 and upload the code to the chip. And with accuracy +/-1us or maybe 2us. Not during runtime. So 633.7 is fine if I wanted to set it to 633. Jan 29, 2022 at 17:57

Where did you get this equation from?

So the math becomes PWM_freq = clock_freq/(psc * counter_per)

PWM_freq = 64000000 / (640 * 100) = 1kHz

I'm going to guess you made that equation yourself. You need to read the User Reference Manual for this stuff. Don't solely rely on the configuration software and certainly do not guess what settings do in the configuration software.

The Counter Period, also known as the ARR (Auto-Rollover Register) that you set to 100 is not a divisor. It is the value that will trigger your counter register to rollover. Without it, your counter period would always have to be the maximum value that the counter register can hold.

The prescaler makes it so multiple clock ticks are required to increment the counter register. That makes each count in the counter register represent more time which allows the same sized counter to count for a longer maximum period of time at the expense of reduced resolution.

But the period does not have to be this maximum counter value. The ARR register determines the value at which the counter register will roll over back to zero when counting down. So your period can be anything shorter than that. And that time resolution is obviously the same as what each time interval one count of the counter represents (after the prescaler).

When counting up the ARR register determines the value that the counter will be set to once it reaches zero.

So all you have to do is set your prescaler to 64 so your 64MHz clock so each count in your counter register represents 1us. Then you set the ARR value between 600 and 655 since that's the number of counts that represents the time counter to stop or rollover for your period. That ARR value might be minus 1 due depending on whether it is the value that the rollover occurs on or just before. I can't remember. Check the User Reference Manual.