Erriez DS1307 I2C RTC library for Arduino  1.0.0
This is a DS1307 I2C Real Time Clock library for Arduino by Erriez.
ErriezDS1307.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #ifndef ERRIEZ_DS1307_H_
34 #define ERRIEZ_DS1307_H_
35 
36 #include <stdint.h>
37 #include <time.h>
38 
40 #define DS1307_REG_SECONDS 0x00
41 #define DS1307_REG_MINUTES 0x01
42 #define DS1307_REG_HOURS 0x02
43 #define DS1307_REG_DAY_WEEK 0x03
44 #define DS1307_REG_DAY_MONTH 0x04
45 #define DS1307_REG_MONTH 0x05
46 #define DS1307_REG_YEAR 0x06
47 #define DS1307_REG_CONTROL 0x07
48 
49 #define DS1307_NUM_REGS 8
51 
52 #define DS1307_SEC_CH 7
54 #define DS1307_HOUR_12H_24H 6
55 #define DS1307_HOUR_AM_PM 5
56 
57 #define DS1307_CTRL_OUT 7
58 #define DS1307_CTRL_SQWE 4
59 #define DS1307_CTRL_RS1 1
60 #define DS1307_CTRL_RS0 0
61 
62 #define DS1307_ADDR (0xD0 >> 1)
64 
68 typedef enum {
74 } SquareWave;
75 
76 
81 {
82 public:
83  // Initialize
84  bool begin();
85 
86  // Oscillator functions
87  bool isRunning();
88  bool clockEnable(bool enable=true);
89 
90  // Set/get date/time
91  time_t getEpoch();
92  bool setEpoch(time_t t);
93  bool read(struct tm *dt);
94  bool write(const struct tm *dt);
95  bool setTime(uint8_t hour, uint8_t min, uint8_t sec);
96  bool getTime(uint8_t *hour, uint8_t *min, uint8_t *sec);
97  bool setDateTime(uint8_t hour, uint8_t min, uint8_t sec,
98  uint8_t mday, uint8_t mon, uint16_t year,
99  uint8_t wday);
100  bool getDateTime(uint8_t *hour, uint8_t *min, uint8_t *sec,
101  uint8_t *mday, uint8_t *mon, uint16_t *year,
102  uint8_t *wday);
103 
104  // Output signal control
105  bool setSquareWave(SquareWave squareWave);
106 
107  // BCD conversions
108  uint8_t bcdToDec(uint8_t bcd);
109  uint8_t decToBcd(uint8_t dec);
110 
111  // Read/write register
112  uint8_t readRegister(uint8_t reg);
113  bool writeRegister(uint8_t reg, uint8_t value);
114 
115  // Read/write buffer
116  bool readBuffer(uint8_t reg, void *buffer, uint8_t len);
117  bool writeBuffer(uint8_t reg, void *buffer, uint8_t len);
118 };
119 
120 #endif // ERRIEZ_DS1307_H_
SQW 32768Hz.
Definition: ErriezDS1307.h:73
#define DS1307_CTRL_SQWE
Square-Wave Enable.
Definition: ErriezDS1307.h:58
bool read(struct tm *dt)
Read date and time from RTC.
bool readBuffer(uint8_t reg, void *buffer, uint8_t len)
Read buffer from RTC.
uint8_t decToBcd(uint8_t dec)
Decimal to BCD conversion.
bool getDateTime(uint8_t *hour, uint8_t *min, uint8_t *sec, uint8_t *mday, uint8_t *mon, uint16_t *year, uint8_t *wday)
Get date time.
bool write(const struct tm *dt)
Write date and time to RTC.
bool setDateTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t mday, uint8_t mon, uint16_t year, uint8_t wday)
Set date time.
#define DS1307_CTRL_RS0
Rate Select 0.
Definition: ErriezDS1307.h:60
bool setSquareWave(SquareWave squareWave)
Configure SQW (Square Wave) output pin.
#define DS1307_CTRL_RS1
Rate Select 1.
Definition: ErriezDS1307.h:59
uint8_t bcdToDec(uint8_t bcd)
BCD to decimal conversion.
bool getTime(uint8_t *hour, uint8_t *min, uint8_t *sec)
Read time from RTC.
bool setEpoch(time_t t)
Write Unix epoch UTC time to RTC.
DS1307 RTC class.
Definition: ErriezDS1307.h:80
SquareWave
Squarewave enum.
Definition: ErriezDS1307.h:68
SQW 8192Hz.
Definition: ErriezDS1307.h:72
bool writeRegister(uint8_t reg, uint8_t value)
Write register.
SQW 1Hz.
Definition: ErriezDS1307.h:70
SQW disable.
Definition: ErriezDS1307.h:69
bool begin()
Initialize and detect DS1307 RTC.
time_t getEpoch()
Read Unix UTC epoch time_t.
bool isRunning()
Read RTC CH (Clock Halt) from seconds register.
bool setTime(uint8_t hour, uint8_t min, uint8_t sec)
Write time to RTC.
SQW 4096Hz.
Definition: ErriezDS1307.h:71
uint8_t readRegister(uint8_t reg)
Read register.
bool writeBuffer(uint8_t reg, void *buffer, uint8_t len)
Write buffer to RTC.
bool clockEnable(bool enable=true)
Enable or disable oscillator.