AttachInterrupt()

出自YFRobotwiki
跳轉到: 導覽搜尋

描述

Digital Pins With Interrupts

attachInterrupt的第一個參數是中斷號。 通常,您應該使用digitalPinToInterrupt(引腳)將實際的數字引腳轉換為特定的中斷號。 例如,如果連接到引腳3,則使用digitalPinToInterrupt(3)作為attachInterrupt的第一個參數。

Board Digital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based 2, 3
Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
Zero all digital pins, except 4
MKR1000 Rev.1 0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Due all digital pins
101 all digital pins (Only pins 2, 5, 7, 8, 10, 11, 12, 13 work with CHANGE



注意:在附加函數中,delay()將不起作用,並且由millis()返回的值不會遞增。 在功能中接收的串行數據可能會丟失。 你應該聲明一個變量在未發生中斷時儲存變量。 有關詳細信息,請參閱下面的ISR部分。


使用中斷

中斷對於在微控制器程序中自動執行事件非常有用,並且可以幫助解決計時問題。 使用中斷的良好任務可能包括讀取旋轉編碼器或監視用戶輸入。

如果你想確保一個程序總是從旋轉編碼器捕獲脈衝,這樣就不會錯過脈衝,所以編寫一個程序來做任何其他事情會變得非常棘手,因為程序需要不斷地輪詢傳感器 用於編碼器的線,以便在發生時捕獲脈衝。 其他傳感器也具有類似的界面動態,例如嘗試讀取試圖抓住咔嗒聲的聲音傳感器,或者試圖抓住硬幣丟失的紅外線槽傳感器(光電斷路器)。 在所有這些情況下,使用中斷可以釋放微控制器以完成其他工作,而不會丟失輸入。


關於中斷服務程序

ISR是特殊的功能,它們具有大多數其他功能沒有的獨特的限制。 ISR不能有任何參數,它們不應該返回任何東西。

一般來說,ISR應儘可能短且快。 如果您的草圖使用多個ISR,則一次只能運行一次,其他中斷將在當前操作完成後依照其所擁有的優先級執行。 millis()依賴於中斷計數,所以它不會在ISR內增加。 由於delay()要求中斷工作,所以如果在ISR內部調用,它將不起作用。 micros()最初工作,但會在1-2毫秒後開始行為不規律。 delayMicroseconds()不使用任何計數器,因此它將正常工作。

通常,全局變量用於在ISR和主程序之間傳遞數據。 為了確保ISR和主程序之間共享的變量被正確更新,請將其聲明為 volatile

有關中斷的更多信息,請參閱Nick Gammon的筆記


語法


attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);	(recommended)
attachInterrupt(interrupt, ISR, mode);	                (not recommended)
attachInterrupt(pin, ISR, mode) ;	                (not recommended Arduino Due, Zero, MKR1000, 101 only)


參數

  • interrupt: the number of the interrupt (int)
  • pin: the pin number (Arduino Due, Zero, MKR1000 only)
  • ISR: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
  • mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
- LOW to trigger the interrupt whenever the pin is low,
- CHANGE to trigger the interrupt whenever the pin changes value
- RISING to trigger when the pin goes from low to high,
- FALLING for when the pin goes from high to low.
The Due board allows also:
- HIGH to trigger the interrupt whenever the pin is high.


返回

none


示例

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}


中斷引腳

通常您應該使用digitalPinToInterrupt(pin),而不是將中斷號碼直接放入草圖中。 具有中斷的特定引腳及其對中斷號的映射在每種類型的板上都不同。 直接使用中斷號碼可能看起來很簡單,但是當您的草圖在不同的電路板上運行時,可能會導致兼容性問題。

然而,較老的草圖通常具有直接的中斷號碼。 通常使用數字0(對於數字引腳2)或數字1(對於數字引腳3)。 下表顯示了各種電路板上可用的中斷引腳。

請注意,在下表中,中斷號碼是指傳遞給attachInterrupt()的號碼。 由於歷史原因,該編號並不總是直接對應於atmega芯片上的中斷編號(例如,int.0對應於Atmega2560芯片上的INT4)。

Board int.0 int.1 int.2 int.3 int.4 int.5
Uno, Ethernet 2 3
Mega2560 2 3 21 20 19 18
32u4-based (e.g Leonardo, Micro) 3 2 0 1 7
Due, Zero, MKR1000, 101 interrupt number = pin number



擴展閱讀

- detachInterrupt()




返回Arduino語法參考列表

更多建議和問題歡迎反饋至 YFRobot論壇