attachInterrupt()
Description
Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The table below shows the available interrupt pins on various boards.
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all available pins. You can directly specify the pin number in attachInterrupt().
attachInterrupt() 函式
attachInterrupt() 函式的用途是用來指定外部中斷的處理函式(Interrupt Service Routine, ISR)。
attachInterrupt() 函式有三個參數:
interrupt: 外部中斷的編號。
function: 中斷處理函式(Interrupt Service Routine, ISR)。中斷處理函式必須是不接受參數而且不回傳任何東西。
mode: 定義什麼狀況下該觸發中斷,有四個可以設定的常數值:
LOW: 當 pin 為 LOW 時觸發中斷
CHANGE: 當 pin 狀態改變時觸發中斷,不管是從 HIGH 到 LOW 或從 LOW 到 HIGH
RISING: 當 pin 狀態從 LOW 到 HIGH 時觸發中斷,RISING 又稱正緣觸發
FALLING: 當 pin 狀態從 HIGH 到 LOW 時觸發中斷,FALLING 又稱負緣觸發
如果要移除外部中斷服務函式,就使用 detachInterrupt() 函式。
啟用與停止中斷
如果要停止 Arduino 所有中斷,可以呼叫 noInterrupt() 函式,要重新啟用中斷,只要呼叫一次 interrupts() 函式即可。