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

Last Update

March 21, 2023

inserted by FC2 system