196 7. TIMING SUBSYSTEM
Select the source of the Timer 2 clock using Clock Select (CS2[2:0]) bits.
The bit settings for the TCCR2A and B registers are summarized in Figure 7.15.
7.8.0.9 Timer/Counter Register(TCNT2)
The TCNT2 is the 8-bit counter for Timer 2.
7.8.0.10 Output Compare Register A and B (OCR2A and B)
The OCR2A and B registers hold a user-defined 8-bit value that is continuously compared to the
TCNT2 register.
7.8.0.11 Timer/Counter Interrupt Mask Register 2 (TIMSK2)
Timer 2 uses the Timer/Counter 2 Output Compare Match Interrupt Enable A and B (OCIE2A
and B) bits and the Timer/Counter 2 Overflow Interrupt Enable A and B (OIE2A and B) bits.When
the OCIE2A or B bit and the I-bit in the Status Register are both set to one, the Timer/Counter 2
Compare Match interrupt is enabled. When the TOIE2 bit and the I-bit in the Status Register are
both set to one, the Timer/Counter 2 Overflow interrupt is enabled.
7.8.0.12 Timer/Counter Interrupt Flag Register 2 (TIFR2)
Timer 2 uses the Output Compare Flags 2 A and B (OCF2A and B) which sets for an output
compare match. Timer 2 also uses the Timer/Counter 2 Overflow Flag (TOV2) which sets when
Timer/Counter 2 overflows.
7.9 PROGRAMMING THE ARDUINO UNO R3 USING THE
BUILT-IN ARDUINO DEVELOPMENT ENVIRONMENT
TIMING FEATURES
The Arduino Development Environment has several built-in timing features. These include:
delay(unsigned long): The delay function pauses a sketch for the amount of time specified in
milliseconds.
delayMicroseconds(unsigned int): The delayMicroseconds function pauses a sketch for the
amount of time specified in microseconds.
pulseIn(pin, value): The pulseIn function measures the length of an incoming digital pulse. If
value is specified as HIGH, the function waits for the specified pin to go high and then times
until the pin goes low. The pulseIn function returns the length of elapsed time in microseconds
as an unsigned long.
analogWrite(pin, value): The analog write function provides a pulse width modulated
(PWM) output signal on the specified pin. The PWM frequency is approximately 490 Hz.
The duty cycle is specified from 0 (value of 0) to 100 (value of 255) percent.
7.9. PROGRAMMING THE ARDUINO UNO R3 197
Clock Select
CS2[2:0] Clock Source
000 None
001 clk
I/0
010 clk
I/0
/8
011 clk
I/0
/32
100 clk
I/0
/64
101 clk
I/0
/128
110 clk
I/0
/256
111 clk
I/0
/1024
Timer/Counter Control Register B (TCCR2B)
70
FOC2A
CS22 CS21 CS20
---
---
WGM22
FOC2B
Timer/Counter Control Register A (TCCR2A)
WGM20
COM2B1 COM2B0
WGM21
COM2A1 COM2A0
--- ---
Mode
0
1
2
3
4
5
6
7
WGM[02:00]
000
001
010
011
100
101
110
111
Mode
Normal
PWM, Phase Correct
CTC
Fast PWM
Reserved
PWM, Phase Correct
Reserved
Fast PWM
Waveform Generation
Mode
COM2A[1:0]
00
01
10
11
Description
Normal, OC2A disconnected
WGM22 = 0: normal operation,
OC2A disconnected
WGM22 = 1: Toggle OC2A on
compare match
Clear OC2A on compare match,
set OC2A at Bottom
(non-inverting mode)
Set OC2A on compare match,
clear OC2A at Bottom
(inverting mode)
COM2A[1:0]
00
01
10
11
Description
Normal, OC2A disconnected
Toggle OC2A on compare match
Clear OC2A on compare match
Set OC2A on compare match
COM2A[1:0]
00
01
10
11
Description
Normal, OC2A disconnected
WGM22 = 0: normal operation,
OC2A disconnected
WGM22 = 1: Toggle OC2A on
compare match
Clear OC2A on compare match,
when upcounting. Set OC2A on
compare match when down counting
Set OC2A on compare match,
when upcounting. Set OC2A on
compare match when down counting
Compare Output Mode, Phase Correct PWM
Compare Output Mode, non-PWM Mode
Compare Output Mode, Fast PWM Mode
COM2B[1:0]
00
01
10
11
Description
Normal, OC2B disconnected
Reserved
Clear OC2B on compare match,
set OC2B at Bottom
(non-inverting mode)
Set OC2B on compare match,
clear OC2B at Bottom
(inverting mode)
COM2B[1:0]
00
01
10
11
Description
Normal, OC2B disconnected
Toggle OC2B on compare match
Clear OC2B on compare match
Set OC2B on compare match
COM2B[1:0]
00
01
10
11
Description
Normal, OC2B disconnected
Reserved
Clear OC2B on compare match,
when upcounting. Set OC2B on
compare match when down counting
Set OC2B on compare match,
when upcounting. Set OC2B on
compare match when down counting
Compare Output Mode, Phase Correct PWM
Compare Output Mode, non-PWM Mode
Compare Output Mode, Fast PWM Mode
Figure 7.15: Timer/Counter Control Register A and B (TCCR2A and B) bit settings.

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.