The TIMER2_COMPA_vect is an interrupt handler which is called when the timer/counter2(TCNT2) and compare register(OCR2A) becomes the same value. It is used to control the output of tone().
Source Code
The TIMER2_COMPA_vect is defined in hardware/arduino/avr/cores/arduino/Tone.cpp as below.
volatilelongtimer2_toggle_count;volatileuint8_t*timer2_pin_port;volatileuint8_ttimer2_pin_mask;ISR(TIMER2_COMPA_vect){if(timer2_toggle_count!=0){// toggle the pin
*timer2_pin_port^=timer2_pin_mask;if(timer2_toggle_count>0)timer2_toggle_count--;}else{// need to call noTone() so that the tone_pins[] entry is reset, so the
// timer gets initialized next time we call tone().
// XXX: this assumes timer 2 is always the first one used.
noTone(tone_pins[0]);// disableTimer(2);
// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
}}
The timer2_toggle_count is a variable which holds how long the tone() outputs HIGH. The timer2_pin_port holds the port(digitalWrite()) corresponding to the output pin of tone(). The timer2_pin_mask holds bit mask corresponding to the port.
The ISR() is a macro to define an interrupt handler.
8
9
10
11
12
13
14
15
if(timer2_toggle_count!=0){// toggle the pin
*timer2_pin_port^=timer2_pin_mask;if(timer2_toggle_count>0)timer2_toggle_count--;}
If the timer2_toggle_count is not 0, it inverts the output of timer2_pin_mask of timer2_pin_port. This toggles ON and OFF of the output pin at each interrupt. As the output is inverted constant period, the duty ratio is 50%.
If the time2_toggle_count is greater than 0, it is decremented.
16
17
18
19
20
21
22
23
24
25
else{// need to call noTone() so that the tone_pins[] entry is reset, so the
// timer gets initialized next time we call tone().
// XXX: this assumes timer 2 is always the first one used.
noTone(tone_pins[0]);// disableTimer(2);
// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
}}
If the timer2_toggle_count becomes 0, the noTone() is called to turn off the tone.