detachInterrupt()

Abstract

The detachInterrupt() resets a function to be called when an external interrupt occurs.

The function is set using detachInterrupt().

Source Code

The detachInterrupt() is defined in hardware/arduino/avr/cores/arduino/WInterrupts.c as below. Only the souce code for Arduino UNO is quoted. The original source code supports many tips using #if’s.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
void detachInterrupt(uint8_t interruptNum) {
  if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
    // Disable the interrupt.  (We can't assume that interruptNum is equal
    // to the number of the EIMSK bit to clear, as this isn't true on the 
    // ATmega8.  There, INT0 is 6 and INT1 is 7.)
    switch (interruptNum) {
    case 0:
      EIMSK &= ~(1 << INT0);
      break;
 
    case 1:
      EIMSK &= ~(1 << INT1);
      break;
       
    case 2:
      break;       
    }
       
    intFunc[interruptNum] = nothing;
  }
}

The input is interruptNum, which is type of uint8_t. It has no output.

1
void detachInterrupt(uint8_t interruptNum) {

When the interrupt is less than EXTERNAL_NUM_INTERRUPTS, rest of the code is executed.

2
    if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {

Sets the value of register related to the interrupt number to disable the interrupt.

 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
    // ATmega8.  There, INT0 is 6 and INT1 is 7.)
    switch (interruptNum) {
    case 0:
      EIMSK &= ~(1 << INT0);
      break;
 
    case 1:
      EIMSK &= ~(1 << INT1);
      break;
       
    case 2:
      break;       
    }
       
    intFunc[interruptNum] = nothing;
  }
}

The EIMSK is a register to enable or disable external interrupts.

Sets nothing to intFunc[] to remove old function. The nothing is a function which dose nothing.

Version

Arduino AVR Boards 1.8.6

Last Update

March 21, 2023

inserted by FC2 system