10
\$\begingroup\$

Just a basic question... For arduino/avr/ATMega328 I find lots of examples using Timer1 (there's even a whole library for it) but rarely any that utilize Timer0 (or Timer2).

Now, I know that using any of these timers to control an ISR method will disable the use of PWM on it's associated pins, but is there a specific reason that people seem to stay away from Timer0? Is it used for something else internally that one should not mess with?

My current application is fine using Timer1, I'm just curious if I'm missing something about these other often neglected timers.

\$\endgroup\$

2 Answers 2

16
\$\begingroup\$

If the question is about a bare AVR microcontroller, then no, there is no constraint on using any of the timers.

  • For the Arduino, Timer0 is best avoided, as the millis(), delay() and all internal timekeeping in the Arduino libraries use this timer. Changing it's time constant will impact these operations.
  • Timer1, as the question states, is the popular first choice for any timer / counter tasks, through libraries such as the TimerOne library, as well as direct manipulation. It is a 16-bit timer, thus with finer resolution than the 8-bit timers.
  • Timer2 is used by the tone() library, but since that is not so ubiquitously used, there is also the msTimer2 library that many find very useful, much like TimerOne. Direct manipulation of Timer2 is fine too.
  • The additional timers on the Mega, Timer3, Timer4 and Timer5, each a 16-bit timer, are also available for use, if you use an Arduino Mega board.
\$\endgroup\$
0
9
\$\begingroup\$

Apart from disabling the associated PWM other reasons I can think of are:

  • TIMER0 is an 8-bit timer while TIMER1 is a 16-bit timer.

  • In the Arduino world TIMER0 is used for the timer functions like delay() and millis(). A lot of other code I've seen uses it the same way because 8-bit resolution is normally ample.

  • TIMER2 is also an 8-bit, I can't think of a reason to specifically avoid that if only 8-bits is required. It is used by the Arduino tone() function however.

So I guess that leaves TIMER1 as a fairly generic choice and is the only choice on the smaller AVRs when a 16-bit timer is required. AVR Mega devices also contain TIMER3 - TIMER5 that are all 16-bit.

\$\endgroup\$

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.