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

asked Dec 1, 2015 at 21:56
\$\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\$ 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).

answered Dec 1, 2015 at 22:13
\$\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\$ 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\$ Commented Dec 1, 2015 at 23:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.