所以我有这个 Arduino 程序,其目标是在 LCD 显示器上显示实时(基于我的计算机)。我已经正确配置了时间和日期,但唯一剩下的问题是我不确定如何让程序正确指示它是 PM 还是 AM。这是到目前为止的代码:
#include <DS1307RTC.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Time.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}
void loop() {
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits2(tm.Hour);
Serial.write(':');
print2digits2(tm.Minute);
Serial.write(':');
print2digits2(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year)-2000);
Serial.println();
lcd.begin(8,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0);
// lcd.print(tm.Hour);
if (tm.Hour>12)
{
tm.Hour = tm.Hour - 12;
print2digits(tm.Hour);
}
else
{
print2digits(tm.Hour);
}
lcd.print(":");
// lcd.print(tm.Minute);
print2digits(tm.Minute);
lcd.print(":");
//lcd.print(tm.Second); // change this text to whatever you like. keep it clean.
print2digits(tm.Second);
lcd.setCursor(0,1); // set cursor to column 0, row 1
lcd.print(tm.Month);
lcd.print("/");
lcd.print(tm.Day);
lcd.print("/");
lcd.print(tmYearToCalendar(tm.Year)-2000);
}
else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
}
else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.print('0');
}
lcd.print(number);
}
void print2digits2(int number) {
if (number >= 0 && number < 10) {
Serial.print('0');
}
Serial.print(number);
}