Electronic – Calculating Timer Tick STM32F4

microcontrollerstm32f4timer

I'm trying to use the timer (TIM4) to log rising and falling edge times from an external sensor.
I need to set the timer so that it ticks every 1µs and has a period of 40ms (so counts 40, 000 ticks before overflowing).
TIM4 has a source clock speed of 84MHz.
The datasheet says:

"16-bit programmable prescaler used to divide the counter clock
frequency by any factor between 1 and 65536".

However in most tutorials divide by 84 – 1 to obtain 1MHz. Can someone provide the exact formulas to calculate the parameters? Thanks in advance

Best Answer

The prescaler register description states, that the input clock is divided by the register value + 1. So if your input frequency is 84 MHz and you want the timer to count at 1 MHz you have to program 84-1 to the PSC register to get a divider of 84 and thus a counter clock of 1 MHz.

The internal PSC counter is not accessible, so there is no work around for the 16 bit limitation.

Program the ARR register with 39999, the overflow will occur on the next (the 40000th edge).