20x4 lcd with arduino and xbee



>im 使用连接到 20x4 LCD 和 xBee 的 Arduino Mega。LCD是一个I2C接口。我使用以下代码将 xbee 接收的数据写入 LCD 上。

/*****************************************************************
XBee_Serial_Passthrough.ino
Set up a software serial port to pass data between an XBee Shield
and the serial monitor.
Hardware Hookup:
  The XBee Shield makes all of the connections you'll need
  between Arduino and XBee. If you have the shield make
  sure the SWITCH IS IN THE "DLINE" POSITION. That will connect
  the XBee's DOUT and DIN pins to Arduino pins 2 and 3.
*****************************************************************/
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LCD03.h>
 LCD03 lcd;
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(10, 11); // RX, TX
void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
    // Initialise a 20x4 LCD
  lcd.begin(20, 4);
  // Turn on the backlight
  lcd.backlight();
  // Write to the LCD
  lcd.print("Hello world");
  // Wait for 5 seconds
  delay(5000);
  // Clear the LCD
  lcd.clear();
}
void loop()
{
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
   lcd.write(XBee.read());
  }
}

但是,它在LCD上显示黑框而不是文字。如果我使用 lcd.print("测试");它显示"文本",这意味着LCD正在接收从XBEE发送的数据,但我无法使用LCD.print,因为接收的数据是随机的。此外,我如何在每个单词后清除屏幕,因为所有单词都在一行中。

我认为

这个问题发生有两个原因:-

  1. 数据可能在传输时损坏,因此可能必须实现一些错误检查方法(例如:校验和),如果是这种情况,请再次请求数据。
  2. 您必须检查从发射器模块传输的数据的格式。我的意思是LCD希望数据采用ASCII格式,与从xbee收到的格式相同。

基本上,您必须在多个点检查数据,并找出问题的确切位置。既然你说写作

 lcd.print("test");

确实可以正常工作,因此我相信I2C设置正确,因此上述问题是我唯一能想到的。

方法:-为什么不在arduino的串行监视器上显示xbee接收的串行数据,并检查接收的数据是否正确。

最新更新