外部中断在不同的Arduino型号上位置也不同,只有外部中断发生在以下端口,Arduino才能捕获到,以下例举了常见的几种型号的外部中断引脚标号。
型号 | int.0 | int.1 | int.2 | int.3 | int.4 | int.5 | UNO\Ethernet | 2 | 3 | | | | | Mega2560 | 2 | 3 | 21 | 20 | 19 | 18 | Leonardo | 3 | 2 | 0 | 1 | | | Due | 所有IO口均可
| 中断模式就是你的中断触发方式。在大多数arduino上有以下四种触发方式:
LOW 低电平触发
CHANGE 电平变化,高电平变低电平、低电平变高电平
RISING 上升沿触发
FALLING 下降沿触发
HIGH 高电平触发(该中断模式仅适用于Arduino due)
在定义中断函数后,要使用外部中断,你只需要在程序的Setup部分配置好中断函数即可,配置函数如下:
attachInterrupt(interrupt, function, mode);//interrupt为你中断通道编号,function为中断函数,mode为中断触发模式
需要注意的是在Arduino Due中,中断设置有点不同:
attachInterrupt(pin, function, mode); //due 的每个IO均可以进行外部中断,所以这里第一个参数为pin,即你使用的引脚编号。
如果在程序中途,你不需要使用外部中断了,你可以用中断分离函数detachInterrupt(interrupt );来取消这一中断设置。
同样在Arduino Due上,该函数为detachInterrupt(interrupt );。
|