1
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ What "formulas" do you want? - Note that dividing by 0 does not make any sense, so that it makes sense to use 0 for 1, 1 for 2, and 65535 for 65536. \$\endgroup\$
    – JimmyB
    Commented Dec 1, 2015 at 22:10

1 Answer 1

2
\$\begingroup\$

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).

\$\endgroup\$
2
  • \$\begingroup\$ Has the OP changed the question? The answer "is a 16 bit timer, it is impossible to get it to count to 4 million" makes no sense, so maybe delete that? What does "flank" mean in the context of "the 40000th flank"? To me, "flank" means the side of something, or a cut of steak, used in Fajitas, yum! \$\endgroup\$
    – gbulmer
    Commented Dec 1, 2015 at 23:28
  • \$\begingroup\$ @gbulmer Yeah the question was edited, there was a 4 million inside the original question. Flank was a false friend - in German the edge of a clock signal is called "Flanke" (or at least can be called that), I have a headache, so my English suffers a bit. Thanks for pointing that out. \$\endgroup\$
    – Arsenal
    Commented Dec 1, 2015 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.