Arduino外部中断速度不够快
我一直在试着测量阿杜伊诺号上的线高的时间。它上升,然后在几毫秒内保持高位。然后它被拉低1U,然后浮回高位。我的代码似乎无法识别线路被拉低了。1U对于中断是否太快?我怎样才能让它慢下来 多谢各位 编辑 我的想法是使用RC滤波器和二极管来降低上升时间,使Arduino能够识别变化,但只有在接收线发生变化时。这是否可行?或者我可以用同样的方法使用带有二极管的脉冲扩展器芯片吗Arduino外部中断速度不够快,arduino,interrupt,pwm,Arduino,Interrupt,Pwm,我一直在试着测量阿杜伊诺号上的线高的时间。它上升,然后在几毫秒内保持高位。然后它被拉低1U,然后浮回高位。我的代码似乎无法识别线路被拉低了。1U对于中断是否太快?我怎样才能让它慢下来 多谢各位 编辑 我的想法是使用RC滤波器和二极管来降低上升时间,使Arduino能够识别变化,但只有在接收线发生变化时。这是否可行?或者我可以用同样的方法使用带有二极管的脉冲扩展器芯片吗 #define ESC 2 //the digital pin the esc signal line is attached
#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;
}
}
}
#定义ESC 2//ESC信号线所连接的数字引脚
int throttlePos=0;
易失性无符号长定时器启动;
易失性int上次中断时间;
挥发性内脉冲时间;
无效设置(){
//将安装代码放在此处,以便运行一次:
pinMode(ESC,输出);//最初将ESC的线路设置为输出,以保持线路高,为油门电枢做好准备
digitalWrite(ESC,高);//由于油门脉冲反转,保持脉冲高
Serial.begin(115200);//打开串行端口以便在测试时使用
定时器启动=0;
附件中断(数字输入中断(ESC)、calcSignal、CHANGE);
对于(throttlePos=0;throttlePos1/1us为1MHz)。鉴于Arduino以16MHz执行指令(充其量,某些指令需要更长的时间),您的中断处理程序可能很容易丢失某些内容。但是,即使中断结束前中断条件已清除,中断仍应执行
您是使用Arduino库进行中断,还是直接在寄存器级别配置引脚更改中断
您是否验证了进入Arduino的脉冲是电性的?在不了解更多电路的情况下,很难提出修复建议。我在编辑中为我的答案添加了一张照片。我正在发送一个反向PWM信号。接收器将线路接地1us后返回滴答声。嗯。看起来Arduino应该捕获e该脉冲。它总是失败,还是只是部分时间?它在12个PWM脉冲中下降了11个。这允许进行校准。我将代码添加到编辑中,以查看是否发现任何错误。现在它只是打印出油门脉冲之间信号高的时间。