【问题标题】:Arduino: detect if 2 signals change within ~100 microseconds of each otherArduino:检测两个信号是否在 100 微秒内发生变化
【发布时间】:2018-11-22 18:51:57
【问题描述】:

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

因为 100us 是一个非常短的窗口,我想我会尝试直接位操作,而不是使用 DigitalRead() 或 DigitalWrite()。不过,这是我第一次尝试位操作,而且我的 C 经验总体上非常有限。我有我的代码的初稿 - 如果你们中的任何人能告诉我它的逻辑是否合理,我将非常感激!

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

// 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);
}

再次感谢您的帮助。

【问题讨论】:

    标签: performance arduino arduino-uno


    【解决方案1】:

    好的,所以我不太确定这个特定设备的中断标志到底是什么,但是我个人认为最简单的方法是让它为每个设备设置 2 个中断输入并设置每个中断代码块以通过从输出引脚跳转来激活定时器中断。这样,当一个被激活时,如果另一个在规定时间内没有被激活,你可以重置中断标志,如果第二个被激活,那么你可以在波同步时运行你希望运行的代码

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多