detachInterrupt()

概要

detachInterrupt()は、外部割り込みが発生したときに呼び出す関数の登録を解除します。detachInterrupt()のリファレンスはこちらを参照してください。

外部割り込みが発生したときに呼び出す関数の登録は、attachInterrupt()を使って設定します。割り込みの詳細は、attachInterrupt()を参照してください。

ソースコード

detachInterrupt()は、hardware/arduino/avr/cores/arduino/WInterrupts.c に定義されています。以下に全ソースコードを示します。Arduino Unoの場合に適用される部分だけを抜粋しています。実際には#if によりさまざまなチップに対応しています。

 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;
  }
}

入力は、interruptNumで、uint8_t型です。出力はありません。

1
void detachInterrupt(uint8_t interruptNum) {

割り込み番号(interruptNum)が、EXTERNAL_NUM_INTERRUPTS未満のとき、それ以降を実行します。

2
    if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {

割り込み番号に対応したレジスタの値を設定し、割り込みを無効にします。

 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;
  }
}

EIMSKは、外部割り込みの許可・禁止を制御するレジスタです。

intFunc[]にnothingを設定して、登録してあった関数を削除します。nothingは、何もしない関数です。

バージョン

Arduino AVR Boards 1.8.6

ライセンス

 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
/*
  Copyright (c) 2016 garretlab.
  Added source code explanation by garretlab.
*/

/*
  Part of the Wiring project - http://wiring.uniandes.edu.co

  Copyright (c) 2004-05 Hernando Barragan

  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., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA

  Modified 24 November 2006 by David A. Mellis
  Modified 1 August 2010 by Mark Sproul
*/

最終更新日

July 31, 2025

inserted by FC2 system