【问题标题】:LCD screen display random characters液晶屏显示随机字符
【发布时间】:2020-08-10 13:36:06
【问题描述】:

所以我最终尝试使用 4x4 数字键盘、arduino 和螺线管构建一个有趣的安全系统。在尝试让数字键盘和 LCD 一起工作时,我不断遇到问题,原因我不知道。下面的代码是我目前所拥有的:

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
#include <Keypad.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 

//_________________________________________

const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;

char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad

  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
byte colPins [cols] = {5, 6, 7, 8};

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);

//_________________________________________

void setup() { 
 Serial.begin(9600);
 lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } 
}

void loop() { 
  char key = myKeypad.getKey();

  if (key){
    Serial.print(key);
    lcd.print("key has been pressed!");
    delay(2000);
    lcd.clear();
  }
}

我不断收到随机和损坏的字符,但我不明白为什么。有人可以帮我吗?

enter image description here

【问题讨论】:

  • 不要使用引脚 1,因为它是为 Tx/Rx 保留的!除串行通信外,不得用于任何用途。
  • 这绝对是因为您使用Serial.print 功能时的引脚1,并且引脚1 在大多数Arduino 上用作TX。

标签: arduino lcd


【解决方案1】:

您的 LCD 显示器未显示预期的字符串,因为您正在重叠用于其他任务的引脚。
大多数 Arduino 板上的引脚 1 用作串行发送器 (Tx) 引脚。同样的引脚也发生在您的 LCD 显示器引脚之一(rs 引脚)上。这会导致 LCD 上出现意外行为和乱码。

//Pin 1 for LCD
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
...
//Pin 1 is used for Serial Communication Tx to send the data via the port.
Serial.print(key);
...

要使用 Arduino 板正确配置 LCD 显示器,请阅读 Arduino 官方网站上的文档:HelloWorld on LCD

【讨论】:

  • 非常感谢!我知道这是一件很无聊的事情,但我非常感激。再次感谢!
猜你喜欢
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多