
107
ATmega8515(L)
2512A–AVR–04/02
Clear Timer on Compare
Match (CTC) Mode
In
clear timer on compare
orCTC mode (WGM13:0 = 4or 12), the OCR1A orICR1 Reg-
ister areused to manipulate the counterresolution. In CTC modethe counter iscleared
to zero when the counter value (TCNT1) matches either the OCR1A (WGM13:0 = 4) or
the ICR1 (WGM13:0 = 12).The OCR1A orICR1 define thetop value for the counter,
hencealsoitsresolution. This modeallows greatercontrol of the comparematch output
frequency. Italso simplifies theoperation ofcounting external events.
The timing diagram for the CTC modeisshowninFigure 51. The counter value
(TCNT1) increases until a comparematch occurs with eitherOCR1A orICR1, and then
counter(TCNT1) iscleared.
Figure 51. CTCMode, Timing Diagram
An interrupt can be generated at each time the counter value reaches theTOPvalue by
either using the OCF1A orICF1 flag according to the register used to define theTOP
value. If theinterruptis enabled, theinterrupt handlerroutine can beusedfor updating
theTOPvalue. However, changing theTOPtoavalue closetoBOTTOM when the
counter isrunning with none or a lowprescaler value must be done withcare sincethe
CTC mode does not have the double buffering feature. If thenew value written to
OCR1A orICR1 islower than the current value of TCNT1, the counterwill miss the com-
parematch.The counterwill then have to count to its maximum value (0xFFFF) and
wrap aroundstarting at 0x0000 beforethe comparematch can occur. Inmanycases
thisfeatureis not desirable. An alternative will then betousethe fastPWM modeusing
OCR1A fordefining TOP (WGM13:0 = 15)sincethe OCR1A then will be double
buffered.
For generating a waveform output in CTC mode, the OC1A output can be set to toggle
itslogicallevel on each comparematch by setting the Compare Output mode bits to tog-
glemode (COM1A1:0 = 1).The OC1A value will not bevisibleonthe port pin unless the
data direction for the pin isset to output (DDR_OC1A = 1).The waveform generatedwill
haveamaximum frequency off
OC
1
A
=f
clk_I/O
/2 when OCR1A isset to zero (0x0000).The
waveform frequency isdefinedbythe following equation:
The N variable represents the prescalerfactor(1, 8,64, 256, or 1024).
Asfor thenormal modeof operation, theTOV1 flag isset in the same timerclock cycle
that the countercountsfrom MAX to 0x0000.
TCNTn
OCnA
(Toggle)
OCnA Interrupt Flag Set
or ICFn Interrupt Flag Set
(Interrupt on TOP)
1 4
Period
2 3
(COMnA1:0 = 1)
OCnA
f
clk_I/O
2 N 1 OCRnA+()⋅⋅
---------------------------------------------------=
Komentáře k této Příručce