拾荒皇子 发表于 2013-6-24 13:32:54

noInterrupts()和interrupts()这两个函数的用法,谢谢


setup中这么写的

pinMode(2,INPUT);
pinMode(13,OUTPUT);
attachInterrupt(0,counter,RISING);

程序首先要执行一段初始化代码,这个时候要忽略中断,请问,该怎么用啊?谢谢,我是这么写的,但是没用
void loop()
{
noInterrupts();

while(ret_step!=1)rst();

}
就是执行while语句的时候,要忽略中断,该怎么写?谢谢

smching 发表于 2013-6-24 22:11:56

本帖最后由 smching 于 2013-6-24 22:22 编辑

请参考http://forum.arduino.cc/index.php/topic,43062.0.html

int servo1 = 2;
unsigned long servo1_startPulse;
volatile unsigned servo1_val;
int servo1_Ready;

void setup() {

pinMode(servo1, INPUT);
attachInterrupt(0, rc1_begin, RISING);    // catch interrupt 0 (digital pin 2) going HIGH and send to rc1()

}


void rc1_begin() {         // enter rc1_begin when interrupt pin goes HIGH.

servo1_startPulse = micros();   // record microseconds() value as servo1_startPulse

detachInterrupt(0);// after recording the value, detach the interrupt from rc1_begin

attachInterrupt(0, rc1_end, FALLING); // re-attach the interrupt as rc1_end, so we can record the value when it goes low

}

void rc1_end() {

servo1_val = micros() - servo1_startPulse;// when interrupt pin goes LOW, record the total pulse length by subtracting previous start value from current micros() vlaue.

detachInterrupt(0);// detach and get ready to go HIGH again

attachInterrupt(0, rc1_begin, RISING);

if (servo1_val < 150 || servo1_val > 600) {// only set the servo1_Ready flag if the value is a valid Servo pulse between 600-2400 microseconds.
servo1_Ready = false;
}
else {
servo1_Ready = true; // if not, don't pass the value to be processed
}
}

void loop() {

    Serial.print("ch1:");
    Serial.print(servo1_val);
    Serial.print("");

}

拾荒皇子 发表于 2013-7-10 12:02:03

smching 发表于 2013-6-24 22:11 static/image/common/back.gif
请参考http://forum.arduino.cc/index.php/topic,43062.0.html

int servo1 = 2;


谢谢,弄好了
页: [1]
查看完整版本: noInterrupts()和interrupts()这两个函数的用法,谢谢