本帖最后由 迷你强 于 2015-4-1 11:12 编辑
连发第二弹啦,用I2C屏幕,显示时间,之前的内容看前篇帖子
传送门
效果图如上图,其实非常简单,都是I2C总线,所有线都是并联的。
需要一个新的库,就是I2C液晶屏的库
点击下载
注意,此库在arduino下有一个同名的库文件,建议删除原来的,再将此文件复制进去,直接替换可能会出问题。
代码如下,因为前篇已经给模块配置好时间了。这个时间会一直走,直到电池耗尽,不过这颗电池供电个1-2年肯定问题不大。
- #include "LiquidCrystal.h"
- #include <Wire.h>
- #include <Rtc_Pcf8563.h>
-
- byte year, month, day,Weekday, hour, minute, second; //初始化一些变量
-
- Rtc_Pcf8563 rtc;
- LiquidCrystal lcd(0); //初始化液晶屏
- void setup()
- {
- lcd.begin(16, 2); //定义屏幕的种类
-
-
- }
-
- void loop()
- {
- rtc.getDateTime(); //从时钟读取时间
- year = rtc.getYear(); //读取年
- month = rtc.getMonth(); //月
- day = rtc.getDay(); //日
- Weekday = rtc.getWeekday(); //星期
- hour = rtc.getHour(); //小时
- minute = rtc.getMinute(); //分钟
- second = rtc.getSecond(); //秒
- if( second ==0) //判断下,如果值是0就刷新下屏幕,避免字符显示混乱
- {
- lcd.clear();
- }
-
- lcd.setCursor(2, 0); //第一行 从第三字符开始
- lcd.print("20");
- lcd.print(year);
- lcd.print("-");
- lcd.print(month);
- lcd.print("-");
- lcd.print(day);
- lcd.setCursor(13, 0); //从第一行 第十四个字符显示
- lcd.print(Weekday);
- lcd.setCursor(4, 1); //第二行第五个字符开始显示
- lcd.print(hour);
- lcd.print(":");
- lcd.print(minute);
- lcd.print(":");
- lcd.print(second);
-
- delay(800);
- }
复制代码
非常简单啦。。这样就有一个时钟了。。
欲知后事如何,请点传送门 |