Pages: 1 2 [3] 4 5 ... 8   Go Down
Author Topic: Library for DS1307 Real Time Clock  (Read 29124 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does anyone know how to change the sqw output on the ds1307 to a 1hz rate (and make sure its enabled)?  I'm trying to use it as an interrupt for my code, as opposed to making it query the chip each time.
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 0
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you only need a reliable source of 1 seconds delays, why not use millis()? Although you will have to check in loop() or modify the interrupt's code (you'll have to search this forum for more on that) to toggle a boolean value every timerCounter%1000 == 0, you'll then know if you've come across a new second when the variable isn't what it last was. This, however implies that nothing that could happen meanwhile would take more than a second. Otherwise you could add a counter to that conditional statement, make sure you don't overlfow the variable though (see the variable types documentation if you're not familiar with this)!
« Last Edit: April 07, 2008, 10:11:33 pm by xSmurf » Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would rather use the interrupt method as millis has been shown to be unreliable in some instances, and if I have that pin I might as well use it.  Since the pin is open drain, for testing I wired up the negative end of an led to it, then the led to a resistor, and that resistor to 5v+.  All that happens is the led lights up, as opposed to alternating on and off.

So far, my last attempt at setting it looks like this:
#include <Wire.h>
#define DS1307_CTRL_ID B11010000

void setup()
{
  delay(300);
  Wire.beginTransmission(DS1307_CTRL_ID);
  Wire.send(0x07);
  Wire.send(B10010011);
  Wire.endTransmission();
  
}

void loop()
{
}

edit: I got it working! I just stopped and started the clock around the wire code.  The easiest way I found to do this was simply including ds1307.cpp and doing RTC.stop(); and RTC.start();
« Last Edit: April 07, 2008, 10:48:30 pm by Tuckie » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for those interested, when I was polling each clock cycle, it was able to run though about 850 times each second.  now that I use the interrupt, its around 151,157 times a second.  I'm sure its even higher if I remove my serial debugging routines.
« Last Edit: April 07, 2008, 11:45:19 pm by Tuckie » Logged

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been using this function . . .

Code:
void Set_Square_Wave(int rate){  // set the square wave output on pin 7 of the DS1307 chip
  rate = rate + 144;                         // add 0x90 (dec 144) to rate
  Wire.beginTransmission(0x68);              // write the control register
  Wire.send(0x07);                           // register address 07H)
  Wire.send(rate);                           // 90H=1Hz, 91H=4kHz, 92H=8kHz, 93H=32kHz
  Wire.endTransmission();
}

I pass it: "0=1Hz 1=4k 2=8k 3=32k"
Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is anyone else getting weird output like this?

0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006

It seems like the clock isn't incrementing at all. It'll just loop that number over and over, even though I physically disconnect the pins.

Any ideas?
Logged

Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Whoooo ! You freezed time !

* big respect *


(Sorry sorry, but I just killed my last AC>DC converter, so I've got nothing to do until shops open ^^)
Logged

... could use some sleep

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is anyone else getting weird output like this?

0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006
0 0 30 0 4 4 2006

It seems like the clock isn't incrementing at all. It'll just loop that number over and over, even though I physically disconnect the pins.

Any ideas?

Did you ever get this resolved?  I'm experiencing similar problems.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
http://arduinors.net
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. Congratulations Matt.

Can you help me?!? I have the same problem that user arduinonano.

But the difference is my ds1307 increments, only later of 5 or 6 seconds..

I don't now what I need to do...
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would it be possible to add support for the other RTCs in the DS13xx series do you think?  I've just got a DS1305 wired up and would like to help make the library (more) comprehensive...  (Note that I've yet to get it to work!)

As far as I can tell, the differences between the 1307 and 1305 are
* the 1307 has a square wave output - the 1305 doesn't
* the 1307 can't use a rechargeable power source; battery backup only - the 1305 can have either
* the 1307 only has a two wire interface (SDA and SCLK) - the 1305 is a three wire interface (SDI, SDO and SCLK) but SDI and SDO can be connected
* the 1307 has no alarms - the 1305 has two
* the 1307 has no power failure out - the 1305 has
* the 1307 has 56 bytes of battery backed RAM - the 1305 has 96 bytes

So, what do folks think?  Will it bloat the library too much?

Thanks for your consideration.

John.
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For simplicity and code space reason, I don't see why we shouldn't just have one library for each.  It may not be super-elegant, but it is pretty simple.

Besides, the 1305 sounds is an SPI.  That's very different than I2C on the peripheral interface side, even if the application interface is similar.


I'd also like to note a small bug in the original DS1307.cpp code.  There's a test for the day of the month that checks for <31, should check for <=31.  It gave me fits while trying to set the clock on July 31.  smiley

-j

Logged

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 268
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello

Ok I'm a completely noob to I2C. I would like to run just a clock on the Arduino.
So I got he DS1307 but how to wire this thing?


SCL to analog input 5.
SDA to analog input 4.
VBat and GND to the Battery.

And what else? Now i get stuck....
Could you pls help me and I  hope you don't get crazy about this "stupid" question...

Andy
« Last Edit: August 08, 2008, 02:55:58 am by GekoCH » Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Connect Vcc to +5, and connect a 32768kHz crystal at X1 and X2.  Ignore SQWOUT.

That Vbat to ground is very important - Vbat must be connected to ground or a battery or the clock doesn't tick.  Of course, if you don't have a battery, your clock forgets what time it is whenever it loses power.

Then use the code from the first few posts in this thread and you're ticking away.

-j

Logged

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 268
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm ok so I checked the Crystal but I got two. Do both work?
https://www.distrelec.com/distrelec/datasheets.nsf/WebAttachments/1F9E989B5F6E3D15C12573FC0030C2CB/$File/AURIS_Uhrenquarz_Datasheet_EN.pdf

Andy
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That datasheet is for the same xtal, just in two different packages.

But yeah, that's the correct frequency.

-j

Logged

Pages: 1 2 [3] 4 5 ... 8   Go Up
Jump to: