Adafruit_NeoPixel 和串口通信

Adafruit_NeoPixel and serial communication

我使用 https://github.com/adafruit/Adafruit_NeoPixel 中的 Adafruit_NeoPixel 我有关于与我的板的串行通信的问题,但是当我不使用 pixels.show(); 时它工作正常;我不知道为什么,但任何人都可以解释这个,以及如何解决这个问题。(我也必须刷新 led 加班) *对不起我的英语

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN            6
#define NUMPIXELS      16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500;
void setup() {

#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif

  pixels.begin();
  Serial.begin(115200);
}

void loop() {
  for(int i=0;i<NUMPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,150,0));
    pixels.show();
  }
}

void serialEvent() {
  if(Serial.available()){
    delay(5);
    size_t len = Serial.available();
    char rcvData[128];
    Serial.readBytes( rcvData, len );
    rcvData[len] = '[=11=]';

    Serial.write(rcvData);
    Serial.write('\n');
  }
}

串行输入

123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs

串行输出

12679
12349
123459
12456789
12569
126789
12389
12389
12389
abdefklqrs
abfghijklmnopqrs
abghmnos
abcdijkpqs
abfglmns
abcdijopqs
abefglmnrs

嗯,如果你读了Adafruit_NeoPixel::show部分,那是因为关键的时机。

note: Data latch = 50+ microsecond pause in the output stream. Rather than put a delay at the end of the function, the ending time is noted and the function will simply hold off (if needed) on issuing the subsequent round of data until the latch time has elapsed. This allows the mainline code to start generating the next frame of data rather than stalling for the latch.

while(!canShow()); endTime is a private member (rather than global var) so that mutliple
instances on different pins can be quickly issued in succession (each instance doesn't delay the next). In order to make this code runtime-configurable to work with any pin, SBI/CBI instructions are eschewed in favor of full PORT writes via the OUT or ST instructions. It relies on two facts: that peripheral functions (such as PWM) take precedence on output pins, so our PORT- wide writes won't interfere, and that interrupts are globally disabled while data is being issued to the LEDs, so no other code will be accessing the PORT. The code takes an initial 'snapshot' of the PORT state, computes 'pin high' and 'pin low' values, and writes these back to the PORT register as needed.

noInterrupts(); Need 100% focus on instruction timing

希望这能回答您的问题