Arduino外部中断不够快

时间:2016-02-15 13:40:56

标签: arduino interrupt pwm

我一直试图测量Arduino线路高的时间。它变高,然后保持高电平几毫秒。然后它被拉低1us然后浮回到高位。我的代码似乎没有意识到线被拉低了。 1us对中断来说太快了吗?我怎么能慢下来呢?

谢谢

修改 我的想法是使用RC滤波器和二极管来减慢上升时间,使Arduino能够识别变化,但仅限于接收线发生变化时。这可行吗?或者我可以以相同的方式使用带二极管的脉冲扩展芯片吗?

#define ESC 2 //the digital pin the esc signal line is attached to

int throttlePos = 0;

volatile unsigned long timer_start;
volatile int last_interrupt_time;
volatile int pulse_time;

void setup() {
 // put your setup code here, to run once:
 pinMode(ESC, OUTPUT); //originally set the ESC's line to output to keep line high ready for throttle armature
 digitalWrite(ESC, HIGH); //keep the pulse high due to inverted throttle pulse
 Serial.begin(115200); //opens the serial port for use when testing
 timer_start = 0; 
   attachInterrupt(digitalPinToInterrupt(ESC), calcSignal, CHANGE);
 for(throttlePos = 0; throttlePos <= 1000; throttlePos += 1) //these for loops arm the ESC by emulating an inverted PWM pulse, process takes two seconds
 {
   digitalWrite(ESC, LOW);
   delayMicroseconds(1500);
   digitalWrite(ESC, HIGH);
   delayMicroseconds(100);
 }
 for(throttlePos = 1000; throttlePos <= 2000; throttlePos += 1)
 {
   digitalWrite(ESC, LOW);
   delayMicroseconds(1000);
   digitalWrite(ESC, HIGH);
   delayMicroseconds(100);
 }
}

void loop() {
 digitalWrite(ESC, LOW);
 delayMicroseconds(1200);
 digitalWrite(ESC, HIGH);
 delayMicroseconds(100);
 delay(19);
 Serial.println(pulse_time);
}
void calcSignal() 
{
   //record the interrupt time so that we can tell if the receiver has a signal from the transmitter 
   last_interrupt_time = micros(); 
   //if the pin has gone HIGH, record the microseconds since the Arduino started up 
   if(digitalRead(ESC) == HIGH) 
   { 
       timer_start = micros();
   } 
   //otherwise, the pin has gone LOW 
   else
   { 
       //only worry about this if the timer has actually started
       if(timer_start != 0)
       { 
           //record the pulse time
           pulse_time = ((volatile int)micros() - timer_start);
           //restart the timer
           timer_start = 0;
       }
   } 
}

oscilloscope

1 个答案:

答案 0 :(得分:0)

1 / 1us是1MHz。鉴于您的Arduino以16MHz执行指令(充其量,某些指令需要更长时间),您的中断处理程序很容易丢失。但是,即使在中断结束之前中断条件被清除,中断仍应执行。

您是否正在使用Arduino库进行中断或直接在寄存器级别配置引脚更改中断?

您是否确认进入Arduino的脉冲电声是否正常?如果不了解您的电路,很难提出修复建议。