啟用和停用 Arduino 中的干擾


如果你想停用干擾(在執行某些關鍵程式碼時,尤其是應該在給定時段內完成的程式碼),可以使用 noInterrupts() 函式來實現。

關鍵程式碼執行完畢後,如果你想重新啟用干擾,可以使用 interrupts() 函式來實現。請注意,Arduino 中預設啟用干擾,因此,在沒有初始呼叫 noInterrupts() 的情況下呼叫 interrupts() 是沒有必要的。

示例

包含 noInterrupts() 和 interrupts() 的程式碼的一般結構如下 −

void setup() {
   // put your setup code here, to run once:
}

void loop() {
   // put your main code here, to run repeatedly:

   noInterrupts();
   //Add critical code that needs to be completed in a specific time below

   interrupts();
   //Add non-critical code, that can tolerate interruptions, below

}

更新於: 2021 年 5 月 31 日

7K+ 瀏覽次數

開啟你的職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.