当前位置:网站首页>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
边栏推荐
- New blog address
- Using fastjson to solve the problem of returning an empty array from a null value of a field string object
- June 13, 2022 Daily: Turing prize winner: what should we pay attention to if we want to succeed in our academic career?
- Pipelines in go concurrency mode
- 2022美容师(技师)上岗证题目及答案
- La différence entre philosophie et littérature
- [matlab] basic knowledge
- Binary search the specified number of numbers in the array binary advanced
- [matlab] matrix
- Conversion of integer part and decimal part between binary / octal / decimal / hexadecimal
猜你喜欢

Cve-2021-24078 vulnerability analysis

A simple deadlock example

也许尘埃落地,我们才能想清楚互联网的本质

Real time preview of PHP in browser by vscade

How to visit a website

DNS attack surface analysis
![[matlab] matrix transformation and matrix evaluation](/img/71/b7614e2e4ea2dda0f44f0ea8bcbf45.png)
[matlab] matrix transformation and matrix evaluation
![[error] invalid use of incomplete type uses an undefined type](/img/8a/7cb5d270cfd8831ddc146687fe4499.png)
[error] invalid use of incomplete type uses an undefined type

Use of split() method in string class

Several interview questions in TCP three grips and four swings
随机推荐
Handling method of wrong heading of VAT special invoice
为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
Using com0com/com2tcp to realize TCP to serial port (win10)
[MRCTF2020]Ez_bypass --BUUCTF
What are the levels of safety accidents
6.824 Lab 2: Raft
[LeetCode]3. The longest substring without duplicate characters forty
[MRCTF2020]Ez_ bypass --BUUCTF
TypeError: wave.ensureState is not a function
On the usage details and special usage of switch case
如何快速查询手机在网状态
[LeetCode]20. Valid parentheses thirty-six
What are the PMP scores?
MASA Auth - 从用户的角度看整体设计
TypeError: wave. ensureState is not a function
新增博客地址
Context of go concurrency mode
Machining Industry MES system Mold Industry MES system CNCl Medium Industry MES System MES code scanning and reporting MES data collection
分公司能与员工签劳动合同么
[LeetCode]1. Sum of two numbers thirty-four