Pages: [1] 2   Go Down
Author Topic: DS1307 RTC library  (Read 6139 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Im using the DS1307 library that comes with the arduino software, but it doesnt seem to have a "isrunning?" command which is causing problems for me. Is there a better library, or a way to preform this inquiry?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you supplied power to the DS1307? If so, it isRunning(). There is no need for a function to tell you that.
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use this one: adafruit's RTClib

Here are a couple of simple sketches that I have used (not particularly efficient use of Arduino processing power, but maybe enough to get people started):

Code:
//
// DS1307SetTime.pde
//
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//
// RTClib from https://github.com/adafruit/RTClib
//
// davekwx
//

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup ()
{
    Serial.begin(9600);
    Wire.begin();
    rtc.begin();

    char inbuf[80];
    char outbuf[80];
    int i;
    unsigned year;
    unsigned month;
    unsigned day;
    unsigned minutes;
    unsigned hours;
    unsigned seconds;
    Serial.print("Enter date/time: YYYYMMDDHHMMSS");
    while(Serial.available() < 14)
        ;
    Serial.println();
    for(i = 0; i < 14; i++) {
        inbuf[i] = Serial.read();
    }
    sscanf(inbuf, "%4d %2d %2d %2d %2d %2d ", &year, &month, &day, &hours, &minutes, &seconds);
    sprintf(outbuf, "YYYY = %d, MM = %02d, DD = %02d, HH = %02d, MM = %02d, SS = %02d",
            year, month, day, hours, minutes, seconds);
    Serial.println(outbuf);
    rtc.adjust(DateTime(year, month, day, hours, minutes, seconds));
}

uint8_t oldSeconds = 0;
void loop ()
{
    DateTime now = rtc.now();
    if (now.second() != oldSeconds) {
        char buffer[100];
        oldSeconds = now.second();
        sprintf(buffer, "%s %s %2u %d %02d:%02d:%02d",
        now.weekDay(), now.mon(), now. day(), now.year(), now.hour(), now.minute(), now.second());
        Serial.println(buffer);

    }
    delay(200);
}

and
Code:
//
// DS1307ShowTime.pde
//
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//
// RTClib from https://github.com/adafruit/RTClib
//
// davekw7x
//
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"

const int ledPin = 13;
RTC_DS1307 RTC;

// initialize the lcd with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup ()
{
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
    Wire.begin();
    RTC.begin();
    lcd.begin(20,2);

    if (! RTC.isrunning()) {
        Serial.println("RTC was not running, you will have to run DS1307SetTime.");
        Serial.println("For now, I'll set the RTC to the date and time the program was compiled.");
        Serial.println("For better initialization, run Dave's DS1307SetTime sketch.");
        Serial.println();
        // following line sets the RTC to the date & time this sketch was compiled
        RTC.adjust(DateTime(__DATE__, __TIME__));
    }
}

uint8_t old_seconds = 0;
void loop ()
{
    DateTime now = RTC.now();
    if (now.second() != old_seconds) {
        char buffer[40];
        old_seconds = now.second();
        sprintf(buffer, "%s %s %2u %d %02d:%02d:%02d",
        now.weekDay(), now.mon(), now.day(), now.year(), now.hour(), now.minute(), now.second());
        Serial.println(buffer);
        lcd.setCursor(0,0);
        sprintf(buffer, "%s %s %2u %d    ", now.weekDay(), now.mon(), now.day(), now.year());
        lcd.print(buffer);
        lcd.setCursor(0,1);
        sprintf(buffer, "    %02d:%02d:%02d       ", now.hour(), now.minute(), now.second());
        lcd.print(buffer);

    }
    delay(200);
}

Output from DS1307ShowTIme on the serial monitor (See Footnote.)


Fri Mar 11 2011 17:02:35
Fri Mar 11 2011 17:02:36
Fri Mar 11 2011 17:02:37
Fri Mar 11 2011 17:02:38
Fri Mar 11 2011 17:02:39
Fri Mar 11 2011 17:02:40
Fri Mar 11 2011 17:02:41
.
.
.


Regards,

Dave

Footnote:
For a convenient display I added the following two functions to RTClib.cpp to make it easy to display month and day of week as short "strings."
Code:
const char *DateTime::weekDay() const
{
    static const char *day [7] = {
        "Sun", "Mon", "Tue", "Wed",
        "Thu", "Fri", "Sat"
    };
    return day[dayOfWeek() % 7];
}
// January = 1, February = 2, ...
const char *DateTime::mon() const
{
    static const char *m[12] = {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    return m[month()-1];
}
And I added the ;public function declarations in RTClib.h
Code:
   const char * weekDay() const; // String abbreviation
    const char * mon() const;

The same type of things could be done with external functions, but I decided to make them part of the class.
« Last Edit: March 12, 2011, 10:45:46 am by davekw7x » Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you supplied power to the DS1307? If so, it isRunning(). There is no need for a function to tell you that.
In fact, when the chip is completely powered down and back up again it is not running, and a flag is set in a DS1307 register that indicates that the device is not running.

Testing that flag is a really good way to see if the backup battery is working, and to see whether you need to set the time (and then start it running).  The RTClib that I downloaded from adafruit has the function the Original Poster was asking about.  I showed a way to use it in the DS1307ShowTime sketch in my previous post.


Regards,

Dave
« Last Edit: March 12, 2011, 10:47:09 am by davekw7x » Logged

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

Hi guys, I'm also having problem with my RTC. I'm using the adafruit lib, but I'm unable to set the time. Running the "ds1307" example always gives me the following output :

2000/0/0 0:0:0
 since midnight 1/1/1970 = 2313941504s = 26781d
 now + 7d + 30s: 2043/5/6 17:32:14

2000/0/0 0:0:0
 since midnight 1/1/1970 = 2313941504s = 26781d
 now + 7d + 30s: 2043/5/6 17:32:14
...


I'm a newcomer in arduino. The DS1307 starts to get hot after a minute or two. Is there something wrong?
Logged

Sydney, Australia
Offline Offline
Full Member
***
Karma: 3
Posts: 230
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The DS1307 starts to get hot after a minute or two. Is there something wrong?

Depends if you want to cook toast on it or tell the time. How do you have it wired up?
Logged

Is life really that serious...??!

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

It's a small rtc shield that i got from eBay. There are two ways to connect it using A4-A5 directly or with some sda and sdc connection.
Logged

Grand Blanc, MI, USA
Online Online
Faraday Member
**
Karma: 71
Posts: 3504
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The DS1307 starts to get hot after a minute or two. Is there something wrong?

Yes.  It should not be warm to the touch at all.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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

It's a small rtc shield that i got from eBay. There are two ways to connect it using A4-A5 directly or with some sda and sdc connection.
Wow I wasn't expect this but I just bought that same one from eBay and mine almost burned me when I touched it. I really hope we didn't get ripped off  smiley-sad-blue
I am also a new and idk what to make of that... Idk el_pablo maybe if we both have the same issue we should demand a refund
Logged

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

I'm not sure these ebay store from China even care for their customers... I will send him the link to this thread.
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 25
Posts: 4108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This overheating problem is probably unrelated to the original post and deserves a new thread.

Right now all we know is that your 'eBay' devices are not operating properly.  We have no way of knowing if you are doing something wrong or if the devices are just defective.  If you provide us with a link to the eBay source we can:

(1) Possibly come up with some information to help you make your devices function properly.
or
(2) Not order from that source and avoid similar problems ourselves.


Don

Logged

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

This is the RTC shield that I bought.

http://cgi.ebay.ca/DS1307-Based-Real-Time-Clock-Arduino-I2C-/230584918558?pt=LH_DefaultDomain_0&hash=item35afeea21e#ht_753wt_1139
Logged

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

Yup that is the same one i have. I have never worked with a RTC before and because of the complexity of the code and process of using it, it would be difficult for me to be able to tell if 1). I am not using it right 2). I broke it...? 3). or i didn't break it, it came faulty

