Arduino 中主要有时钟中断和外部中断,本文所说的中断指的是外部中断。Arduino 中的外部中断通常是由Pin 口(数字 Pin 口,不是模拟口)电平改变触发的。每种型号的 Arduino 版都有数个 Pin 口可以用来注册中断,具体如下:
开发板 | 可以用来注册中断的Pin口 |
---|---|
Uno, Nano, Mini, other 328-based | 2, 3 |
Uno WiFi Rev.2 | 所有数字口 |
Mega, Mega2560, MegaADK | 2, 3, 18, 19, 20, 21 |
Micro, Leonardo, other 32u4-based | 0, 1, 2, 3, 7 |
Zero | 除了4号口外的所有数字口 |
MKR Family boards | 0, 1, 4, 5, 6, 7, 8, 9, A1, A2 |
Due | 所有数字口 |
101 | 所有数字口 (只有 2, 5, 7, 8, 10, 11, 12, 13数字口可以使用 ?CHANGE ? 类型中断,中断类型在下文有介绍) |
注册中断主要是通过? attachInterrupt()
?函数实现的,其原型为:
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
- 第一个参数为中断号,Arduino上每个可以注册中断的Pin口都会被分配一个中断号,这里需要传入的是中断号而不是Pin口号。但是不同的Arduino开发板上面的中断号分配并不完全一样。各个开发板的Pin口号和中断号对应关系如下:
开发板 中断号0 中断号1 中断号2 中断号3 中断号4 中断号5 Uno, Ethernet PIN 2 PIN 3 Mega2560 PIN 2 PIN 3 PIN 21 PIN 20 PIN 19 PIN 18 基于32u4的开发板 如 Leonardo, Micro PIN 3 PIN 2 PIN 0 PIN 1 PIN 7
这种方式来注册中断号。attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
- 第二个参数是中断服务例程(ISR)的函数指针,在 C/C++ 中直接写该函数的函数名即可。在触发时,该函数将会被调用。该函数必须没有任何的参数也没有任何的返回值。
- 第三个参数是中断触发条件,由几个可选的值:
LOW 当中断所在 Pin 口处于低电平时触发
CHANGE 当中断所在 Pin口电平改变时触发
RISING 当中断所在Pin口从低电平变为高电平(上升沿)时触发
FALLING 当中断所在Pin口从高电平变为低电平(下降沿)时触发
对于 Due,Zero 和 MKR1000开发板,还有一个 HIGH 表示当中断所在 Pin 口处于高电平时触发
示例
int pin = 2; //define interrupt pin to 2 volatile int state = LOW; // To make sure variables shared between an ISR //the main program are updated correctly,declare them as volatile. void setup() { pinMode(13, OUTPUT); //set pin 13 as output attachInterrupt(digitalPinToInterrupt(pin), blink, CHANGE); //interrupt at pin 2 blink ISR when pin to change the value } void loop() { digitalWrite(13, state); //pin 13 equal the state value } void blink() { //ISR function state = !state; //toggle the state when the interrupt occurs }
attachInterrupt语句语法
attachInterrupt(digitalPinToInterrupt(pin),ISR,mode);//recommended for arduino board attachInterrupt(pin, ISR, mode) ; //recommended Arduino Due, Zero only //argument pin: the pin number //argument ISR: the ISR to call when the interrupt occurs; //this function must take no parameters and return nothing. //This function is sometimes referred to as an interrupt service routine. //argument mode: defines when the interrupt should be triggered.
在 Arduino 中使用中断需要注意的问题
- 由于中断会打断正常代码的运行,因此 ISR 的应该尽可能快地执行完毕。
- 在 ISR 中修改的全局变量要用 volatile 修饰符修饰以防止编译器优化
- 在 ISR 中不能使用其他用中断实现的函数,如 millis() delay() 等。延时可以使用 delayMicroseconds(),它不是用中断实现的。
标签:Arduino
相关阅读 >>
更多相关阅读请进入《Arduino》频道 >>