Arduino 中断


当前第2页 返回上一页

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);
  1. 第一个参数为中断号,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  
    从上表中可以看出同一个 Pin 口在不同的开发板上可能会有不同的中断号,这势必会影响程序的可移植性。幸运的是,Arduino 还提供了另一个函数 digitalPinToInterrupt(int)。从名字就能看出,这个函数能输入 Pin 口号并输出对应的中断号。需要注意的是,输入的 Pin 口号需要在上述的支持列表当中。所以,Arduino 官方推荐我们使用 
    attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); 
    这种方式来注册中断号。
  2. 第二个参数是中断服务例程(ISR)的函数指针,在 C/C++ 中直接写该函数的函数名即可。在触发时,该函数将会被调用。该函数必须没有任何的参数也没有任何的返回值。
  3. 第三个参数是中断触发条件,由几个可选的值:
    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 中使用中断需要注意的问题

  1. 由于中断会打断正常代码的运行,因此 ISR 的应该尽可能快地执行完毕。
  2. 在 ISR 中修改的全局变量要用 volatile 修饰符修饰以防止编译器优化
  3. 在 ISR 中不能使用其他用中断实现的函数,如 millis() delay() 等。延时可以使用  delayMicroseconds(),它不是用中断实现的。






标签:Arduino

返回前面的内容

相关阅读 >>

Arduino 步进电机

Arduino 伺服电机

Arduino 内部集成电路

Arduino 读取模拟电压

Arduino 串行外设接口

Arduino 闪烁led

Arduino due 和 zero

Arduino 水位检测器传感器

Arduino 数学库

Arduino 中断

更多相关阅读请进入《Arduino》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...