【问题标题】:Arduino 16*2 LCD displaying random characters instead of expected textArduino 16*2 LCD 显示随机字符而不是预期文本
【发布时间】:2021-02-05 11:36:00
【问题描述】:

我正在尝试向我的 16*2 LCD 模块显示一些数据,但显示了一些随机字符。我有一些简单的代码用于测试我的 LCD 显示器,它运行良好。代码:

#include<LiquidCrystal.h>


// initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

void setup() {
  lcd.begin(16, 2);
}

void loop() {
  lcd.print("Testing");   // thats the top row string
  delay(1800);

  lcd.setCursor(2, 1);   // move to the 2nd row, 1st col
  lcd.print("Display this!");
  delay(1800);
  lcd.clear();

  lcd.setCursor(7, 1);
  delay(400);
  lcd.blink();
  lcd.setCursor(6, 1);
  delay(400);
  lcd.setCursor(5, 1);
  delay(400);
  lcd.setCursor(4, 1);
  delay(400);
  lcd.setCursor(3, 1);
  delay(400);
  lcd.setCursor(2, 1);
  delay(400);
  lcd.setCursor(1, 1);
  delay(400);
  lcd.setCursor(0, 1);
  lcd.noBlink();
  lcd.print("Silly Isn't It?");

  lcd.cursor();
  delay(1800);
  lcd.noCursor();

  lcd.clear();
}

不过,我现在在面包板上有更多东西 - LCD、微型 SD 读卡器、电位计和 LM35 温度传感器 这是我的代码:

#include<LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>

////////// LCD
//initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);


const int CS_PIN = 10;
const int POW_PIN = 8;
int refreshRate = 2000;       // for reading vals

////////// LEDs
int ledPinR = 11;
int ledPinG = 12;
int ledPinY = 13;

////////// LM35
float temp;
int tempPin = A0;

void setup() {

  ////////// LED
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinY, OUTPUT);

  ////////// LCD
  lcd.begin(16, 2);
  lcd.print("please wait...");   //thats the top row string
  delay(2000);
  lcd.clear();
  lcd.blink();


  ////////// SD
  Serial.begin(9600);
  Serial.println("\nNow Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);
  pinMode(POW_PIN, OUTPUT);
  digitalWrite(POW_PIN, HIGH);

  if(!SD.begin(CS_PIN)){
    Serial.println("\nSomething went wrong. Probably card failure, card format, or something else.");
    return;
  }

  Serial.println("\nCard ready!");
  File commandFile =  SD.open("tempLevels.txt");

  if(commandFile){
    Serial.println("\nNow Reading Command File...");

    while(commandFile.available())
    {
      refreshRate = commandFile.parseInt();
    }

    Serial.print("\nTapiwa, the refresh rate is: ");
    Serial.print(refreshRate);
    Serial.print(" ms");

    commandFile.close();
  }

  else{
    Serial.println("Oops! Failing to read command file!");
    return;
  }
}

void loop() {

  ////////// LM35
  temp = analogRead(tempPin);
  float mV = (temp / 1024.0) * 5000;
  float tempVal = mV / 10;

  Serial.println("\nTemperature is: ");
  Serial.println(tempVal);  

  File dataFile =  SD.open("log.csv", FILE_WRITE);          // dont know about that .csv format

  if(dataFile)
  {
    dataFile.print("\nTemperature is: ");
    dataFile.print(tempVal);
    dataFile.println("Deg");
    dataFile.close();

    Serial.println("\nSaved in DataFile >> Temperature is: ");
    Serial.print(tempVal);
  }

  else
  {
    Serial.println("DataFile error! Reading not saved");
    Serial.println("Could not open log file! Not on SD card!");
  }

  lcd.print("Temp: ");
  lcd.setCursor(2, 1);   // 2nd row, 1st col
  lcd.print(tempVal);
  delay(2000);

  lcd.clear();
  delay(refreshRate);
}

我在串行监视器中得到结果,但 LCD 显示类似于加密文本的随机字符。我哪里做错了? 我查看了该站点和其他站点上的多个帖子,但它们并没有那么有用:

This one made sense but not useful in my case.

This one too!.

And this one

【问题讨论】:

    标签: arduino


    【解决方案1】:

    如果您查看Serial 的文档,它会说:

    所有 Arduino 板都至少有一个串行端口(也称为 UART 或 USART):Serial。它通过数字引脚 0 (RX) 和 1 (TX) 以及通过 USB 与计算机进行通信。 因此,如果您使用这些功能,则不能同时使用引脚 0 和 1 进行数字输入或输出

    因此,您应该重新安排您的方案,以便 LCD 不使用引脚 1。

    【讨论】:

    • 事情就这样发生了。非常感谢!从引脚 1 更改为引脚 3,因此更改为: ////////// LCD //初始化引脚 - RS、E、其余数据引脚 LiquidCrystal lcd(3, 2, 4, 5, 6, 7 );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多