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

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

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.