如何创建C语言为Arduino的中断? [英] How can I create interrupts in C for Arduino?
问题描述
于是我在C.这样做code为Arduino的,是用于控制步进电机。但每次我不得不等待,直到单片机开始一个新的循环,这样就可以把价值,为新的变量的时候,我怎么能产生中断,因此会做在程序中的任何时候?
的#include< AVR / io.h>
#定义F_CPU 4000000UL
#包括LT&; UTIL / delay.h>#包括IO / ioconfig.h
#包括leebotones / leebotonesA.h
#包括leebotones / leebotonesB.h
#包括若缇娜/ avanza.h
#包括若缇娜/ retrocede.h焦炭variableA的= 0;
焦炭是variableB = 0;诠释主要(无效){
IOconfig进行();
而(1){
如果(leebotonesA()== 1){
variableA的++;
} //翅德如果leebotonesA。 如果(leebotonesB()== 1){
如果(variableA的== 0){
是variableB = 1;
}
其他{
是variableB = 0;
}
} 如果(variableA的== 2){
variableA的= 0;
PORTD = 0×00;
_delay_ms(10000);
} //翅德如果variableA的。 如果(variableA的== 1安培;&安培; variableB的== 0){
AVANZA();
} //翅德如果leebotonesA。 如果(variableA的== 1和;&放大器; variableB的== 1){
交还();
}
_delay_ms(25);
} //而结束
} //主结束
在中断引脚中的一个接收状态的变化发生在Arduino的硬件中断。该功能的使用,如果你有机会到Arduino库,是 attachInterrupt 。
举例code监听中断(从我联系到的文件导出,我添加了注释,以帮助解释):
// Arduino的具有13引脚配置的LED
INT引脚= 13;
//保存的LED来处理切换的当前状态
挥发性INT状态=低;无效设置()
{
pinMode(针,OUTPUT);
//第一个参数:
// 0引用中断号。在Duemilanove,中断0
//对应数字引脚2和中断1对应数字引脚
// 3.只有两个为Duemilanove中断引脚,我相信
//欧诺了。
//第二个参数:
//眨眼是要调用的函数的名字检测到中断时,
//第三个参数:
//变化是,该引脚上发生该事件。 CHANGE意味着针
//改变值。也有低,上升,下滑。
attachInterrupt(0,闪烁,变化);
}无效循环()
{
//打开或关闭取决于状态的LED
digitalWrite(销状态);
}无效眨眼()
{
//切换状态
!=状态状态;
}
还有就是支持每个引脚上引脚电平变化中断的概念。看到的底部<一href=\"http://forums.trossenrobotics.com/tutorials/how-to-diy-128/an-introduction-to-interrupts-3248/\"相对=nofollow>介绍中断获取更多信息。
不过,有时一个硬件中断,可以通过重构你的code避免。例如,让您的循环()快速运行---大多只是阅读的投入,限制使用延迟()的---而在循环中,检测到有针对性的输入值时调用的函数。
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屋!