极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 24350|回复: 7

OCROBOT 实时时钟模块(RTC pcf8563)调试实录 ① 模块的初始化

[复制链接]
发表于 2015-3-30 14:06:49 | 显示全部楼层 |阅读模式
本帖最后由 迷你强 于 2015-3-30 17:43 编辑

     好久没写文档了,最近太忙了,抽空写篇使用说明文档吧,最近憋了一款很高级的模块,实时时钟(RTC),这款时钟与市面上其他时钟方案有一些区别,功能比较强大,偏向于实际应用中断唤醒什么的,而不是单纯的提供现在的时间,主要在低功耗应用场景有非常不错的应用



拥有后备电池,超低功耗 ,供电电压1.0-5.5V均可

属于简单好用,高精度的时间模块了

我们简单说下怎么配置时间。我写了一段通过串口配置的代码,比当年写DS3231的时候高级了一点。

先来下载驱动库



解压缩放入arduino\libraries\  目录下重启IDE即可

看图接线



接线很简单,VCC 接5v , GND 接GND  , SCL 接A5 , SDA 接 A4
标准的I2C通讯电路

好了,下载下面的代码
  1. /* Demonstration of Rtc_Pcf8563 Set Time.
  2. * 时钟时间初始化代码,初始化后时钟会一直运行,以后基本不需要重新初始化了。
  3. *电路链接
  4. * VCC to 5V
  5. * GND to GND
  6. * SCL to A5
  7. * SDA to A4
  8. */
  9. #include <Wire.h>
  10. #include <Rtc_Pcf8563.h>

  11. //init the real time clock
  12. Rtc_Pcf8563 rtc;


  13. char data[15];   //(多一位作为结束符存储)
  14. char Str_year[3], Str_month[3], Str_date[3], Str_DoW[3], Str_hour[3], Str_minute[3], Str_second[3];    //字符串时间(多一位作为结束符存储空间)
  15. byte year, month, day, DoW, hour, minute, second;

  16. void setup()
  17. {
  18.    Serial.begin(9600);
  19.   //clear out the registers

  20.   //set a time to start with.
  21.   //day, weekday, month, century(1=1900, 0=2000), year(0-99)
  22.   Serial.println("Please enter the time: year[2]month[2]date[2]DoW[2]hour[2]minute[2]second[2]");
  23.         Serial.println("example: 2014-12-3 Wednesday 14:15:15 enter:14120303141515");


  24. }

  25. void loop()
  26. {
  27.   
  28.   if (Serial.available() >=14)     //串口读取数据
  29.         {
  30.                 for (int a = 0; a <14; a++)
  31.                 {
  32.                                 data[a] = Serial.read();
  33.                 }
  34.                 Str_year[0] = data[0];    //拆包
  35.                 Str_year[1] = data[1];
  36.                 Str_month[0] = data[2];
  37.                 Str_month[1] = data[3];
  38.                 Str_date[0] = data[4];
  39.                 Str_date[1] = data[5];
  40.                 Str_DoW[0] = data[6];
  41.                 Str_DoW[1] = data[7];
  42.                 Str_hour[0] = data[8];
  43.                 Str_hour[1] = data[9];
  44.                 Str_minute[0] = data[10];
  45.                 Str_minute[1] = data[11];
  46.                 Str_second[0] = data[12];
  47.                 Str_second[1] = data[13];

  48.                 //Str to byte
  49.                 year = atoi(Str_year);    //转换数据类型
  50.                 month = atoi(Str_month);
  51.                 day = atoi(Str_date);
  52.                 DoW = atoi(Str_DoW);
  53.                 hour = atoi(Str_hour);
  54.                 minute = atoi(Str_minute);
  55.                 second = atoi(Str_second);


  56.                 // conf times;    //写入时钟
  57.                rtc.initClock();
  58.                //day, weekday, month, century(1=1900, 0=2000), year(0-99)
  59.                 rtc.setDate(day, DoW, month, 0, year);
  60.                  //hr, min, sec
  61.                rtc.setTime(hour, minute, second);       
  62.         }


  63.   //both format functions call the internal getTime() so that the
  64.   //formatted strings are at the current time/date.
  65.   Serial.print(rtc.formatTime());
  66.   Serial.print("\r\n");
  67.   Serial.print(rtc.formatDate());
  68.   Serial.print("\r\n");
  69.   delay(1000);
  70. }
复制代码



代码很简单,新买的模块是没有时间的,需要配置,按照要求串口写入时间,就将模块初始化好了。就可以使用了

基本是这样。假设现在的时间是2015年3月30日 周一 14点 30分 52秒
你就在串口写入数字 15033001143052  发送即可。
格式就是:年[2]月[2]日[2]周[2]小时[2]分钟[2]秒[2]


输出时间是外国的习惯  月/日/年 不是错了哟。。。
好了今天就介绍到这里了。下期我们介绍如何独立的输出各个时间来应用,我们下期会用1602液晶屏来做个时钟



有需要的朋友可以在论坛的淘宝杂货铺购买

第二篇传送门

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-3-30 15:07:28 | 显示全部楼层
谢谢,刚好有一个PCF8563,少个库
回复 支持 反对

使用道具 举报

发表于 2015-3-30 15:08:05 | 显示全部楼层
期待你用PCF8563做出更好的作品
回复 支持 反对

使用道具 举报

发表于 2015-3-30 15:12:45 | 显示全部楼层
顶一个+++++
回复 支持 反对

使用道具 举报

发表于 2015-3-30 17:06:46 | 显示全部楼层
顶顶顶顶顶
回复 支持 反对

使用道具 举报

发表于 2015-3-31 08:05:05 | 显示全部楼层
请问这个是充电电池?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-1 10:43:38 | 显示全部楼层
幻生幻灭 发表于 2015-3-31 08:05
请问这个是充电电池?

非充电电池,基本上这样一颗电池都能用用1-2年时间,充电锂电池的寿命也就这么长时间,所以没必要,而且超小电流放电很容易将充电电池过放而死,充电电池的平均容量只有非充电电池的一半而已。一次性电池才是好选择
回复 支持 反对

使用道具 举报

发表于 2016-11-3 21:28:04 | 显示全部楼层
感谢楼主的分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 14:18 , Processed in 0.047128 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表