自己封装的arduino1.0.1时钟库,使用DS1307芯片
本帖最后由 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
下面是老版
为了防止冲突,我使用了DS1307A这个类名 #include <Wire.h>
int clockAddress = 0x68;// This is the I2C address
int command = 0;// This is the command char, in ascii form, sent from the serial port
long previousMillis = 0;// will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers,
// Probably need to put in checks for valid numbers.
void setDateDs1307()
{
// Use of (byte) type casting and ascii math to achieve result.
second = 12;
minute = 33;
hour= 16;
dayOfWeek = 1;
dayOfMonth = 2;
month = 3;
year= 50;
Wire.beginTransmission(clockAddress);
Wire.write(byte(0x00));
Wire.write(decToBcd(second));// 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
// Reset the register pointer
Wire.beginTransmission(clockAddress);
Wire.write(byte(0x00));
Wire.endTransmission();
Wire.requestFrom(clockAddress, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
// Need to change this if 12 hour am/pm
hour = bcdToDec(Wire.read() & 0x3f);
dayOfWeek= bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print("");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
}
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {// Look for char in serial que and process if found
command = Serial.read();
if (command == 84) { //If command = "T" Set Date
setDateDs1307();
getDateDs1307();
Serial.println(" ");
}
else if (command == 81) {//If command = "Q" RTC1307 Memory Functions
delay(100);
if (Serial.available()) {
command = Serial.read();
// If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff).
// Therefore 255 or 0 will be an invalid value.
if (command == 49) {
// 255 will be the init value and 0 will be cosidered an error that
// occurs when the RTC is in Battery mode.
Wire.beginTransmission(clockAddress);
// Set the register pointer to be just past the date/time registers.
Wire.write(byte(0x08));
for (int i = 1; i <= 27; i++) {
Wire.write(byte(0xff));
delay(100);
}
Wire.endTransmission();
getDateDs1307();
Serial.println(": RTC1307 Initialized Memory");
}
else if (command == 50) { //If command = "2" RTC1307 Memory Dump
getDateDs1307();
Serial.println(": RTC 1307 Dump Begin");
Wire.beginTransmission(clockAddress);
Wire.write(byte(0x00));
Wire.endTransmission();
Wire.requestFrom(clockAddress, 64);
for (int i = 1; i <= 64; i++) {
test = Wire.read();
Serial.print(i);
Serial.print(":");
Serial.println(test, DEC);
}
Serial.println(" RTC1307 Dump end");
}
}
}
Serial.print("Command: ");
Serial.println(command);// Echo command CHAR in ascii that was sent
}
command = 0;// reset command
delay(100);
}
开始找到的是这个,这是一个简单的可以工作的程序 ttyp 发表于 2012-10-28 14:22 static/image/common/back.gif
#include
int clockAddress = 0x68;// This is the I2C address
有些比这个更简单,都把东西装进去库里去了! Randy 发表于 2012-10-28 17:57 static/image/common/back.gif
有些比这个更简单,都把东西装进去库里去了!
我看一般的库都是这样搞的啊,没有提供这个地址的设置 本帖最后由 shenhaiyu 于 2012-10-28 19:54 编辑
这就是一个完全没问题的官方库,当然,里面的例子是我自己写的,加了个串口时间设置功能。
DS1307第一次用的时候最需要注意的就是年份只有2位,不要按照4位年份往里写 shenhaiyu 发表于 2012-10-28 19:52 static/image/common/back.gif
这就是一个完全没问题的官方库,当然,里面的例子是我自己写的,加了个串口时间设置功能。
DS1307第一 ...
看了下应该是没问题的,不过我觉得没我封装的好
我支持任意年份,一般的只支持2000-3000年,支持自定义的输出日期字符串,读取时兼容12小时制的读取....
看到DS1307_CTRL_ID B1101000才发现1307就是twi地址的意思 D1307A库很好用,我的平台是 Mega2560, 之前用的官方库试了些程序没有办法设置年到 2013。现象是设了2013, 但显示变为是2000。
但发现这个库在我的实验环境下有个问题。串口显示出数量不定的行数后就再无响应。
我尝试注释了从ds.poke至ds.writeBuffer(ram) 后一切正常。 请教这是什么问题? 谢谢! 也许你的板子是没有ram的? shenhaiyu 发表于 2012-10-28 19:52 static/image/common/back.gif
这就是一个完全没问题的官方库,当然,里面的例子是我自己写的,加了个串口时间设置功能。
DS1307第一 ...
库是可以剪裁的,用不到的函数可以删除的,没有的功能想要加上就难了哦 ttyp 发表于 2013-1-7 13:48 static/image/common/back.gif
也许你的板子是没有ram的?
我看到REG RAM的地址如下, 这是是什么设备? 在 DS1307片上还是其他的?
#define REG_RAM_BEGIN 0x8
#define REG_RAM_END 0x3f
还有个问题是, 串口显示会偶尔出现明显不对的一行。 没有任何规律。
2013.1.717:3:43
2013.1.717:3:44
2070.61.140:34:13
2013.1.717:3:46
2013.1.717:3:47
2013.1.717:3:48
谢谢! ttyp 发表于 2013-1-7 13:50 static/image/common/back.gif
库是可以剪裁的,用不到的函数可以删除的,没有的功能想要加上就难了哦
这个我也试过,我不知道是不是我输入的格式不对还是其他原因, 总之 问题还是 13年的。
比如 我输入 13,1,1,1,1,1 再读取时间时显示不出2013年。
而 TTYP 自封装的库就没有这个问题。
用这个形式设时间, 舒服的很!
ds.setDate(2013,1,7);
ds.setTime(1,1,1);
ds.setWeek(MONDAY); wruomlig 发表于 2013-1-7 17:11 static/image/common/back.gif
我看到REG RAM的地址如下, 这是是什么设备? 在 DS1307片上还是其他的?
#define REG_RAM_BEGIN 0x8
...
我不知道你的板子是怎么样的,我的板子有一个芯片24C32,是和eprom一样可以存数据的
你的串口偶尔不对我没碰到过,可能是板子质量问题或干扰,有条件用LCD1602试试,反正我用1602做的时钟一直正常 ttyp 发表于 2013-1-7 19:38 static/image/common/back.gif
我不知道你的板子是怎么样的,我的板子有一个芯片24C32,是和eprom一样可以存数据的
你的串口偶尔不对 ...
高人指路事半功倍哈。
1. 已经成功写入REG。 我手里的板子两头都有接线, 插入带24C的就正常了
2. 发现是USB数据线的问题, 换了根带磁环的就好了。
这个库非常不错, 再结合了 shenhaiyu 给出的程序中串口写入的样例。 我现在已经实现了串口改数据的功能了。2013已经不是问题。
目前自己玩玩, 如果有人买单, 我一定付专利费哈。
再次感谢!
1307早买了,C语言俺干不了,有库了,比葫芦画瓢还凑乎:)
页:
[1]
2