2016-09-23 4 views
0

그래서이 목표는 내 컴퓨터를 기반으로 실시간을 LCD 디스플레이에 표시하는 Arduino 프로그램입니다. 나는 시간과 날짜를 적절히 설정했으나 남은 유일한 문제는 프로그램이 PM인지 오전인지를 올바르게 알려주는 방법이 확실하지 않다는 것입니다. 지금까지 코드는 다음과 같습니다.RTC에 대한 LCD 디스플레이에 AM/PM을 나타냅니다.

#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); 

} 

답변

0

정확한 오전과 오후인지 언제 알 수 있습니다. 그러니 나중에 그것을 유지 : 나중에

bool pm = false; 
if (tm.Hour>12) 
{ 
    tm.Hour = tm.Hour - 12; 
    print2digits(tm.Hour); 
    pm = true; 
} 
else 
{ 
    print2digits(tm.Hour); 
} 

과 :

lcd.print(pm ? " PM" : " AM"); // print PM if pm is set to true, otherwise "AM"