Serial.print()填充在Arduino的Timer1 ISR中的数组

时间:2019-02-20 22:15:30

标签: timer arduino isr

我学习了如何在Arduino Uno中使用Timer1中断。我写了这段代码,每五秒钟将0到49的数字打印到Serial Monitor上,就可以实现我想要的代码:

#include <TimerOne.h>

#define TIMER_US 100000

const int payload_length = 50;
volatile int payload_buffer[payload_length] = {0};
volatile int payload_capacity = -1;


void setup() {

  Timer1.initialize(TIMER_US);                  // Initialise timer 1
  Timer1.attachInterrupt( timerIsr );           // attach the ISR routine here
  Serial.begin(115200);
  Serial.println(0);
}

void loop() {

  if(payload_capacity+1 == payload_length)
  {

    int output_buffer[payload_length] = {0};
    int *src = payload_buffer;
    int *dst = output_buffer;
    noInterrupts();
    memcpy(dst, src, sizeof(payload_buffer));
    payload_capacity = -1;
    interrupts();
    for(int i=0; i<payload_length; i++)
    {
      int audio_signal = output_buffer[i];

      Serial.print(audio_signal);
      Serial.print(" ");
      //radio.write( &audio_signal, sizeof(int) ));

    }
    Serial.println(" ");
  }
}

void timerIsr()
{
  if(payload_capacity+1 < payload_length )
  {
    payload_capacity++;
    payload_buffer[payload_capacity] = payload_capacity;
  }
}

但是,当我将payload_length更改为值512时,串行监视器仅每五十秒打印一次数字0。相反,我期望串行监视器显示数字0到511。为什么没有发生这种情况?

我尝试将TIMER_US缩短为100微秒,但是当payload_length为50时,串行监视器仍然可以打印0到49,但是只有{{1} }是512。

我是否误解了中断如何与其他变量或串行打印命令一起工作?

注意:这是一种方法的一部分,我希望使用ISR从payload_length采样模拟值并将这些值保存到数组中。阵列装满后,我计划使用nrf24l01传输到另一台设备。

0 个答案:

没有答案