我在为 arudino 运行 lcd 代码时遇到问题。代码很好,但如果我尝试在代码上启动串行开始,它将无法工作

问题描述 投票:0回答:1

简而言之,代码写得不好,因为我计划将其作为计算器的基础,但现在只是一个计时器,所以这可能是问题所在,但一切正常,直到我尝试与串行建立连接监控“Serial.begin(9600)”

没有串行线代码工作正常,如果你把它,液晶显示器不会打开。 (contrast 和 backlight 是开着的,但是不受代码控制

代码:

#include <Keypad.h>
#include <LiquidCrystal.h>

// This defines the LCD wiring to the DIGITALpins
const int rs = 0, en = 1, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Digital LCD Constrast setting
int cs = 6;  // pin 9 for contrast PWM
const int contrast = 100;  // default contrast

// Time Set Buttons REMOVE LATER
int button1;
int button2;

// Pins definition for Time Set Buttons
int hs=0;// pin 0 for Hours Setting
int ms=2;// pin 1 for Minutes Setting


// initial Time display is 12:59:45 PM
int h = 7;
int m = 6;
int s = 45;
int flag = 1;  //PM

// Keypad
const byte ROWS = 4; // number of rows
const byte COLS = 3; // number of columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; // row pinouts of the keypad R1 = D8, R2 = D7, R3 = D6, R4 = D5
byte colPins[COLS] = {4, 3, 2};    // column pinouts of the keypad C1 = D4, C2 = D3, C3 = D2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Backlight Time Out 
const int Time_light = 150;
int bl_TO = Time_light;  // Backlight Time-Out
int bl = 9;  // Backlight pin
const int backlight = 120;  // no more than 7mA !!!

// For accurate Time reading, use Arduino Real Time Clock and not just delay()
static uint32_t last_time, now = 0;  // RTC

void setup() { 
  lcd.begin(16, 2);
  pinMode(hs,INPUT_PULLUP);// avoid external Pullup resistors for Button 1 REMOVE LATER
  pinMode(ms,INPUT_PULLUP);// and Button 2 REMOvE LATER
  analogWrite(cs, contrast);  // Adjust Contrast VO
  analogWrite(bl, backlight);  // Turn on Backlight
  now = millis();  // read RTC initial value
  Serial.begin(9600);
}

void loop()
{ 

  lcd.begin(16,2);// every second
// Update LCD Display
// Print TIME in Hour, Min, Sec + AM/PM
 lcd.setCursor(0,0);
 lcd.print("Time ");
 if(h<10)lcd.print("0");// always 2 digits
 lcd.print(h);
 lcd.print(":");
 if(m<10)lcd.print("0");
 lcd.print(m);
 lcd.print(":");
 if(s<10)lcd.print("0");
 lcd.print(s);

 if(flag==0) lcd.print(" AM");
 if(flag==1) lcd.print(" PM");
 
 lcd.setCursor(0,1);// for Line 2
 lcd.print("Precision clock");


// improved replacement of delay(1000) 
// Much better accuracy, no more dependant of loop execution time

for ( int i=0 ;i<5 ;i++)// make 5 time 200ms loop, for faster Button response
{

  while ((now-last_time)<200) //delay200ms
  { 
    now=millis();
  }
 // inner 200ms loop
 last_time=now; // prepare for next loop 

 // read Setting Buttons
 button1=digitalRead(hs);// Read Buttons
 button2=digitalRead(ms);

 //Backlight time out 
 bl_TO--;
 if(bl_TO==0)
 {
  analogWrite(bl,0);// Backlight OFF
  bl_TO++;
 }
 
 // Hit any to activate Backlight 
 if(  ((button1==0)|(button2==0)) & (bl_TO==1)  )
 {
  bl_TO=Time_light;
  analogWrite(bl,backlight);
  // wait until Button released
  while ((button1==0)|(button2==0))
  {
   button1=digitalRead(hs);// Read Buttons
   button2=digitalRead(ms);
  }
 }
 else
 // Process Button 1 or Button 2 when hit while Backlight on 
 {
  if(button1==0){
   h=h+1;
   bl_TO=Time_light;
   analogWrite(bl,backlight);
  }

 if(button2==0){
  s=0;
  m=m+1;
  bl_TO=Time_light;
  analogWrite(bl,backlight);
  }

/* ---- manage seconds, minutes, hours am/pm overflow ----*/
 if(s==60){
  s=0;
  m=m+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
 }
 if(h==13)
 {
  h=1;
  flag=flag+1;
  if(flag==2)flag=0;
 }


 if((button1==0)|(button2==0))// Update display if time set button pressed
 {
  // Update LCD Display
  // Print TIME in Hour, Min, Sec + AM/PM
  lcd.setCursor(0,0);
  lcd.print("Time ");
  if(h<10)lcd.print("0");// always 2 digits
  lcd.print(h);
  lcd.print(":");
  if(m<10)lcd.print("0");
  lcd.print(m);
  lcd.print(":");
  if(s<10)lcd.print("0");
  lcd.print(s);

  if(flag==0) lcd.print(" AM");
  if(flag==1) lcd.print(" PM");
 
  lcd.setCursor(0,1);// for Line 2
  lcd.print("Precision clock");
 }


 } // end if else
}// end for



// outer 1000ms loop

 s=s+1; //increment sec. counting
    
    
// ---- manage seconds, minutes, hours am/pm overflow ----
 if(s==60){
  s=0;
  m=m+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
 }
 if(h==13)
 {
  h=1;
  flag=flag+1;
  if(flag==2)flag=0;
 } 
 

 
// Loop end
}
c++ arduino-uno
1个回答
0
投票

Arduino Uno 使用引脚 0 和 1 进行串口。根据参考

在 Uno、Nano、Mini 和 Mega 上,引脚 0 和 1 用于与计算机通信。将任何东西连接到这些引脚都可能干扰该通信,包括导致上传到电路板失败。

现在,您正在将这些引脚用于 LCD:

// This defines the LCD wiring to the DIGITALpins
const int rs = 0, en = 1, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

尝试为

rs
en
连接使用不同的引脚。

© www.soinside.com 2019 - 2024. All rights reserved.