目录[隐藏]困扰
发现
noInterrupts(); //重要、时间敏感的代码 interrupts(); //其他代码写在这里 经验
中断的本质使用中断Arduino的方式void setup(){ attachInterrupt(0, work, LOW); //中断0,D2低电平 } void loop(){ //do some thing } void work(){ //here there -- 来到了中断 } 中断端口
中断适合做什么
中断不适合做什么
设置调用函数
终止中断
中断类型
中断实验全局中断概念停止所有一切的中断,来处理当前的现场,看起来是这样描述。 使用全局中断禁止cli(); 全局中断开启sei(); 想法
困扰
发现
官方文档attachInterrupt()
Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. The Arduino Uno has :two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). 见识开始 Arduino 之旅! - 第三章 沿途见识[1]那么我们该怎么做才能实现一个中断呢?这非常容易,除了有一些限制外。你只能监控两个端口(对一个普通arduino板)或者在 Arduino mega 上监控六个。此外,你还不能在中断函数中使用 delay() 函数。现在,你需要决定三件事:你需要监控的端口 (数字端口 2 或者 3, 被映射为中断 0 或 1);当中断发生时要执行的函数;以及要监控的端口上触发中断的行为——也就是端口状态变化的情况。你要监控数字端口也需要使用 pinMode();. 函数来设置成输出模式(译者注:可能John在这个地方写错了,需要监听的端口是不需要使用pinMode()函数来指定模式的,这已经在arduino语言中被预定义过,被硬编码为监听模式了)。 /* 例子 3.2 – 中断 创建于 21/04/2010 — 作者 John Boxall — http://tronixstuff. — CC by-sa v3.0 向 LCD 屏幕传送一些信息,同时监听两个中断 */ #include <LiquidCrystal.h> // 需要这个库来控制LCD // 使用指定的端口,初始化LCD函数库 LiquidCrystal lcd(7, 8, 9, 10, 11, 12); float noisy = 0; void setup() { lcd.begin(16, 2); // 需要制定LCD显示屏显示的行数和列数 lcd.setCursor(0,0); lcd.println('* example 3.2 * '); lcd.setCursor(0,1); lcd.println('tronixstuff.com '); lcd.setCursor(0,1); delay(4000); lcd.clear(); randomSeed(analogRead(0)); // 刷新随机数种子 attachInterrupt(0, panicone, RISING); // 当中断 0 (数字端口 2) 的状态发生改变,将触发中断并执行函数 'panicone' attachInterrupt(1, panictwo, RISING); // 当中断 1 (数字端口 3) 的状态发生改变,将触发中断并执行函数 'panictwo' } void loop() { noisy=random(1000); lcd.setCursor(0,0); lcd.print('Random Numbers!'); lcd.setCursor(0,1); lcd.print('Number: '); lcd.print(noisy,0); delay(1000); } void panicone() { lcd.clear(); lcd.println('Interrupt one '); } void panictwo() { lcd.clear(); lcd.println('Interrupt two '); } AVR中断的学习_avr 沿途见识[2] 八、中断标志会一直保持,如果中断被禁止或者是cpu不能马上响应的话。这是称作“挂起”,一旦cpu可以响应了,则马上按优先级处理中断。
引用信息
|
|