|
此方法可以不用任何外接时钟芯片,即可实现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=
库
http://www.arduino.cc/playground/uploads/Code/DateTime.zip
代码如下- #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);
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|