But because i followed instruction given for a similar RTC and el_pablo has the same issues. I am getting the feeling its not our fault or we are missing some kind of instructions.

Should i email the seller? and what should i say.
Logged

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

PS. if our DS1307 IC got that hot does that mean that they are now fried? Because guessing by normal ebay policy i will have to pay for return shipping and for the same price i can get another new DS1307 IC...?
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 25
Posts: 4108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well there are loads of DS1307 based RTC modules out there on the internet but I could not find any other places (besides other similar eBay sites in different countries) that sold that particular module.  All of the eBay sites with that module display the same information (or lack of information).

The module is unique in that it is designed specifically for the Arduino and it should be idiot proof.  Unless of course it was an idiot who designed or assembled the board.  When you get an overheating chip it is frequently because the power is connected backwards.  I can't tell any of this from the provided picture but it is possible that:
(1) they got the pin spacing wrong so you aren't getting 5V and GND at the left set of pins when the pins at the right are in 4 and 5.
(2) they got the wiring to 5V and GND backwards.
(3) they got the chip in backwards.

What is the labeling on the five pins opposite the Arduino?  If the left one is VCC then the chip is in correctly, if it is GND then the chip is in backwards.  If you provide some nice clear pictures of both sides of the board I may be able to determine if the other guesses are correct.


Quote
if our DS1307 IC got that hot does that mean that they are now fried?
Re-fried chips sometimes still function but they are frequently out of spec.

Don
« Last Edit: April 02, 2011, 05:09:28 pm by floresta » Logged

Pages: [1] 2   Go Up
Jump to: