93
2513L–AVR–03/2013
ATmega162/V
Using the Output
Compare Unit
Since writing TCNT0 in any mode of operation will block all compare matches for one timer clock
cycle, there are risks involved when changing TCNT0 when using the output compare channel,
independently of whether the Timer/Counter is running or not. If the value written to TCNT0
equals the OCR0 value, the Compare Match will be missed, resulting in incorrect waveform gen-
eration. Similarly, do not write the TCNT0 value equal to BOTTOM when the counter is down-
counting.
The setup of the OC0 should be performed before setting the Data Direction Register for the port
pin to output. The easiest way of setting the OC0 value is to use the Force Output Compare
(FOC0) strobe bits in Normal mode. The OC0 Register keeps its value even when changing
between Waveform Generation modes.
Be aware that the COM01:0 bits are not double buffered together with the compare value.
Changing the COM01:0 bits will take effect immediately.
Compare Match
Output Unit
The Compare Output mode (COM01:0) bits have two functions. The Waveform Generator uses
the COM01:0 bits for defining the Output Compare (OC0) state at the next Compare Match.
Also, the COM01:0 bits control the OC0 pin output source.
Figure 36 shows a simplified sche-
matic of the logic affected by the COM01:0 bit setting. The I/O Registers, I/O bits, and I/O pins in
the figure are shown in bold. Only the parts of the general I/O Port Control Registers (DDR and
PORT) that are affected by the COM01:0 bits are shown. When referring to the OC0 state, the
reference is for the internal OC0 Register, not the OC0 pin. If a System Reset occur, the OC0
Register is reset to “0”.
Figure 36. Compare Match Output Unit, Schematics
The general I/O port function is overridden by the Output Compare (OC0) from the waveform
generator if either of the COM01:0 bits are set. However, the OC0 pin direction (input or output)
is still controlled by the Data Direction Register (DDR) for the port pin. The Data Direction Regis-
ter bit for the OC0 pin (DDR_OC0) must be set as output before the OC0 value is visible on the
pin. The port override function is independent of the Waveform Generation mode.
PORT
DDR
DQ
OCn
Pin
OCn
DQ
Waveform
Generator
COMn1
COMn0
0
1
D
ATA
B
U
S
FOCn
clk
I/O