当前位置:网站首页>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, Mini | 2, 3 |
| Mega, Mega2560, MegaADK | 2, 3, 18, 19, 20, 21 |
| Micro, Leonardo | 0, 1, 2, 3, 7 |
| Zero | except 4 All digital pins except pin No |
| MKR1000 Rev.1 | 0, 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
边栏推荐
- 6.824 Lab 2: Raft
- Assembly language learning
- Explain bio, NiO, AIO in detail
- antdPro - ProTable 实现两个选择框联动效果
- Several interview questions in TCP three grips and four swings
- How to quickly query the mobile phone number home and operator
- 测试平台系列(97) 完善执行case部分
- [matlab] basic operation
- 63. different paths II
- 睡前小故事之MySQL起源
猜你喜欢

Tsinghua Bosch joint ml center, thbi lab:cheng Yang Ying | realize safety reinforcement learning through the value at risk of constraints

Use of split() method in string class
![[GXYCTF2019]禁止套娃--详解](/img/c8/8c588ab8f58e2b38b9c64c4ccd733f.png)
[GXYCTF2019]禁止套娃--详解

A simple deadlock example

What are the levels of safety accidents

Another year 1024, happy programmer's Day!

Target recognition gadget

JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception

Maya modeling VI

Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
随机推荐
Apispace empty number detection API interface is free and easy to use
Matlab [path planning] - UAV drug distribution route optimization
JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception
antdPro - ProTable 实现两个选择框联动效果
ik分词器的安装
Paper reading and sharing
USTC of China University of science and technology: Minrui Wang | distribution network voltage stabilization based on transformer Multi-Agent Reinforcement Learning
6.824 Lab 2: Raft
通过抓包下载钉钉直播回放
[LeetCode]1. Sum of two numbers thirty-four
[LeetCode]28. Implement strstr()
[MRCTF2020]Ez_bypass --BUUCTF
Kali system -- dnsmap for DNS collection and analysis
Map from getting started to performance optimization
2022施工員-設備方向-通用基礎(施工員)操作證考試題及模擬考試
[supersocket 2.0] supersocket 2.0 from the beginning to the end
Conversion of integer part and decimal part between binary / octal / decimal / hexadecimal
DNS attack surface analysis
Penetration test summary
BUUCTF之BabySQL[极客大挑战 2019]