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=
库
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);
} 掉电不能保存时间吧? 珜羽 发表于 2012-6-23 14:25 static/image/common/back.gif
掉电不能保存时间吧?
是的~~~~~~~ plantpark 发表于 2012-6-23 19:05 static/image/common/back.gif
是的~~~~~~~
你多加一个EEprom进去就可以解决掉电的问题了! Randy 发表于 2012-6-23 23:18 static/image/common/back.gif
你多加一个EEprom进去就可以解决掉电的问题了!
哈哈~~thinkthink
飞翔的红猪 发表于 2012-6-24 00:00 static/image/common/back.gif
哈哈~~thinkthink
:)不客气了,那天我也整一个完整的出来发上来一起学习一下! 额。真正的时钟芯片也不贵,也就1元,国产的几毛钱。。带后备电池。。。单片机掉电了 这个还能计时。。
eerom是可以保存时间。但是你如果想要保存的时间误差不大的话,就要频繁的写。。。eerom是有寿命的,你还要改进算法,不要写在同一个地方。。。 当前的代码在年份显示一项只显示2010,不知为何。
关键在这句,不明白是哪里设置的问题 :o话说arduino晶振准确度。。。。。一天能跑偏多少时间?我原来尝试过1小时能偏差4-5秒 怎么连线的啊,不管是用弘毅的连线,还是连你调用liquidcrysta库l时用的引脚(12,11,5,4,3,2)(对应的应该是rs ,e,,d0,d1,d2,d3吧),我的液晶屏上都没有显示。菜鸟求指教 而且我的datatime那个包 一运行就说那个错误啊 datetime的那个怎么弄呀 {:soso_e104:}我希望用一块DS1302/1307的,比较实用! 晶振用16M的?自己做板子也可以的哦! 编译过不了...用IDE 1.03/10.1都不行:Q...