极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 41650|回复: 25

自己封装的arduino1.0.1时钟库,使用DS1307芯片

[复制链接]
发表于 2012-10-28 14:06:23 | 显示全部楼层 |阅读模式
本帖最后由 ttyp 于 2012-12-3 20:12 编辑

模块使用的是和arduino学习笔记27 - DS1307 RTC时钟芯片与DS18B20数字温度传感器实验一样的模块,不过我这是没有DS18B20芯片的,例子是好。不过我的IDE1.0.1上代码已经不可用了。用了arduino学习笔记38 - Arduino + DS1307 RTC时钟模块与经典总结里提供的库也不行,开始还以为硬件是坏的呢,找了JS询问还是资料还是没有结果,查线序,找了datasheet测量各个管脚电压,测量上拉电阻,均正常。最后发现在这贴的回复里29楼的代码可以用,硬件没坏就好办了,花了1天时间封装了一下。

这是连接方式,很简单的:


这是封装好的库文件,例子在里面:

这个是最新版,修改了几个BUG


下面是老版

本帖子中包含更多资源

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

x

评分

参与人数 1 +1 收起 理由
幻生幻灭 + 1 赞一个!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2012-10-28 14:07:29 | 显示全部楼层
为了防止冲突,我使用了DS1307A这个类名
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-28 14:22:17 | 显示全部楼层
  1. #include <Wire.h>

  2. int clockAddress = 0x68;  // This is the I2C address
  3. int command = 0;  // This is the command char, in ascii form, sent from the serial port     
  4. long previousMillis = 0;  // will store last time Temp was updated
  5. byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  6. byte test;

  7. // Convert normal decimal numbers to binary coded decimal
  8. byte decToBcd(byte val)
  9. {
  10.   return ( (val/10*16) + (val%10) );
  11. }

  12. // Convert binary coded decimal to normal decimal numbers
  13. byte bcdToDec(byte val)
  14. {
  15.   return ( (val/16*10) + (val%16) );
  16. }

  17. // 1) Sets the date and time on the ds1307
  18. // 2) Starts the clock
  19. // 3) Sets hour mode to 24 hour clock
  20. // Assumes you're passing in valid numbers,
  21. // Probably need to put in checks for valid numbers.
  22. void setDateDs1307()               
  23. {
  24.   // Use of (byte) type casting and ascii math to achieve result.  
  25.   second = 12;
  26.   minute = 33;
  27.   hour  = 16;
  28.   dayOfWeek = 1;
  29.   dayOfMonth = 2;
  30.   month = 3;
  31.   year= 50;
  32.   Wire.beginTransmission(clockAddress);
  33.   Wire.write(byte(0x00));
  34.   Wire.write(decToBcd(second));  // 0 to bit 7 starts the clock
  35.   Wire.write(decToBcd(minute));
  36.   Wire.write(decToBcd(hour));    // If you want 12 hour am/pm you need to set
  37.   // bit 6 (also need to change readDateDs1307)
  38.   Wire.write(decToBcd(dayOfWeek));
  39.   Wire.write(decToBcd(dayOfMonth));
  40.   Wire.write(decToBcd(month));
  41.   Wire.write(decToBcd(year));
  42.   Wire.endTransmission();
  43. }

  44. // Gets the date and time from the ds1307 and prints result
  45. void getDateDs1307() {
  46.   // Reset the register pointer
  47.   Wire.beginTransmission(clockAddress);
  48.   Wire.write(byte(0x00));
  49.   Wire.endTransmission();

  50.   Wire.requestFrom(clockAddress, 7);

  51.   // A few of these need masks because certain bits are control bits
  52.   second     = bcdToDec(Wire.read() & 0x7f);
  53.   minute     = bcdToDec(Wire.read());
  54.   
  55.   // Need to change this if 12 hour am/pm
  56.   hour       = bcdToDec(Wire.read() & 0x3f);  
  57.   dayOfWeek  = bcdToDec(Wire.read());
  58.   dayOfMonth = bcdToDec(Wire.read());
  59.   month      = bcdToDec(Wire.read());
  60.   year       = bcdToDec(Wire.read());

  61.   Serial.print(hour, DEC);
  62.   Serial.print(":");
  63.   Serial.print(minute, DEC);
  64.   Serial.print(":");
  65.   Serial.print(second, DEC);
  66.   Serial.print("  ");
  67.   Serial.print(month, DEC);
  68.   Serial.print("/");
  69.   Serial.print(dayOfMonth, DEC);
  70.   Serial.print("/");
  71.   Serial.print(year, DEC);

  72. }


  73. void setup() {
  74.   Wire.begin();
  75.   Serial.begin(9600);
  76. }

  77. void loop() {
  78.   if (Serial.available()) {  // Look for char in serial que and process if found
  79.     command = Serial.read();
  80.     if (command == 84) {      //If command = "T" Set Date
  81.       setDateDs1307();
  82.       getDateDs1307();
  83.       Serial.println(" ");
  84.     }
  85.     else if (command == 81) {  //If command = "Q" RTC1307 Memory Functions
  86.       delay(100);     
  87.       if (Serial.available()) {
  88.         command = Serial.read();
  89.         
  90.         // If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff).  
  91.         // Therefore 255 or 0 will be an invalid value.  
  92.         if (command == 49) {
  93.          
  94.           // 255 will be the init value and 0 will be cosidered an error that
  95.           // occurs when the RTC is in Battery mode.
  96.           Wire.beginTransmission(clockAddress);
  97.          
  98.           // Set the register pointer to be just past the date/time registers.
  99.           Wire.write(byte(0x08));  
  100.           for (int i = 1; i <= 27; i++) {
  101.             Wire.write(byte(0xff));
  102.             delay(100);
  103.           }   
  104.           Wire.endTransmission();
  105.           getDateDs1307();
  106.           Serial.println(": RTC1307 Initialized Memory");
  107.         }
  108.         else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
  109.           getDateDs1307();
  110.           Serial.println(": RTC 1307 Dump Begin");
  111.           Wire.beginTransmission(clockAddress);
  112.           Wire.write(byte(0x00));
  113.           Wire.endTransmission();
  114.           Wire.requestFrom(clockAddress, 64);
  115.           for (int i = 1; i <= 64; i++) {
  116.             test = Wire.read();
  117.             Serial.print(i);
  118.             Serial.print(":");
  119.             Serial.println(test, DEC);
  120.           }
  121.           Serial.println(" RTC1307 Dump end");
  122.         }
  123.       }  
  124.     }
  125.     Serial.print("Command: ");
  126.     Serial.println(command);  // Echo command CHAR in ascii that was sent
  127.   }

  128.   command = 0;  // reset command                  
  129.   delay(100);
  130. }

