Pages: [1]   Go Down
Author Topic: DS3231.h --- Library for DS3231 (or DS3232) real-time clock  (Read 13809 times)
0 Members and 1 Guest are viewing this topic.
Chico, CA
Offline Offline
Newbie
*
Karma: 2
Posts: 7
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written a header file/library/examples for the DS3231 clock: it's available here:
    http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html

I wrote it for the DS3231, but the timekeeping registers are identical on the DS3232 so it'll work for that also.

This forum has been incredibly helpful to me over the years, and I hope this is a start on repayment. Drop me an email if you use it, and let me know if you find bugs so I can fix them right away.

-Eric Ayars
<firstname at lastname.org>
Logged

São Paulo/SP/Brazil
Offline Offline
Sr. Member
****
Karma: 2
Posts: 305
Brazilian Arduino Team
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Great work.

Just a suggestion, why not integrate on the Time Library? There's some examples using DS1307 already, just have to port to work with your library.

Time Library Link:
http://www.arduino.cc/playground/Code/Time
Logged

São Paulo/SP/Brazil
Offline Offline
Sr. Member
****
Karma: 2
Posts: 305
Brazilian Arduino Team
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm with some spare time so I made the library.
https://bitbucket.org/trunet/ds3231rtc

Fully compatible with Time Library and has getTemp method to retrieve temperature.

Just use the TimeRTC examples provided on Time and change DS1307RTC.h to DS3231RTC.h.
Logged

0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 93
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, Eric.  This is cool, I'll be needing that chip in the future and I am glad to have your work!
 smiley-grin
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I was just curious if you are planning to upgrade this library to Arduino 1.0?

https://github.com/trunet/DS3231RTC

Regards
James
Logged

São Paulo/SP/Brazil
Offline Offline
Sr. Member
****
Karma: 2
Posts: 305
Brazilian Arduino Team
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

it just needs minor updates. I can work with this next week maybe.
Logged

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

As it turns out i ran into the same issue with the 1.0 ide and ended up updating it myself.
I ran the examples and they seem to work just fine.

i do not know if you already updated the code or not but here is the a copy of the files i updated.
The example codes remains unchanged but is now in an ino file.

http://soiling-detector.googlecode.com/files/DS3231updated.zip

Your orig lib and examples helped me understand how to get this IC up and running for my datalogger.
I have found a few of your sketches to be rather helpful.
Thank you
« Last Edit: February 09, 2012, 12:55:10 pm by jhub » Logged

São Paulo/SP/Brazil
Offline Offline
Sr. Member
****
Karma: 2
Posts: 305
Brazilian Arduino Team
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

that's the way open source software works... thanks for your help and I'll update the code later.
Logged

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


I set my clock with the example in the zip, and when I run the example test for some reason one time is exactly 6 hours off of the other.  The logs look like:

2012 2 17 5 19 30 25 24h T=20.00 O+
Alarm 1: 5 DoW 19 22 30 enabled
Alarm 2: 17 Date 19 23 enabled
12/2/17day of the week :5
13:30:26

Any ideas where I can look for the bug?  Or ideas what it may be?
Logged

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

It appears from my tests that getHour() is returning the proper hours, but getTime() is not, looking at the code and there didnt seem to be any glaring differences though, any local pros have an idea?
Logged

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

Found it, there is a bug on line 40 of DS3231.cpp where the hours are run through bdcToDec() twice.  To fix this, on line 40 change:

tempBuffer = bcdToDec(Wire.read());

to

tempBuffer = Wire.read();

After this your getTime() calls should read correctly!
Logged

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

hm... how will i get leading zeros?

can anybody help?
Logged

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

Leading  Zeros formatting. Enjoy smiley-cool
05/23/2014


Code:
// Display the time >  getTime() function
Clock.getTime(year, month, date, DoW, hour, minute, second);

        if (month < 10)
          {
            Serial.print("0");
            Serial.print(month, DEC);
          }
        else
          {
            Serial.print(month, DEC);
          }

        Serial.print("/");

if (date < 10)
          {
            Serial.print("0");
            Serial.print(date, DEC);
          }
       else
          {
            Serial.print(date, DEC);
          }
          
        Serial.print("/");
        
        Serial.print("2");
if (Century)
          { // Won't need this until 2100 - or if clock gets reset.
Serial.print("1");
 }
        else
          {
Serial.print("0");
 }
        if (year < 10)             // used if the clock gets reset or if the year is 2100 - 2110
          {
            Serial.print("0");
          }
Serial.println(year);
 
        Serial.print("day of the week : ");
Serial.println(DoW, DEC);

        if (hour <10)
          {
            Serial.print("0");
            Serial.print(hour, DEC);
          }
else
          {
            Serial.print(hour, DEC);
          }
          
        Serial.print(":");

        if (minute < 10)
          {
            Serial.print("0");
            Serial.print(minute, DEC);
          }
        else
          {
            Serial.print(minute, DEC);
          }
        Serial.print(":");
        if (second < 10)
        {
          Serial.print("0");
          Serial.println(second, DEC);
          
        }
else
        {
          Serial.println(second, DEC);
        }

// simple way to format the output to include the leading zeros.
// with adjustments for formatting the year if clock gets reset or year 2100+
// NewAgeRyan85

« Last Edit: May 25, 2014, 02:21:12 pm by NewAgeRyan85 » Logged

Pages: [1]   Go Up
Jump to: