Performance Arduino:检测两个信号是否在彼此约100微秒内变化

Performance Arduino:检测两个信号是否在彼此约100微秒内变化,performance,arduino,arduino-uno,Performance,Arduino,Arduino Uno,我试图使用Arduino Uno作为一个更大电路的一部分,该电路同步两个方形电压波形(这些波形被馈入Uno的引脚)。Uno将由来自较大电路另一部分的数字信号激活,该激活信号将一次持续几秒钟。当激活信号很高时,Uno的工作将只是检测其测量的方波何时同步:它将监控它们,并在它们彼此之间约100微秒内到达零交叉点时产生自己的脉冲(方波本身约为50Hz) 因为100us是一个很短的窗口,我想我应该尝试直接位操作,而不是使用DigitalRead()或DigitalWrite()。不过,这是我第一次尝试位

我试图使用Arduino Uno作为一个更大电路的一部分,该电路同步两个方形电压波形(这些波形被馈入Uno的引脚)。Uno将由来自较大电路另一部分的数字信号激活,该激活信号将一次持续几秒钟。当激活信号很高时,Uno的工作将只是检测其测量的方波何时同步:它将监控它们,并在它们彼此之间约100微秒内到达零交叉点时产生自己的脉冲(方波本身约为50Hz)

因为100us是一个很短的窗口,我想我应该尝试直接位操作,而不是使用DigitalRead()或DigitalWrite()。不过,这是我第一次尝试位操作,我在C语言方面的经验一般来说相当有限。我这里有我的代码初稿-如果你们中的任何人能告诉我它的逻辑是否合理,我将非常感谢

它看起来很长,但只有几行的想法,其余的是一堆复制/粘贴的块(这无疑意味着有更优雅的方式来写它)

//同步检测器
//此代码构建为在Arduino Uno上运行。
//硬件定时器以16MHz的频率运行。使用
//在计数器上除以8,每次计数为
//500纳秒,50Hz波周期为40000个时钟周期。
#包括
#包括
无效设置(){
//设置时钟预分频器
//这被设置为“除以8”,但如果我能逃脱
//使用delay语句并不重要

TCCR1B&=~(1好的,所以我不太确定这个特定设备的中断标志到底是什么,但是我个人认为最简单的方法是为每个输入设置2个中断,并设置每个中断代码块,以便通过从n输出引脚。这样,当一个被激活时,如果另一个在时间段内未被激活,则可以重置中断标志,如果第二个被激活,则可以在同步波形时运行希望运行的代码

// Synchronism detector

// This code is built to run on an Arduino Uno.
// The hardware timer runs at 16MHz. Using a
// divide by 8 on the counter each count is 
// 500 ns and a 50Hz wave cycle is 40000 clock cycles.


#include <avr/io.h>
#include <avr/interrupt.h>

void setup(){

  // Set clock prescaler
  // This is set to 'divide by 8' but if I can get away
  // with using delay statements that doesn't really matter
  TCCR1B &= ~(1 << CS12);
  TCCR1B != ~(1 << CS11);
  TCCR1B &= ~(1 << CS10);

  // Set up pins
  // 3rd bit of port D will be the activation flag
  // 4th and 5th bits will be read pins for the incoming waves
  DDRD = DDRD & B11100011
  // Note that I'll also have the incoming waves going into the int0 and int1 pins

  // 6th bit will be a write pin for the outgoing signal
  DDRD = DDRD | B00100000
  PORTD = PORTD & B11011111

  // Set initial values for zero-cross detect flags
  wave_0 = ((PIND & B00001000) >> 3);
  wave_0_prev = ((PIND & B00001000) >> 3);
  wave_1 = ((PIND & B00010000) >> 4);
  wave_1_prev = ((PIND & B00010000) >> 4);
}

void loop(){

  // Poll activation flag
  if (((PIND & B00000100) >> 2) == 1)

    // Poll wave input pins
    wave_0 = ((PIND & B00001000) >> 3);
    wave_1 = ((PIND & B00010000) >> 4);

    // Check zero crossing detection, 
    // Start with wave 0, rising edge
    if (wave_0 == 1 && wave_0_prev == 0))
      attachInterrupt(int1,sync_interrupt, RISING);
      delay(.0001);
      detachInterrupt(int1);

      // Reset pins
      wave_0 = ((PIND & B00001000) >> 3);
      wave_0_prev = ((PIND & B00001000) >> 3);
      wave_1 = ((PIND & B00010000) >> 4);
      wave_1_prev = ((PIND & B00010000) >> 4);
      )      

// Wave 0, falling edge
if (wave_0 == 0 && wave_0_prev == 1))
  attachInterrupt(int1,sync_interrupt, FALLING);
  delay(.0001);
  detachInterrupt(int1);

  // Reset pins
  wave_0 = ((PIND & B00001000) >> 3);
  wave_0_prev = ((PIND & B00001000) >> 3);
  wave_1 = ((PIND & B00010000) >> 4);
  wave_1_prev = ((PIND & B00010000) >> 4);
  )

// Wave 1, rising edge
if (wave_1 == 1 && wave_1_prev == 0))
  attachInterrupt(int0,sync_interrupt, RISING);
  delay(.0001);
  detachInterrupt(int0);

  // Reset pins
  wave_0 = ((PIND & B00001000) >> 3);
  wave_0_prev = ((PIND & B00001000) >> 3);
  wave_1 = ((PIND & B00010000) >> 4);
  wave_1_prev = ((PIND & B00010000) >> 4);
  )

// Wave 1, falling edge
if (wave_1 == 0 && wave_1_prev == 1))
  attachInterrupt(int0,sync_interrupt, FALLING);
  delay(.0001);
  detachInterrupt(int0);

  // Reset pins
  wave_0 = ((PIND & B00001000) >> 3);
  wave_0_prev = ((PIND & B00001000) >> 3);
  wave_1 = ((PIND & B00010000) >> 4);
  wave_1_prev = ((PIND & B00010000) >> 4);
  ) 
}

sync_interrupt(){
  // Set output bit high
  PORTD = PORTD | B00100000
  delay(.001}
  // Set output bit low
  PORTD = PORTD & B00100000

  // Reset pins
  wave_0 = ((PIND & B00001000) >> 3);
  wave_0_prev = ((PIND & B00001000) >> 3);
  wave_1 = ((PIND & B00010000) >> 4);
  wave_1_prev = ((PIND & B00010000) >> 4);
}