attachInterrupt()

설명

인터럽트 가진 디지털 핀

attachInterrupt() 의 첫 매개변수는 인터럽트 번호다. 일반적으로 digitalPinToInterrupt(pin) 을 써서 실제 디지털 핀을 특정 인터럽트 번호로 변환해야 한다. 예를 들어, 핀 3에 연결하면, digitalPinToInterrupt(3)attachInterrupt() 에 대한 첫번째 매개변수로 쓰세요.

보드 인터럽트에 쓸 수 있는 디지털 핀
우노, 나노, 미니 기타 328-기반 2, 3
메가, 메가2560, 메가ADK 2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
제로 all digital pins, except 4
MKR Family boards 0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Nano 33 IoT 2, 3, 9, 10, 11, 13, A1, A5, A7
Nano 33 BLE, Nano 33 BLE Sense all pins
두에 all digital pins
101 all digital pins (Only pins 2, 5, 7, 8, 10, 11, 12, 13 work with CHANGE)

※ 주의 및 경고:

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function. See the section on ISRs below for more information.

인터럽트 사용하기

Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input.

If you wanted to insure that a program always caught the pulses from a rotary encoder, so that it never misses a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the input.

인터럽트 서비스 루틴에 대하여

ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn't return anything.

Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have. millis() relies on interrupts to count, so it will never increment inside an ISR. Since delay() requires interrupts to work, it will not work if called inside an ISR. micros() works initially, but will start behaving erratically after 1-2 ms. delayMicroseconds() does not use any counter, so it will work as normal.

Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.

For more information on interrupts, see Nick Gammon's notes.

문법

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); (권고)

attachInterrupt(interrupt, ISR, mode); (권하지 않음)

attachInterrupt(pin, ISR, mode); (권하지 않음 Arduino Due, Zero, MKR1000, 101 전용)

매개변수

  • interrupt: 인터럽트 번호 (int)
  • pin: 핀 번호 (Arduino Due, Zero, MKR1000 only)
  • ISR: 인터럽트가 생기면 부를 ISR; 이 함수는 매개변수 없고 반환도 없음. 이 함수는 때때로 인터럽트 서비스 루틴으로 참조됨.
  • mode: 인터럽트가 언제 트리거 되야하는지 정의함. 4개 상수는 타당한 값으로 미리 정의 됨:
    • 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, Zero and MKR1000 boards allows also:
    • HIGH to trigger the interrupt whenever the pin is high.

반환값

  • 없음

예제 코드

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

인터럽트 번호

Normally you should use digitalPinToInterrupt(pin), rather than place an interrupt number directly into your sketch. The specific pins with interrupts, and their mapping to interrupt number varies on each type of board. Direct use of interrupt numbers may seem simple, but it can cause compatibility trouble when your sketch is run on a different board.

However, older sketches often have direct interrupt numbers. Often number 0 (for digital pin 2) or number 1 (for digital pin 3) were used. The table below shows the available interrupt pins on various boards.

Note that in the table below, the interrupt numbers refer to the number to be passed to attachInterrupt(). For historical reasons, this numbering does not always correspond directly to the interrupt numbering on the ATmega chip (e.g. int.0 corresponds to INT4 on the ATmega2560 chip).

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

For Uno WiFiRev.2, Due, Zero, MKR Family and 101 boards the interrupt number = pin number.

더보기

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES