|
发表于 2014-4-15 19:43:41
|
显示全部楼层
- #include <Wire.h>
- #include <RTClib.h>
-
- void printDateTime(DateTime dateTime);
-
- //创建实例
- RTC_DS1307 RTC;
-
- void setup (void){
- Serial.begin(9600);
- //初始化总线
- Wire.begin();
- //初始化实时时钟
- RTC.begin();
-
- }
-
- void loop() {
- if (Serial.available() > 0) {
-
- int instruct = Serial.read();
-
- switch (instruct) {
- case 'D': {
- //获取当前日期和时间
- DateTime now = RTC.now();
- //通过串口传送当前的日期和时间
- printDateTime(now);
- break;
- } case 'S':
- RTC.set(RTC_YEAR, 14);
- RTC.set(RTC_MONTH, 3);
- RTC.set(RTC_DAY, 5);
-
- RTC.set(RTC_HOUR, 22);
- RTC.set(RTC_MINUTE, 22);
- RTC.set(RTC_SECOND, 0);
- break;
- }
- }
- }
-
- void printDateTime(DateTime dateTime) {
- //传送年份
- Serial.print(dateTime.year(), DEC);
- Serial.print('/');
- //传送月份
- Serial.print(dateTime.month(), DEC);
- Serial.print('/');
- //传送月份中的第几天
- Serial.print(dateTime.day(), DEC);
- Serial.print(' ');
- //传送小时
- Serial.print(dateTime.hour(), DEC);
- Serial.print(':');
- //传送分钟
- Serial.print(dateTime.minute(), DEC);
- Serial.print(':');
- //传送秒
- Serial.print(dateTime.second(), DEC);
- Serial.println();
- }
复制代码 |
|