arduino教程系列P-1 arduino lcd1602电子时钟

此方法可以不用任何外接时钟芯片,即可实现1602显示当前年月日时间等信息。

但是如何通过加入按钮实现时间调节尚未实现。当前的代码在年份显示一项只显示2010,不知为何。

板子

http://arduino.cc/en/Main/arduinoBoardDuemilanove

lcd 1602 接线参照弘毅的教程

http://www.geek-workshop.com/forum.php?mod=viewthread&tid=78&reltid=955&pre_thread_id=0&pre_pos=1&ext=


2012-06-23_13-38-26_221 (640x347).jpg

2012-6-23 13:58 上传
(167.29 KB)


2012-06-23_13-38-40_477 (640x281).jpg

2012-6-23 13:58 上传
(147.61 KB)





http://www.arduino.cc/playground/uploads/Code/DateTime.zip

代码如下ARDUINO 代码复制打印

#include <LiquidCrystal.h>#include <DateTime.h>#include <DateTimeStrings.h>#define dt_SHORT_DAY_STRINGS#define dt_SHORT_MONTH_STRINGS// simple sketch to display a digital clock on an LCD// see the LiquidCrystal documentation for more info on thisLiquidCrystal lcd(12, 11, 5, 4, 3, 2);int backLight = 13; // pin 13 will control the backlightvoid setup(){pinMode(backLight, OUTPUT);digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.DateTime.sync(DateTime.makeTime(30, 41, 12, 23, 06, 2012)); // sec, min, hour, date, month, year // Replace this with the most current time}void loop(){ if(DateTime.available()) {unsigned long prevtime = DateTime.now();while( prevtime == DateTime.now() ) // wait for the second to rollover;DateTime.available(); //refresh the Date and time propertiesdigitalClockDisplay( ); // update digital clock}}void printDigits(byte digits){// utility function for digital clock display: prints preceding colon and leading 0lcd.print(":");if(digits < 10)lcd.print('0');lcd.print(digits,DEC);}void digitalClockDisplay(){lcd.clear();lcd.begin(16,2);lcd.setCursor(3,0);//lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));if(DateTime.Day <10)lcd.print('0');lcd.print(DateTime.Day,DEC);lcd.print("/");//lcd.print(DateTimeStrings.monthStr(DateTime.Month));if(DateTime.Month <10)lcd.print('0');lcd.print(DateTime.Month,DEC);lcd.print("/");lcd.print((DateTime.Year,DEC)+2000);//lcd.print(" ");if(DateTime.Hour <10)lcd.setCursor(5,1);lcd.setCursor(4,1);// digital clock display of current timelcd.print(DateTime.Hour,DEC);printDigits(DateTime.Minute);printDigits(DateTime.Second);}
#include <LiquidCrystal.h>

#include <DateTime.h>

#include <DateTimeStrings.h>



#define dt_SHORT_DAY_STRINGS

#define dt_SHORT_MONTH_STRINGS



// simple sketch to display a digital clock on an LCD

// see the LiquidCrystal documentation for more info on this



LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int backLight = 13; // pin 13 will control the backlight



void setup(){

pinMode(backLight, OUTPUT);

digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.



DateTime.sync(DateTime.makeTime(30, 41, 12, 23, 06, 2012)); // sec, min, hour, date, month, year // Replace this with the most current time

}



void loop(){

if(DateTime.available()) {

unsigned long prevtime = DateTime.now();

while( prevtime == DateTime.now() ) // wait for the second to rollover

;



DateTime.available(); //refresh the Date and time properties

digitalClockDisplay( ); // update digital clock

}

}



void printDigits(byte digits){

// utility function for digital clock display: prints preceding colon and leading 0

lcd.print(":");

if(digits < 10)

lcd.print('0');

lcd.print(digits,DEC);

}







void digitalClockDisplay(){

lcd.clear();

lcd.begin(16,2);

lcd.setCursor(3,0);



//lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));

if(DateTime.Day <10)

lcd.print('0');

lcd.print(DateTime.Day,DEC);

lcd.print("/");



//lcd.print(DateTimeStrings.monthStr(DateTime.Month));

if(DateTime.Month <10)

lcd.print('0');

lcd.print(DateTime.Month,DEC);

lcd.print("/");

lcd.print((DateTime.Year,DEC)+2000);



//lcd.print(" ");

if(DateTime.Hour <10)

lcd.setCursor(5,1);

lcd.setCursor(4,1);



// digital clock display of current time

lcd.print(DateTime.Hour,DEC);

printDigits(DateTime.Minute);

printDigits(DateTime.Second);

}


via - 极客工坊

标签: Arduino教程