I2Cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs

DS1307/DS1307.h
00001 // I2Cdev library collection - DS1307 I2C device class header file
00002 // Based on Maxim DS1307 datasheet, 2008
00003 // 11/13/2011 by Jeff Rowberg <jeff@rowberg.net>
00004 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
00005 // I2C Device Library hosted at http://www.i2cdevlib.com
00006 //
00007 // Changelog:
00008 //     2011-11-13 - initial release
00009 
00010 /* ============================================
00011 I2Cdev device library code is placed under the MIT license
00012 Copyright (c) 2011 Jeff Rowberg
00013 
00014 Permission is hereby granted, free of charge, to any person obtaining a copy
00015 of this software and associated documentation files (the "Software"), to deal
00016 in the Software without restriction, including without limitation the rights
00017 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00018 copies of the Software, and to permit persons to whom the Software is
00019 furnished to do so, subject to the following conditions:
00020 
00021 The above copyright notice and this permission notice shall be included in
00022 all copies or substantial portions of the Software.
00023 
00024 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00025 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00026 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00027 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00028 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00029 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00030 THE SOFTWARE.
00031 ===============================================
00032 */
00033 
00034 #ifndef _DS1307_H_
00035 #define _DS1307_H_
00036 
00037 #include "I2Cdev.h"
00038 
00039 // comment this out if you already have a JeeLabs' DateTime class in your code
00040 // or if you don't need DateTime functionality
00041 #define DS1307_INCLUDE_DATETIME_CLASS
00042 
00043 // comment this out if you don't need DateTime functionality
00044 #define DS1307_INCLUDE_DATETIME_METHODS
00045 
00046 #define DS1307_ADDRESS              0x68 // this device only has one address
00047 #define DS1307_DEFAULT_ADDRESS      0x68
00048 
00049 #define DS1307_RA_SECONDS           0x00
00050 #define DS1307_RA_MINUTES           0x01
00051 #define DS1307_RA_HOURS             0x02
00052 #define DS1307_RA_DAY               0x03
00053 #define DS1307_RA_DATE              0x04
00054 #define DS1307_RA_MONTH             0x05
00055 #define DS1307_RA_YEAR              0x06
00056 #define DS1307_RA_CONTROL           0x07
00057 #define DS1307_RA_RAM               0x08
00058 
00059 #define DS1307_SECONDS_CH_BIT       7
00060 #define DS1307_SECONDS_10_BIT       6
00061 #define DS1307_SECONDS_10_LENGTH    3
00062 #define DS1307_SECONDS_1_BIT        3
00063 #define DS1307_SECONDS_1_LENGTH     4
00064 
00065 #define DS1307_MINUTES_10_BIT       6
00066 #define DS1307_MINUTES_10_LENGTH    3
00067 #define DS1307_MINUTES_1_BIT        3
00068 #define DS1307_MINUTES_1_LENGTH     4
00069 
00070 #define DS1307_HOURS_MODE_BIT       6 // 0 = 24-hour mode, 1 = 12-hour mode
00071 #define DS1307_HOURS_AMPM_BIT       5 // 2nd HOURS_10 bit if in 24-hour mode
00072 #define DS1307_HOURS_10_BIT         4
00073 #define DS1307_HOURS_1_BIT          3
00074 #define DS1307_HOURS_1_LENGTH       4
00075 
00076 #define DS1307_DAY_BIT              2
00077 #define DS1307_DAY_LENGTH           3
00078 
00079 #define DS1307_DATE_10_BIT          5
00080 #define DS1307_DATE_10_LENGTH       2
00081 #define DS1307_DATE_1_BIT           3
00082 #define DS1307_DATE_1_LENGTH        4
00083 
00084 #define DS1307_MONTH_10_BIT         4
00085 #define DS1307_MONTH_1_BIT          3
00086 #define DS1307_MONTH_1_LENGTH       4
00087 
00088 #define DS1307_YEAR_10H_BIT         7
00089 #define DS1307_YEAR_10H_LENGTH      4
00090 #define DS1307_YEAR_1H_BIT          3
00091 #define DS1307_YEAR_1H_LENGTH       4
00092 
00093 #define DS1307_CONTROL_OUT_BIT      7
00094 #define DS1307_CONTROL_SQWE_BIT     4
00095 #define DS1307_CONTROL_RS_BIT       1
00096 #define DS1307_CONTROL_RS_LENGTH    2
00097 
00098 #define DS1307_SQW_RATE_1           0x0
00099 #define DS1307_SQW_RATE_4096        0x1
00100 #define DS1307_SQW_RATE_8192        0x2
00101 #define DS1307_SQW_RATE_32768       0x3
00102 
00103 #ifdef DS1307_INCLUDE_DATETIME_CLASS
00104     // DateTime class courtesy of public domain JeeLabs code
00105     // simple general-purpose date/time class (no TZ / DST / leap second handling!)
00106     class DateTime {
00107         public:
00108             DateTime (uint32_t t=0);
00109             DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour=0, uint8_t min=0, uint8_t sec=0);
00110             DateTime (const char* date, const char* time);
00111             uint16_t year() const       { return 2000 + yOff; }
00112             uint8_t month() const       { return m; }
00113             uint8_t day() const         { return d; }
00114             uint8_t hour() const        { return hh; }
00115             uint8_t minute() const      { return mm; }
00116             uint8_t second() const      { return ss; }
00117             uint8_t dayOfWeek() const;
00118     
00119             // 32-bit times as seconds since 1/1/2000
00120             long secondstime() const;
00121             // 32-bit times as seconds since 1/1/1970
00122             uint32_t unixtime(void) const;
00123         
00124         protected:
00125             uint8_t yOff, m, d, hh, mm, ss;
00126     };
00127 #endif
00128 
00129 class DS1307 {
00130     public:
00131         DS1307();
00132         DS1307(uint8_t address);
00133 
00134         void initialize();
00135         bool testConnection();
00136 
00137         // SECONDS register
00138         bool getClockRunning();
00139         void setClockRunning(bool running);
00140         uint8_t getSeconds(); // 0-59
00141         void setSeconds(uint8_t seconds);
00142 
00143         // MINUTES register
00144         uint8_t getMinutes(); // 0-59
00145         void setMinutes(uint8_t minutes);
00146 
00147         // HOURS register
00148         uint8_t getMode(); // 0-1
00149         void setMode(uint8_t mode);
00150         uint8_t getAMPM(); // 0-1
00151         void setAMPM(uint8_t ampm);
00152         uint8_t getHours12(); // 1-12
00153         void setHours12(uint8_t hours, uint8_t ampm);
00154         uint8_t getHours24(); // 0-23
00155         void setHours24(uint8_t hours);
00156 
00157         // DAY register
00158         uint8_t getDayOfWeek(); // 1-7
00159         void setDayOfWeek(uint8_t dow);
00160 
00161         // DATE register
00162         uint8_t getDay(); // 1-31
00163         void setDay(uint8_t day);
00164         
00165         // MONTH register
00166         uint8_t getMonth(); // 1-12
00167         void setMonth(uint8_t month);
00168 
00169         // YEAR register
00170         uint16_t getYear(); // 1970, 2000, 2011, etc
00171         void setYear(uint16_t year);
00172         
00173         // CONTROL register
00174         bool getFixedOutputLevel();
00175         void setFixedOutputLevel(bool level);
00176         bool getSquareWaveEnabled();
00177         void setSquareWaveEnabled(bool enabled);
00178         uint8_t getSquareWaveRate();
00179         void setSquareWaveRate(uint8_t rate);
00180         
00181         // RAM registers
00182         uint8_t getMemoryByte(uint8_t offset);
00183         void setMemoryByte(uint8_t offset, uint8_t value);
00184 
00185         // convenience methods
00186 
00187         void getDate(uint16_t *year, uint8_t *month, uint8_t *day);
00188         void setDate(uint16_t year, uint8_t month, uint8_t day);
00189 
00190         void getTime12(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint8_t *ampm);
00191         void setTime12(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t ampm);
00192         
00193         void getTime24(uint8_t *hours, uint8_t *minutes, uint8_t *seconds);
00194         void setTime24(uint8_t hours, uint8_t minutes, uint8_t seconds);
00195         
00196         void getDateTime12(uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint8_t *ampm);
00197         void setDateTime12(uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t ampm);
00198         
00199         void getDateTime24(uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *seconds);
00200         void setDateTime24(uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds);
00201         
00202         #ifdef DS1307_INCLUDE_DATETIME_METHODS
00203             DateTime getDateTime();
00204             void setDateTime(DateTime dt);
00205         #endif
00206 
00207     private:
00208         uint8_t devAddr;
00209         uint8_t buffer[1];
00210         bool mode12;
00211         bool clockHalt;
00212 };
00213 
00214 #endif /* _DS1307_H_ */
 All Data Structures Functions Variables