复制代码


开始找到的是这个,这是一个简单的可以工作的程序
回复 支持 反对

使用道具 举报

发表于 2012-10-28 17:57:54 | 显示全部楼层
ttyp 发表于 2012-10-28 14:22
#include

int clockAddress = 0x68;  // This is the I2C address

有些比这个更简单,都把东西装进去库里去了!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-28 18:20:47 | 显示全部楼层
Randy 发表于 2012-10-28 17:57
有些比这个更简单,都把东西装进去库里去了!

我看一般的库都是这样搞的啊,没有提供这个地址的设置
回复 支持 反对

使用道具 举报

发表于 2012-10-28 19:52:58 | 显示全部楼层
本帖最后由 shenhaiyu 于 2012-10-28 19:54 编辑


这就是一个完全没问题的官方库,当然,里面的例子是我自己写的,加了个串口时间设置功能。

DS1307第一次用的时候最需要注意的就是年份只有2位,不要按照4位年份往里写

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-28 21:05:27 | 显示全部楼层
shenhaiyu 发表于 2012-10-28 19:52
这就是一个完全没问题的官方库,当然,里面的例子是我自己写的,加了个串口时间设置功能。

DS1307第一 ...

看了下应该是没问题的,不过我觉得没我封装的好

我支持任意年份,一般的只支持2000-3000年,支持自定义的输出日期字符串,读取时兼容12小时制的读取....

看到DS1307_CTRL_ID B1101000才发现1307就是twi地址的意思
回复 支持 反对

使用道具 举报

发表于 2013-1-7 13:29:26 | 显示全部楼层
D1307A库很好用,我的平台是 Mega2560, 之前用的官方库试了些程序没有办法设置年到 2013。现象是设了2013, 但显示变为是2000。

但发现这个库在我的实验环境下有个问题。串口显示出数量不定的行数后就再无响应。
我尝试注释了从ds.poke至  ds.writeBuffer(ram) 后一切正常。 请教这是什么问题? 谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-1-7 13:48:06 | 显示全部楼层
也许你的板子是没有ram的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-1-7 13:50:39 | 显示全部楼层
shenhaiyu 发表于 2012-10-28 19:52
这就是一个完全没问题的官方库,当然,里面的例子是我自己写的,加了个串口时间设置功能。

DS1307第一 ...


库是可以剪裁的,用不到的函数可以删除的,没有的功能想要加上就难了哦
回复 支持 反对

使用道具 举报

发表于 2013-1-7 17:11:25 | 显示全部楼层
ttyp 发表于 2013-1-7 13:48
也许你的板子是没有ram的?

我看到REG RAM的地址如下, 这是是什么设备? 在 DS1307片上还是其他的?
#define REG_RAM_BEGIN        0x8
#define REG_RAM_END     0x3f

还有个问题是, 串口显示会偶尔出现明显不对的一行。 没有任何规律。
2013.1.7  17:3:43
2013.1.7  17:3:44
2070.61.14  0:34:13
2013.1.7  17:3:46
2013.1.7  17:3:47
2013.1.7  17:3:48

谢谢!
回复 支持 反对

使用道具 举报

发表于 2013-1-7 17:15:28 | 显示全部楼层
ttyp 发表于 2013-1-7 13:50
库是可以剪裁的,用不到的函数可以删除的,没有的功能想要加上就难了哦

这个我也试过,我不知道是不是我输入的格式不对还是其他原因, 总之 问题还是 13年的。
比如 我输入 13,1,1,1,1,1 再读取时间时显示不出2013年。

而 TTYP 自封装的库就没有这个问题。
用这个形式设时间, 舒服的很!
ds.setDate(2013,1,7);
ds.setTime(1,1,1);
ds.setWeek(MONDAY);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-1-7 19:38:03 | 显示全部楼层
wruomlig 发表于 2013-1-7 17:11
我看到REG RAM的地址如下, 这是是什么设备? 在 DS1307片上还是其他的?
#define REG_RAM_BEGIN        0x8
...

我不知道你的板子是怎么样的,我的板子有一个芯片24C32,是和eprom一样可以存数据的

你的串口偶尔不对我没碰到过,可能是板子质量问题或干扰,有条件用LCD1602试试,反正我用1602做的时钟一直正常
回复 支持 反对

使用道具 举报

发表于 2013-1-9 14:36:08 | 显示全部楼层
ttyp 发表于 2013-1-7 19:38
我不知道你的板子是怎么样的,我的板子有一个芯片24C32,是和eprom一样可以存数据的

你的串口偶尔不对 ...

高人指路事半功倍哈。
1. 已经成功写入REG。 我手里的板子两头都有接线, 插入带24C的就正常了
2. 发现是USB数据线的问题, 换了根带磁环的就好了。

这个库非常不错, 再结合了 shenhaiyu 给出的程序中串口写入的样例。 我现在已经实现了串口改数据的功能了。2013已经不是问题。

目前自己玩玩, 如果有人买单, 我一定付专利费哈。

再次感谢!




回复 支持 反对

使用道具 举报

发表于 2013-1-9 15:49:30 | 显示全部楼层
1307早买了,C语言俺干不了,有库了,比葫芦画瓢还凑乎
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 21:36 , Processed in 0.043849 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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