Advanced Arduino: Direct Use of Atmega Counter/Timers
Advanced Arduino: Direct Use of Atmega Counter/Timers
The millis'( and micros'( functions use timer# to &eep trac& of the time since last reset. The Tone library uses whiche)er timer's( are needed for the pin's( in)ol)ed - see the pin*timer map below. analog-rite'( function uses whiche)er timer is needed for the pin in)ol)ed. The .er)o library uses timer1 to pro)ide interrupt-dri)en ser)o-signal generation.
The standard Arduino has ! timers timer# is 8 bit and used for the millis'( and micros'( functions. Timer1 is 16 bit and not used by default timer" is another 8 bit timer li&e timer# but not used by default. timer bits channel Arduino pin Mega pin timer# 8 timer# 8 A / 6 $ 1 1# 11 ! 1! 0 11 1" 1# 1 $ " ! 6
timer bits channel Arduino pin Mega pin timer0 16 / timer0 16 C timer$ 16 A timer$ 16 / timer$ 16 C 2 8 00 0$ 06
At restart the Arduino bootloader sets up the timers ready for analog-rite'( 3-% use. The timer*counters are set to phase-correct 8-bit 3-% mode and with cloc& prescaled to system cloc& di)ided by 60. 4owe)er timer# is set to fast 3-% mode for use with the millis'(*micros'( code. 5or standard Arduinos the system cloc& is 16%46 so that the timers are cloc&ing at "$#&46 by default. 3hase correct 8-bit 3-% mode ta&es $1# cloc&s to cycle and fast 8-bit 3-% mode ta&es "$6. This means a fre7uency of 01#46 for pins $ and 6 and 12246 for pins ! 1 1# 11 when used with analog-rite'(. 8t is possible to change the cloc& prescaling to change these default 3-% fre7uencies.
#95555 'MAX(. %any modes compare the OCR1A and OCR1B )alues to the TCNT1 at each cloc& and may change the output pins for those channels. 8n phase-correct 3-% mode the counter re)erses direction when getting to BOTTOM or TOP thus ramping both up and down. 8n other modes the counter =ust counts up and resets to BOTTOM 'usually 6ero( upon reaching TOP. There are interrupt handlers for se)eral e)ents associated with a timer namely o)erflow compare matching on A or / and input capture. The names for the interrupt )ectors are TIMER1_CAPT_vect TIMER1_COMPA_vect TIMER1_COMPB_vect TIMER1_OVF_vect. There is no CA3T )ector for timers # and " since they are 8-bit.
<ote that timer" has different prescaler codes from all the other timers - consult the datasheet. The output modes are "-bit )alues affecting the pin rele)ant to that channel with following meanings code meaning ## #1 1# 11 The 16 modes are summari6ed in this table.
Mode 3-% 3hase Correct 8-bit 3-% 3hase Correct 1-bit 3-% 3hase Correct 1#-bit CTC 5ast 3-% 8-bit 5ast 3-% 1-bit 5ast 3-% 1#-bit
TO
!pdate of OC"#x TO$# %lag &et %A? /;TT;% /;TT;% /;TT;% %A? T;3 T;3 T;3 /;TT;% /;TT;% /;TT;% /;TT;% %A? T;3 T;3
#95555 8mmediate #9##55 T;3 #9#155 T;3 #9#!55 T;3 ;CR1A 8mmediate #9##55 /;TT;% #9#155 /;TT;% #9#!55 /;TT;% /;TT;% T;3 8mmediate /;TT;%
3-% 3hase and 5re7uency Correct 8CR1 3-% 3hase Correct 3-% 3hase Correct CTC 'Reser)ed( 5ast 3-% 5ast 3-% 8CR1 8CR1 8CR1
;CR1A /;TT;%
<ote that the TOV1 flag is set when the counter o)erflows normally when it cloc&s round to 6ero or when it reaches MAX or TOP depending on the mode. This if enabled triggers the TIMER1_OVF_vect interrupt.
Counting
8n normal and CTC modes the counter is simply counting and not controlling the 3-% output channels - in CTC mode TOP can be controlled. To truly count the cloc& configuration should be set to the e9ternal cloc& options uses pin T1 which is Arduino pin $. 8n these modes the o)erflow interrupt only happens if the counter reaches MAX '#95555( - if this is handled then more than 16 bits of counting is possible with the aid of software. @ou can measure fre7uency say by using timer1 to count input pulses this way and sample the result in an o)erflow interrupt on a different timer 'which is used as a timer not a counter(.
Timing
Any mode can be used for timing if the cloc& source is set appropriately 'prescaled system cloc&(. Timing is simply counting system cloc&s. Accurate timing re7uires the system cloc& is crystal-controlled. The new Arduino Ano doesn,t ha)e a crystal cloc& and is not suitable for more than #."B accuracy. %ost crystal-deri)ed cloc&s gi)e better than #.#1B accuracy. 5or times to microsecond accuracy a prescale of 1 or 8 is needed and an interrupt handler should be used since polling is li&ely too slow and other interrupts generate =itter.