极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12715|回复: 2

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

[复制链接]
发表于 2013-6-24 13:32:54 | 显示全部楼层 |阅读模式

setup中这么写的

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

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

while(ret_step!=1)  rst();

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

使用道具 举报

发表于 2013-6-24 22:11:56 | 显示全部楼层
本帖最后由 smching 于 2013-6-24 22:22 编辑

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

  1. int servo1 = 2;
  2. unsigned long servo1_startPulse;
  3. volatile unsigned servo1_val;
  4. int servo1_Ready;

  5. void setup() {

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

  8. }


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

  10.   servo1_startPulse = micros();     // record microseconds() value as servo1_startPulse
  11.   
  12.   detachInterrupt(0);  // after recording the value, detach the interrupt from rc1_begin
  13.   
  14.   attachInterrupt(0, rc1_end, FALLING); // re-attach the interrupt as rc1_end, so we can record the value when it goes low
  15.   
  16. }

  17. void rc1_end() {

  18. servo1_val = micros() - servo1_startPulse;  // when interrupt pin goes LOW, record the total pulse length by subtracting previous start value from current micros() vlaue.
  19.   
  20. detachInterrupt(0);  // detach and get ready to go HIGH again
  21.   
  22. attachInterrupt(0, rc1_begin, RISING);

  23. 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.
  24.   servo1_Ready = false;
  25. }
  26. else {
  27.   servo1_Ready = true; // if not, don't pass the value to be processed
  28. }
  29. }

  30. void loop() {

  31.     Serial.print("ch1:  ");
  32.     Serial.print(servo1_val);
  33.     Serial.print("  ");

  34. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-7-10 12:02:03 | 显示全部楼层
smching 发表于 2013-6-24 22:11
请参考http://forum.arduino.cc/index.php/topic,43062.0.html

int servo1 = 2;

谢谢,弄好了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-6-4 01:34 , Processed in 0.047009 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表