disableTimer()

Abstract

The disableTimer() initializes the timer/counter set by tone().

It checks the timer/counter number and stops it.

Source Code

The disableTimer() is defined in hardware/arduino/avr/cores/arduino/Tone.cpp as below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
void disableTimer(uint8_t _timer)
{
  switch (_timer)
  {
    case 0:
        TIMSK0 = 0;
      break;
 
   case 1:
      bitWrite(TIMSK1, OCIE1A, 0);
      break;
 
    case 2:
        bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
        TCCR2A = (1 << WGM20);
        TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
        OCR2A = 0;
      break;
  }
}

The input is _timer which is type uint8_t. There is no output value.

It branches according to the _timer. Currently it is 2 or -1. If the _timer is -1, it dose nothing and returns. If the _timer is 2, it sets TIMSK2, TCCR2A, TCCR2B and OCR2A to reset the timer/counter.

13
14
15
16
17
18
case 2:
    bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
    TCCR2A = (1 << WGM20);
    TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
    OCR2A = 0;
  break;

The TIMSK2 is a register to control interrupt mask. In the tone() it sets the OCIE2A bit to 1 to enable interrupt. Here it reset to 0 using bitWrite() to disable interrupt.

The TCCR2A and TCCR2B are registers to control timer/counter2. See analogWrite().

It sets the WGM20 bit of TCCR2A. In this case, timer/counter2 operates as Phase Correct PWM mode, which is used by analogWrite(). In the init(), dose the same.

The TCCR2B is set to bitwise AND of TCCR2B and 0b11111000, then set CS22 bit. This holds upper 5 bits and sets CS22 of lower 3 bits. This sets the division ratio of the clock to 64. In the init(), dose the same too.

Version

Arduino AVR Boards 1.8.6

License

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
  Copyright (c) 2019 garretlab.
  Added source code explanation by garretlab.
*/

/* Tone.cpp

  A Tone Generator Library

  Written by Brett Hagman

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Version Modified By Date     Comments
------- ----------- -------- --------
0001    B Hagman    09/08/02 Initial coding
0002    B Hagman    09/08/18 Multiple pins
0003    B Hagman    09/08/18 Moved initialization from constructor to begin()
0004    B Hagman    09/09/26 Fixed problems with ATmega8
0005    B Hagman    09/11/23 Scanned prescalars for best fit on 8 bit timers
                    09/11/25 Changed pin toggle method to XOR
                    09/11/25 Fixed timer0 from being excluded
0006    D Mellis    09/12/29 Replaced objects with functions
0007    M Sproul    10/08/29 Changed #ifdefs from cpu to register
0008    S Kanemoto  12/06/22 Fixed for Leonardo by @maris_HY
0009    J Reucker   15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62)
0010    jipp        15/04/13 added additional define check #2923
*************************************************/

Last Update

July 31, 2025

inserted by FC2 system