Arduino系列之中断函数

今天我将简单记录中断函数git

函数分为外部中断和定时中断函数

外部中断的定义:通常由外设发出中断请求,如:键盘中断、打印机中断、外部中断需外部中断源发出中断请求才能发中断。oop

定时中断的定义:是指主程序在运行一段程序事后自动进行的中断服务程序。flash

 

interrupt  能够被中断的代码it

nointerrupt  能够被中断的代码io

外部中断:function

attach interrupt(interrupt,function,mode)请求

1)interrupt:中断号,UNO只用0,1,即表明D2,D3口程序

2)function:调用中断函数,中断发生时调用的函数im

3)mode:中断触发模式

UNO R3支持四种模式

low  当针脚输入为低时,触发中断

change 当针脚输入发生变化时,触发中断

rising 当针脚由低变高时,触发中断

falling 当针脚由高到低时,触发中断

1.中断服务程序不可以有参数和返回值,即void Function name(void){}

2.在中断函数中delay()函数将不起做用

3.在中断函数中millis()函数的值不会增长

4.获得的串行数据将会丢失

5.需在中断函数内部更改的值需声明为volatile类型

detach interrupt (interrupt)

定时中断

常见的定时库有FlexiTimer2.h和Ms.Timer2.h

void start()         开启定时中断

void stop()         关闭定时中断

 

 

#include<MsTimer2.h>

void flash()

{

static boolean cutput=HIGH;

digitalWrite(13,OUTPUT);

OUTPUT=!OUTPUT;

MsTimer2:set(500,flash);

MsTimer2:start();

 

}

void loop()

{}

相关文章
相关标签/搜索