当前位置:网站首页>Arduino interrupt

Arduino interrupt

2022-06-13 00:35:00 Flying husky

attachInterrupt()

explain

attachInterrupt() The function is used for Arduino Development board setup and execution ISR( Interrupt service routine ) With

ISR( Interrupt service routine ) As the name suggests, it is interruption Arduino Priority should be given to executing the interrupt service program for the current work . When the interrupt service program is completed , Come back and continue to do what you just did . Interrupt service program to monitor Arduino Input is of great use .

We can use attachInterrupt() function , utilize Arduino The pin of triggers the interrupt program . The following list shows which pins support interrupts :

Arduino Control panel Pins that support interrupts
Uno, Nano, Mini2, 3
Mega, Mega2560, MegaADK2, 3, 18, 19, 20, 21
Micro, Leonardo0, 1, 2, 3, 7
Zero except 4 All digital pins except pin No
MKR1000 Rev.10, 1, 4, 5, 6, 7, 8, 9, A1, A2
Due All digital pins

Be careful

stay ISR( Interrupt service routine ) Function ,delay() Functions don't work , and millis() The return value of the function no longer grows . stay ISR( Interrupt service routine ) Operation period Arduino The serial port data received by the development board may also be lost . in addition ISR The variables used in the function should be declared as volatile type . See below for details ” About ISR( Interrupt service routine )” part .

Use interrupt

Interrupts are good for performing tasks that require constant checking , For example, check whether the key switch connected to a pin is pressed . Interrupts are more suitable for signal checking that will soon disappear , For example, a certain pin is used to detect the pulse signal , The duration of this pulse signal may be very short . If you do not use interrupts , So if Arduino While the development board is performing other tasks , Suddenly the pulse came , Not yet Arduino The development board completes the work being executed , The pulse signal may have disappeared . And use interrupt , It can ensure that the transient pulse signal can be well Arduino The development board detects and executes the corresponding task .

About ISR( Interrupt service routine )

about Arduino For the development board ,ISR( Interrupt service routine ) It's a special function . Its particularity means that it has the limitations and characteristics that other types of functions do not have .

  • ISR A function cannot have any arguments .ISR And there's no return value .
  • Usually ISR The shorter the better ! Also, if you have more than one in your code ISR function , So every time Arduino Only one can run ISR function , Other ISR Function only in the current ISR After function execution , Can be executed according to its priority order .
  • millis() The operation of a function depends on Arduino Interrupt function of development board , therefore ISR Function millis() Functions don't work properly .micros() It's a similar situation , It can only be used in the initial 1-2 Can run in milliseconds , But after this 1-2 The problem started in milliseconds . delayMicroseconds() You don't need any counters to run , therefore delayMicroseconds() The operation will not be affected .
  • In general ,ISR The transfer of data between a function and the main program depends on global variables . To ensure that the global variable is in ISR Function can work normally , Should be possible to be ISR The global variables used in the function are declared as volatile type .

For more information on interrupts , Please refer to Nick Gammon’s notes.

grammar

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

Parameters

pin: Interrupt pin number
ISR: Interrupt service program name
mode: Interrupt mode

Interrupt mode (mode) There are several forms of :

LOW: When the pin is low, the interrupt service program is triggered
CHANGE: When the pin level changes, the interrupt service program is triggered
RISING: When the pin level changes from low level to high level, the interrupt service program is triggered
FALLING: When the pin level changes from high level to low level, the interrupt service program is triggered

Return value

nothing

Example

Arduino

const byte ledPin = 13; // use 2 No. pin as interrupt trigger pin const byte interruptPin = 2; volatile byte state = LOW; void setup() { pinMode(ledPin, OUTPUT); // Will interrupt the trigger pin (2 Pin No ) Set to INPUT_PULLUP( Input pull-up ) Pattern pinMode(interruptPin, INPUT_PULLUP); // Set the interrupt trigger attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE); } void loop() { digitalWrite(ledPin, state); } // Interrupt service routine void blink() { state = !state; }

const byte ledPin = 13;


// use 2 No. pin as interrupt trigger pin 

const byte interruptPin = 2;  


volatile byte state = LOW;


void setup() {

  pinMode(ledPin, OUTPUT);


  // Will interrupt the trigger pin (2 Pin No ) Set to INPUT_PULLUP( Input pull-up ) Pattern 

  pinMode(interruptPin, INPUT_PULLUP);


  // Set the interrupt trigger 

  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);

}


void loop() {

  digitalWrite(ledPin, state);

}


// Interrupt service routine 

void blink() {

  state = !state;

}

Be careful

  • In the interrupt service program , Out of commission delay() Functions and millis() function . Because they can't work properly in the interrupt service program .delayMicroseconds() It can work normally in the interrupt service program .
  • Interrupt service procedures should be kept as simple and short as possible . Otherwise, it may affect Arduino Work .
  • Variables involved in an interrupt service routine should be declared as volatile type .
  • The interrupt service routine cannot return any value . So you should try to use global variables in the interrupt service program .

Reprinted from Taiji maker

Taiji maker – Arduino, ESP8266 Applications of the Internet of things 、 Develop and learn materials

原网站

版权声明
本文为[Flying husky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280600042052.html