本帖最后由 迷你强 于 2015-3-30 17:43 编辑
好久没写文档了,最近太忙了,抽空写篇使用说明文档吧,最近憋了一款很高级的模块,实时时钟(RTC),这款时钟与市面上其他时钟方案有一些区别,功能比较强大,偏向于实际应用中断唤醒什么的,而不是单纯的提供现在的时间,主要在低功耗应用场景有非常不错的应用
拥有后备电池,超低功耗 ,供电电压1.0-5.5V均可
属于简单好用,高精度的时间模块了
我们简单说下怎么配置时间。我写了一段通过串口配置的代码,比当年写DS3231的时候高级了一点。
先来下载驱动库
解压缩放入arduino\libraries\ 目录下重启IDE即可
看图接线
接线很简单,VCC 接5v , GND 接GND , SCL 接A5 , SDA 接 A4
标准的I2C通讯电路
好了,下载下面的代码 - /* Demonstration of Rtc_Pcf8563 Set Time.
- * 时钟时间初始化代码,初始化后时钟会一直运行,以后基本不需要重新初始化了。
- *电路链接
- * VCC to 5V
- * GND to GND
- * SCL to A5
- * SDA to A4
- */
- #include <Wire.h>
- #include <Rtc_Pcf8563.h>
- //init the real time clock
- Rtc_Pcf8563 rtc;
- char data[15]; //(多一位作为结束符存储)
- char Str_year[3], Str_month[3], Str_date[3], Str_DoW[3], Str_hour[3], Str_minute[3], Str_second[3]; //字符串时间(多一位作为结束符存储空间)
- byte year, month, day, DoW, hour, minute, second;
- void setup()
- {
- Serial.begin(9600);
- //clear out the registers
- //set a time to start with.
- //day, weekday, month, century(1=1900, 0=2000), year(0-99)
- Serial.println("Please enter the time: year[2]month[2]date[2]DoW[2]hour[2]minute[2]second[2]");
- Serial.println("example: 2014-12-3 Wednesday 14:15:15 enter:14120303141515");
-
- }
- void loop()
- {
-
- if (Serial.available() >=14) //串口读取数据
- {
- for (int a = 0; a <14; a++)
- {
- data[a] = Serial.read();
- }
- Str_year[0] = data[0]; //拆包
- Str_year[1] = data[1];
- Str_month[0] = data[2];
- Str_month[1] = data[3];
- Str_date[0] = data[4];
- Str_date[1] = data[5];
- Str_DoW[0] = data[6];
- Str_DoW[1] = data[7];
- Str_hour[0] = data[8];
- Str_hour[1] = data[9];
- Str_minute[0] = data[10];
- Str_minute[1] = data[11];
- Str_second[0] = data[12];
- Str_second[1] = data[13];
- //Str to byte
- year = atoi(Str_year); //转换数据类型
- month = atoi(Str_month);
- day = atoi(Str_date);
- DoW = atoi(Str_DoW);
- hour = atoi(Str_hour);
- minute = atoi(Str_minute);
- second = atoi(Str_second);
- // conf times; //写入时钟
- rtc.initClock();
- //day, weekday, month, century(1=1900, 0=2000), year(0-99)
- rtc.setDate(day, DoW, month, 0, year);
- //hr, min, sec
- rtc.setTime(hour, minute, second);
- }
- //both format functions call the internal getTime() so that the
- //formatted strings are at the current time/date.
- Serial.print(rtc.formatTime());
- Serial.print("\r\n");
- Serial.print(rtc.formatDate());
- Serial.print("\r\n");
- delay(1000);
- }
复制代码
代码很简单,新买的模块是没有时间的,需要配置,按照要求串口写入时间,就将模块初始化好了。就可以使用了
基本是这样。假设现在的时间是2015年3月30日 周一 14点 30分 52秒
你就在串口写入数字 15033001143052 发送即可。
格式就是:年[2]月[2]日[2]周[2]小时[2]分钟[2]秒[2]
输出时间是外国的习惯 月/日/年 不是错了哟。。。
好了今天就介绍到这里了。下期我们介绍如何独立的输出各个时间来应用,我们下期会用1602液晶屏来做个时钟
有需要的朋友可以在论坛的淘宝杂货铺购买
第二篇传送门 |