You are on page 1of 5

Advanced Arduino: direct use of ATmega counter/timers

What are the counter/timers


The Arduino platform consists of a set of software libraries that run on a group of microcontroller chips. Currently the supported micro-controllers are the AVR ATmega168 ATmega!"8 and the more featureful ATmega1"8# and ATmega"$6# used in the Arduino %ega. Also smaller chips li&e the ATtiny are useable if you are prepared to do some more homewor&. Currently the Arduino platform uses the ATmega series but the software is not strongly tied to any one architecture 'in principle(. These notes are howe)er firmly tied to the ATmega architecture and the ATmega168 and ATmega!"8 chips of the standard Arduinos. The ATmega1"8# and "$6# are )ery similar howe)er 'but with more counter*timers(.

Where to find things out


All things Arduino can be found at or from the Arduino wesbite. +oogle naturally is )ery useful for locating more resources. The chip datasheets can be located in numerous places but Atmel,s website has them at their ATmega!"8p doc page.

Existing use/support of counter/timers in Arduino libraries


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

timer1 16 A timer1 16 / timer" 8 timer" 8 A /

timer! 16 A timer! 16 / timer! 16 C timer0 16 A

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.

ATmega counter-timer basics


The counter*timer hardware modules are )ery fle9ible and consist of a set of registers to control the beha)iour as well as the counter*timer register itself. 16 bit timers ha)e mainly 16-bit registers 'although the control registers are " separate 8-bit ones in all timers(. 8n the following description 8 shall use timer1,s register names but the naming is systematic and replacing the ,1, by another number you get the corresponding names. TCCR1A and TCCR1B are the two 8-bit control registers. These set up the cloc& prescale ratio set the mode for the counter*timer and set the output control for the 3-% pins. TCNT1 is the 16 bit 'for timer1( counter )alue itself. At the assembly language le)el this register has to be written or read as two 8-bit half-registers and in a certain order. The +CC AVR compiler used by the Arduino software automatically generates the two accesses as if this was a single 16 bit transfer. 4owe)er if you ha)e an interrupt handler that also accesses 16 bit registers in the same timer you must protect accesses by disabling interrupts across the access. :ach timer has " or ! channels which control a dedicated pin if the rele)ant output mode is set and that pin is enabled as an output. The OCR1A and OCR1B are the 16 bit ;utput Compare Registers that apply to the two channels 'A and /( that timer1 has. The %ega has timers ! 0 and $ that each ha)e ! channels A / and C. The same considerations about 16 bit transfers as for the TC<T1 register of course. The 8nput Capture Register ICR1 is a 16 bit register used to record the )alue of TCNT1 when an e9ternal e)ent happens - typically a change on the ICP1 pin 'Arduino pin 8(. ;nly 16 bit timers ha)e input capture. The basic operation of the unit common to most modes is for the counter register TCNT1 to count up and*or down between )alues BOTTOM 'which is 6ero( and TOP. 8n some modes TOP is pro)ided by a register such as OCR1A or ICR1 in other modes it is fi9ed at #955 or #9155 or #9!55 or

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

Control register set-up


The TCCR1A and TCCR1B registers can be programmed thus>
void timer1_setup (b te mode! i"t presc#$e! b te outmode_A! b te outmode_B! b te c#pture_mode% & '' e"(orce (ie$d )idt*s (or s#"it mode +, 1- . outmode_A +, / . outmode_B +, / . c#pture_mode +, / . b te c$oc0_mode , 1 . '' 1 me#"s "o c$oc0i"2 3 t*e cou"ter is (ro4e"5 s)itc* (presc#$e% & c#se 16 c$oc0_mode , 1 . bre#0 . c#se 76 c$oc0_mode , 8 . bre#0 . c#se 9:6 c$oc0_mode , / . bre#0 . c#se 8-96 c$oc0_mode , : . bre#0 . c#se 118:6 c$oc0_mode , - . bre#0 . de(#u$t6 i( (presc#$e ; 1% c$oc0_mode , < . '' e=ter"#$ c$oc0 > TCCR1A , (outmode_A ;; 9% ? (outmode_B ;; :% ? (mode + /% . TCCR1B , (c#pture_mode ;; 9% ? ((mode + 1=C% ;; 1% ? c$oc0_mode . >

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

code # 1 " ! 0 $ 6 2 8 1 1# 11 1" 1! 10 1$ <ormal

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

3-% 3hase and 5re7uency Correct ;CR1A /;TT;% ;CR1A T;3

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

ulse Width Modulation - WM


There seem to be a bewildering )ariety of 3-% modes but the basic )arieties are fast phasecorrect and fre7uency-and-phase-correct. 5ast simply counts up till TOP and resets to 6ero 'BOTTOM(. The others count up to TOP and then count down to BOTTOM and then count up again etc. This &eeps the wa)eform symmetric as the duty-cycle )aries but has nearly twice the period of the e7ui)alent fast more. 5re7uency-correct really means fre7uency-settable in that the )alue of TOP is not constrained to a power of two but is controlled by either ICR1 or OCR1A in particular the )alue of T;3 is carefully changed only at a safe point when TCNT1 ,, BOTTOM. .e)eral of the modes set the counter to being effecti)ely 8 1 or 1# bits. The default arrangement on the Arduino is 8-bit phase-correct 'or on timer# 8-bit fast(.

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.