如何在 C 中为 Arduino 创建中断? [英] How can I create interrupts in C for Arduino?
问题描述
所以我用 C 语言为 Arduino 编写了这段代码.它用于控制步进电机.但是每次我必须等到微控制器开始一个新循环以便它可以获取新变量的值时,我如何创建中断以便它在程序的任何时间执行?
#include #define F_CPU 4000000UL#include #include "IO/ioconfig.h"#include "leebotones/leebotonesA.h"#include "leebotones/leebotonesB.h"#include "rutina/avanza.h"#include "rutina/retrocede.h"字符变量A = 0;字符变量B = 0;int main(void){ioconfig();而(1){如果 (leebotonesA()==1) {变量A++;}//Fin de if leebotonesA.如果 (leebotonesB()==1) {如果(变量A==0){变量B=1;}别的 {变量B=0;}}如果(变量A==2){变量A=0;端口=0x00;_delay_ms(10000);}//Fin de if variableA.if (variableA==1 && variableB==0) {avanza();}//Fin de if leebotonesA.if (variableA==1 && variableB==1) {交还();}_delay_ms(25);}//while结束}//主线结束
当其中一个中断引脚收到状态更改时,Arduino 上会发生硬件中断.如果您有权访问 Arduino 库,则要使用的函数是 attachInterrupt..>
监听中断的示例代码(来自我链接的文档,我添加了注释以帮助解释):
//Arduino 在引脚 13 处配置了一个 LEDINT引脚= 13;//保持 LED 的当前状态以处理切换易失性 int 状态 = 低;无效设置(){pinMode(pin, 输出);//第一个参数://0 引用中断号.在 Duemilanove 上,中断 0//对应数字引脚 2,中断 1 对应数字引脚//3. Duemilanove 只有两个中断引脚,我相信//也是 Uno.//第二个参数://闪烁是检测到中断时要调用的函数的名称//第三个参数://CHANGE 是该引脚上发生的事件.CHANGE 表示引脚//改变的值.还有 LOW、RISING 和 FALLING.附加中断(0,闪烁,改变);}空循环(){//根据状态打开或关闭 LED数字写入(引脚,状态);}无效闪烁(){//切换状态状态 = !状态;}
还有一个每个引脚都支持的引脚变化中断的概念.请参阅中断介绍的底部 了解更多信息.
但是,有时可以通过重构代码来避免硬件中断.例如,让您的 loop() 保持快速运行 --- 主要只是读取输入,限制使用 delay() --- 在循环中,当检测到目标输入值时调用函数.
So I did this code for Arduino in C. It is for controlling a stepper motor. But every time I have to wait until the microcontroller begins a new loop so that it can take the value for the new variables, how can I create an interrupt so it will do it in any time of the program?
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#include "IO/ioconfig.h"
#include "leebotones/leebotonesA.h"
#include "leebotones/leebotonesB.h"
#include "rutina/avanza.h"
#include "rutina/retrocede.h"
char variableA = 0;
char variableB = 0;
int main(void){
ioconfig();
while(1) {
if (leebotonesA()==1) {
variableA++;
} //Fin de if leebotonesA.
if (leebotonesB()==1) {
if (variableA==0) {
variableB=1;
}
else {
variableB=0;
}
}
if (variableA==2) {
variableA=0;
PORTD=0x00;
_delay_ms(10000);
} //Fin de if variableA.
if (variableA==1 && variableB==0) {
avanza();
} //Fin de if leebotonesA.
if (variableA==1 && variableB==1) {
retrocede();
}
_delay_ms(25);
}//End of while
}// End of main
A hardware interrupt on the Arduino occurs when one of the interrupt pins receives a change of state. The function to use, if you have access to the Arduino library, is attachInterrupt.
Example code to listen for an interrupt (derived from the documentation I linked to, I added comments to help explain):
// The Arduino has an LED configured at pin 13
int pin = 13;
// Holds the current state of the LED to handle toggling
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
// First Parameter:
// 0 references the interrupt number. On the Duemilanove, interrupt 0
// corresponds to digital pin 2 and interrupt 1 corresponds to digital pin
// 3. There are only two interrupt pins for the Duemilanove and I believe
// the Uno too.
// Second Parameter:
// blink is the name of the function to call when an interrupt is detected
// Third Parameter:
// CHANGE is the event that occurs on that pin. CHANGE implies the pin
// changed values. There is also LOW, RISING, and FALLING.
attachInterrupt(0, blink, CHANGE);
}
void loop()
{
// Turns the LED on or off depending on the state
digitalWrite(pin, state);
}
void blink()
{
// Toggles the state
state = !state;
}
There is also a concept of Pin Change Interrupts that is supported on every pin. See the bottom part of introduction to interrupts for more info.
However, sometimes a hardware interrupt can be avoided by refactoring your code. For example, keep your loop() running quickly --- mostly just reading inputs, limit the use of delay() --- and in the loop, call a function when the targeted inputted value is detected.
这篇关于如何在 C 中为 Arduino 创建中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!