user3619: I'm currently playing with Arduino's hardware timers, and a question came to my mind. Let me explain it a bit.
Let's suppose I want a certain function to execute every 1024 clock ticks. AFAIK, I could achieve this in several ways, playing with the prescaler value for Timer2, and its CTC:

Setting the prescaler to 1 and the CTC to 1024
Setting the prescaler to 8 and the CTC to 128
...
Setting the prescaler to 1024 and the CTC to 1.

All these ways would achieve what I want, but, which one is more efficient? Or it doesn't matter at all?
Thanks in advance for sharing your knowledge :P

user3731: If 'clock tick' count (here 1024), timer width and  MCU clock used are constant (a fixed value), I would not go for last option i.e. prescalar 1024 and CTC of 1. Here are my reasons (specially for Arduino ) 
1) When CTC is close to zero ( like 1-> 10), sometimes timer behaves as if count is zero and outputs a square wave (with 50% duty ratio) with half the demanded frequency. So make CTC reasonably big. But again selecting a high clock frequency (pre scalar 1) may also show reduced accuracy. A intermediate option I think is better.
2) When count CTC changes dynamically in a process, timer pre scalar should be selected such that CTC count will always be less than or equal to timer width over whole process. 
i.e. the maximum 'timer tick count' should be comfortably accommodated by the timer with it's given width. Even here same dilemma again occurs, then selection based on point 1 is better.
If this is not enough, then only way I think is to implement each case  physically and check which combination gives you the best results. Practical results I think are more reliable ( may be you will get a board specific efficient combination!)

user10794: There is a trade-off between resolution and achievable frequencies. You mention Timer 2, but on the Atmega328P Timer 2 is an 8-bit timer, thus you would not be able to set the CTC to 1024.
Let's assume we are talking about an 16-bit timer, like Timer 1 on the Atmega328P. With a prescaler of one, you can time (assuming a 16 MHz clock) from 1 to 65536 "ticks", that is 62.5 ns up to 4096 µs.
This would be the most precise measurement because you are using one (processor) clock tick per timer tick (a prescaler of one).
However if you plan to time for more than 4.096 ms then you need to bump up the prescaler. The next prescaler up on Timer 1 is 8, so now you can time for an interval 8 times as long (32768 µs) however your accuracy (precision) has now decreased by a factor of 8. The granularity of the timer has increased from 62.5 ns to 62.5 * 8 ns, which is 500 ns.
If you need to time longer than 32.768 ms then the prescaler has to be larger again, the next one being 64. So now you can time up to 262144 µs, but with a granularity of 62.5 * 64, which is 4000 ns (4 µs).
My suggestion would be to use the lowest prescaler that you can, but still get the interval you want. So obviously you can't use a prescaler of one to time 10 ms.

I have a discussion about timers on http://www.gammon.com.au/timers.
On that page is a chart which helps visualize the effects of different prescalers:

The top part (count of one) effectively gives you the granularity of each prescaler. For example, a prescaler of 256 has a granularity of 16,000 ns (16 µs). Certain frequencies (powers of 2) will lend themselves to combinations (eg. prescaler of 1 with a count of 256, or prescaler of 256 with a count of 1).
However for frequencies that don't have that property, the smaller prescaler will (if it can be used) give a finer granularity.

3619: <p>I'm currently playing with Arduino's hardware timers, and a question came to my mind. Let me explain it a bit.</p>

<p>Let's suppose I want a certain function to execute every 1024 clock ticks. AFAIK, I could achieve this in several ways, playing with the prescaler value for Timer2, and its CTC:</p>

<ol>
<li>Setting the prescaler to 1 and the CTC to 1024</li>
<li>Setting the prescaler to 8 and the CTC to 128</li>
<li>...</li>
<li>Setting the prescaler to 1024 and the CTC to 1.</li>
</ol>

<p>All these ways would achieve what I want, but, which one is more efficient? Or it doesn't matter at all?</p>

<p>Thanks in advance for sharing your knowledge :P</p>
 3731: <p>If 'clock tick' count (here 1024), timer width and  MCU clock used are constant (a fixed value), I would not go for last option i.e. prescalar 1024 and CTC of 1. Here are my reasons (specially for Arduino ) </p>

<p>1) When CTC is close to zero ( like 1-> 10), sometimes timer behaves as if count is zero and outputs a square wave (with 50% duty ratio) with half the demanded frequency. So make CTC reasonably big. But again selecting a high clock frequency (pre scalar 1) may also show reduced accuracy. A intermediate option I think is better.</p>

<p>2) When count CTC changes dynamically in a process, timer pre scalar should be selected such that CTC count will always be less than or equal to timer width over whole process. </p>

<p>i.e. the maximum 'timer tick count' should be comfortably accommodated by the timer with it's given width. Even here same dilemma again occurs, then selection based on point 1 is better.</p>

<p>If this is not enough, then only way I think is to implement each case  physically and check which combination gives you the best results. Practical results I think are more reliable ( may be you will get a board specific efficient combination!)</p>
 10794: <p>There is a trade-off between resolution and achievable frequencies. You mention Timer 2, but on the Atmega328P Timer 2 is an 8-bit timer, thus you would not be able to set the CTC to 1024.</p>

<p>Let's assume we are talking about an 16-bit timer, like Timer 1 on the Atmega328P. With a prescaler of one, you can time (assuming a 16 MHz clock) from 1 to 65536 "ticks", that is 62.5 ns up to 4096 µs.</p>

<p>This would be the most <em>precise</em> measurement because you are using one (processor) clock tick per timer tick (a prescaler of one).</p>

<p>However if you plan to time for more than 4.096 ms then you need to bump up the prescaler. The next prescaler up on Timer 1 is 8, so now you can time for an interval 8 times as long (32768 µs) however your accuracy (precision) has now decreased by a factor of 8. The <em>granularity</em> of the timer has increased from 62.5 ns to 62.5 * 8 ns, which is 500 ns.</p>

<p>If you need to time longer than 32.768 ms then the prescaler has to be larger again, the next one being 64. So now you can time up to 262144 µs, but with a granularity of 62.5 * 64, which is 4000 ns (4 µs).</p>

<p>My suggestion would be to use the lowest prescaler that you can, but still get the interval you want. So obviously you can't use a prescaler of one to time 10 ms.</p>

<hr>

<p>I have a discussion about timers on <a href="http://www.gammon.com.au/timers" rel="nofollow noreferrer">http://www.gammon.com.au/timers</a>.</p>

<p>On that page is a chart which helps visualize the effects of different prescalers:</p>

<p><a href="https://i.stack.imgur.com/9dz8M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9dz8M.png" alt="Timers and prescalers"></a></p>

<p>The top part (count of one) effectively gives you the granularity of each prescaler. For example, a prescaler of 256 has a granularity of 16,000 ns (16 µs). Certain frequencies (powers of 2) will lend themselves to combinations (eg. prescaler of 1 with a count of 256, or prescaler of 256 with a count of 1).</p>

<p>However for frequencies that don't have that property, the smaller prescaler will (if it can be used) give a finer granularity.</p>
