cancel
Showing results for 
Search instead for 
Did you mean: 

Discovery board PWM output example period value

jono-tree
Associate II
Posted on April 14, 2014 at 22:17

Hi all, just a simple question hopefully,

In the peripheral example for configuring TIM3 for PWM output, calculations for working out PSC and ARR values are given, comments supposing to explain them.

The bit thats confusing me is:

 To get TIM3 counter clock at 28 MHz, the prescaler is computed as follows:

       Prescaler = (TIM3CLK / TIM3 counter clock) - 1

       Prescaler = ((SystemCoreClock /2) /28 MHz) - 1

                                              

    To get TIM3 output clock at 30 KHz, the period (ARR)) is computed as follows:

       ARR = (TIM3 counter clock / TIM3 output clock) - 1

           = 665

If I'm understanding it right, the first calculation sets the counter clock at 28MHz.

The second one should then be that value divided by the output frequency you want, which seems to be 30kHz.

But 28000000/ 30000 =/= 666!!!!! It should be 933.3!!

I tried to consider what the value written to Auto-reload should be, by considering how many periods of 28MHz fit into one period of 30MHz and arrived at the same calculation as given above (2nd one).

Obviously I'm missing or misunderstanding something which is making my maths go off course here, because I'd assume they wouldn't publish such a crazy random and misleading mistake... Any help would be appreciated 

#output #stm32f4 #pwm #discovery
3 REPLIES 3
Posted on April 15, 2014 at 00:00

Yeah, I have no idea what you're doing.

For 30 KHz from 84 MHz

84,000,000 / 30,000 = 2,800

Ok, so the rational way to do this would be Prescaler = 1 - 1; // NO division, Period = 2800 - 1;

You're dealing with factors, integer ones, and the prescaler needs to fit in 16-bit, and the period in 16 or 32-bit depending on the counter. It's generally more helpful to keep the prescaler the smaller of the two.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jono-tree
Associate II
Posted on April 15, 2014 at 09:08

Thanks for the reply Clive, that definitely makes sense and is how I would have thought about it had I not tried to first follow the example.

But I still don't understand where they're getting that 665 from. In contrast to the approach you mentioned of only adjusting the period, they first first set a prescaler such that the counter clock will be at 28MHz:

 ''  To get TIM3 counter clock at 28 MHz, the prescaler is computed as follows:

       Prescaler = (TIM3CLK / TIM3 counter clock) - 1

       Prescaler = ((SystemCoreClock /2) /28 MHz) - 1 ''

By my maths that should be (84M/28M)-1 = 2

But then, the example claims to get output frequency (counter clock auto reload frequency) of 30kHz, that 28MHz counter clock then must be prescaled by (665+1).

That gives 42K.

In practise I would always use your approach, I just want to also understand the example given aswell.

Cheers,

Jonathan

Posted on April 15, 2014 at 18:53

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_PWM_Output\main.c

I have no idea where they pulled the numbers from, I'd presume they thought they were working with 20 MHz, not 28 MHz, or the code was just badly adapted from some other example.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..