2

我正在尝试编写一个从 i2c 接收字符串数据并将其显示在 LCD 上的程序。第一次将数据接收到 arduino 时,它会渲染它,但是随后的 i2c 有效负载将被忽略。我的 onReceive 功能在液晶显示器的第二行有一个状态行显示,它显示seconds()来自定时器芯片的字段。秒数似乎没有增加。但是,在 loop() 中呈现的每秒点闪烁确实会继续闪烁,因此 mcu 不会冻结。

#include <LiquidCrystal.h>    
#include <Wire.h>    
#include <Time.h>    
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();                  // start with a blank screen

}

void loop()
{
  lcd.setCursor(15,1);
  if (second() % 2 == 0)
    lcd.write(".");
    else
    lcd.write(" ");


  delay(100);

}

void receiveEvent(int howMany)
{

  //char buf[howMany];
  int i=0;
  char output[16];

  lcd.clear();
  while(Wire.available())
  {
    char c = Wire.read(); // receive byte as a character
    lcd.setCursor(i,0);
    lcd.write(c);
    i++;
    //buf[i++]=c;
    //buf[i+1]=0;
  }
  lcd.setCursor(0,1);
  sprintf(output,"s%dNB%dI%d",second(),howMany,i);
  lcd.write(output);

}
4

1 回答 1

0

你的 Arduino 可能被困在这里:

while(Wire.available())
 {
  //...

利用:

if(Wire.available() > 0) {
 //stuff
}

反而。

于 2013-08-06T22:10:20.010 回答