Library for DS1307 Real Time Clock

For an alarm you'd probably be better off with a DS1337, that has two alarms.

xSmurf wrote a lib for it.
Read from here : http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191209057/6#6

With the DS1307, you'll could periodically read the RTC data and compare it against a predetermined time.

Thank you! I don't really need an alarm now, but I wanted to understand this chip as better as possible. Great, great work mattt, and everyone who contributed!

Mattt

Have you had a chance to update this library to include the 12 hour, AM/PM mode?

Thanks

Thanks for you work Mattt and others!

Downloaded the code from Google code and had it up and running in no time.

Much appreciated!

I observe a strange behaviour on some of the SRAM registers on my DS1307 ...

Like in the example, I am resetting the SRAM with

for(int i=0; i<56; i++)
{
RTC.set_sram_byte(65,i);
}

and then reading it with
RTC.get_sram_data(data);
for(int i=0; i<56; i++)
{
Serial.print(data*+0);*

  • Serial.print(" ");*
  • }[/font]*
    the result is strange:
    65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 18 65 4 65 8 65 3 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65
    i.e. I seem to have registers 26, 28, 30, 32 which apparently contain other values than what I wrote into them? Anybody got an idea why that might be?

Hi,

I would like also to develop a scientific devide consisting on a arduino duemilenove, two (for the moment) temperature sensors (DS18N20), a RTC (DS1307), and a microSD component. My idea is to measure temperature from each sensor one per hour, and save in a txt file at the microSD file, together with date, time and the temperature from each sensor.

I saw some of you are involve in similar devides. Since i am completely new in arduino, could you give me the detailed steps to develop it? You seems to have so much experience and could made me easy to go ahead with this project.

Details about what i have:
Arduino duemilenove: http://www.bricogeek.com/shop/10-arduino-duemilanove-usb.html
Temperature sensors DS18B20: http://www.bricogeek.com/shop/41-sensor-de-temperatura-ds18b20-one-wire.html
RTC DS1307: http://www.bricogeek.com/shop/56-placa-reloj-rtc-ds1307.html
MicroSD: http://www.libelium.com/tienda/catalog/product_info.php?cPath=21&products_id=66

Thanks!

New DS1337 owner here, should I jump in with the code posted at the beginning of the thread specifically for the DS1337 or go with the DS1307 library listed later on Google source?

I picked up a nice couple of CronoDots with the headers soldered on the wrong side for cheap at macetech.com. Look for the ChronoDoh!

If you are just starting out on the DS1337 you may want to checkout my library at
Loading.... I have implemented most functions of the chip.
The library works with the Arduino and Sanguino.

I have a couple of Arduino examples on the site. One outputs a localtime string. The
other demos the interrupts. I will be posting the Sanguino examples (with pics of my
new board) in the next few days.

(* jcl *)


www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org

Ha ha! It lives!

Soldered the backup battery on, wired it up very carefully, and tried the most basic sketch from the macetech.com documentation that doesn't use any library.

It's ticking and spitting out the wrong time! (I haven't set it yet)

ok, tried jluciani's library first. Nothing comes back on the serial port at all. Here is what I tried:

#include <TWI.h>
#include <RTC.h>

#define int_enable   PCICR  |= (1 << PCIE1)
#define int_disable  PCICR  &= ~(1 << PCIE1)

RTC rtc;

void setup()
{
  Serial.begin(9600);
  // setup the TWI 

  TWBR = TWI_TWBR;                         // Set bit rate register (Baudrate). Defined in TWI.h  
  TWDR = 0xFF;                             // Default content = SDA released.

  TWCR = (1<<TWEN)|                        // Enable TWI-interface and release TWI pins.
         (0<<TWIE)|(0<<TWINT)|             // Disable Interupt.
         (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|  // No Signal requests.
         (0<<TWWC);

  int_enable;

  // initialize the date
  // rtc.set_date(2009, 9, 30);

  // intialize the clock
  // rtc.set_time(18, 37, 0);
}


void loop() {
  char timestr[22];
  while(1) {
    rtc.read_regs();
    rtc.localtime(timestr);
    Serial.print(timestr);
    Serial.print("\r");
    delay(1000);
  }
}

What uC are you using?

I've run that code on the 168,328 and 644 (with the Sanguino).

Try putting some Serial.print statements before each instruction in the loop to see
if you can isolate the specific instruction that is causing a problem.

In my main program I have a couple of other include statements. I am assuming
that if these were required your program wouldn't compile.

(* jcl *)

Aha! A moment to get back to this.

I am using a 168.

The code gets through the setup, enters the loop, and gets as far as:
rtc.read_regs();
It never returns from that call.

It could be something simple, what pins should I be using? Is it analog pins 4 and 5?

Yes it is analog pins 4 and 5.

Do you have pull-ups on those lines?

There is a schematic in the NB1A datasheet at http://www.wiblocks.com

(* jcl *)

Hello,

I just started with this library, it seems to work but every time I close the serial port and open it agian the time resets (I have a 3V battery connected)

This is the sketch I use:

#include <Wprogram.h>
#include <Wire.h>
#include <DS1307.h>

int rtc[7];  

void setup()
{
  Serial.begin(9600);
  RTC.stop();
  RTC.set(DS1307_SEC,1);
  RTC.set(DS1307_MIN,23);
  RTC.set(DS1307_HR,12);
  RTC.set(DS1307_DOW,1);
  RTC.set(DS1307_DATE,1);
  RTC.set(DS1307_MTH,10);
  RTC.set(DS1307_YR,7);
  RTC.start();
}

void loop()
{
  RTC.get(rtc,true);
  
  for(int i=0; i<7; i++)
  {
    if (rtc[0] < 10 && i==0){ Serial.print("0");}
    Serial.print(rtc[i]);
    Serial.print(" ");
  }
  
  Serial.println();
  
  delay(1000);

}

Hi Wouterk,

Isn't that what you do in the setup() of the sketch? Setting the time to 12:23:01.

Jeroen

So every time a serial connection is established the Setup() routine is called?

That should indeed explain the kind of behaviour that i'm experiencing...hmm

Okay so I removed the RTC.set statements from the Setup routine.

And set it once to the right time and date.
After 4 minutes the RTC is already more than 2 minutes behind

I'm using an 32,768 Mhz crystal, here's a picture of my setup:

Your picture is very fuzzy so I can't say for sure but it seems like the wiring my not be correct. The crystal should wire to pins 1 & 2, batt + to pin 3 and batt- to pin 4 and common ground.

Lefty

Maybe this will help.
Its what I did when I was playing with th DS1037.

http://scratchpad.thisandthose.org/joomla/index.php?option=com_content&view=article&id=1&Itemid=3

Gordon

Yes it is analog pins 4 and 5.

Do you have pull-ups on those lines?

There is a schematic in the NB1A datasheet at http://www.wiblocks.com

(* jcl *)

Me again, had another few minutes to poke at this.

No pull up resistors on the data lines.

I think the wiring is correct however, because the sketch with no library worked great.

I tried the original DS1307 library next, it works great! Yea! I set the correct time and modified my alarm clock prototype to use the DS1337 breakout board using the DS1307 library. The only extra feature of the DS1337 that I care about really is the added precision, so I think this will work ok for now.