00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _DS1307_H_
00035 #define _DS1307_H_
00036
00037 #include "I2Cdev.h"
00038
00039
00040
00041 #define DS1307_INCLUDE_DATETIME_CLASS
00042
00043
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
00105
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
00120 long secondstime() const;
00121
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
00138 bool getClockRunning();
00139 void setClockRunning(bool running);
00140 uint8_t getSeconds();
00141 void setSeconds(uint8_t seconds);
00142
00143
00144 uint8_t getMinutes();
00145 void setMinutes(uint8_t minutes);
00146
00147
00148 uint8_t getMode();
00149 void setMode(uint8_t mode);
00150 uint8_t getAMPM();
00151 void setAMPM(uint8_t ampm);
00152 uint8_t getHours12();
00153 void setHours12(uint8_t hours, uint8_t ampm);
00154 uint8_t getHours24();
00155 void setHours24(uint8_t hours);
00156
00157
00158 uint8_t getDayOfWeek();
00159 void setDayOfWeek(uint8_t dow);
00160
00161
00162 uint8_t getDay();
00163 void setDay(uint8_t day);
00164
00165
00166 uint8_t getMonth();
00167 void setMonth(uint8_t month);
00168
00169
00170 uint16_t getYear();
00171 void setYear(uint16_t year);
00172
00173
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
00182 uint8_t getMemoryByte(uint8_t offset);
00183 void setMemoryByte(uint8_t offset, uint8_t value);
00184
00185
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