Erriez DS1302 RTC library for Arduino  2.0.0
DS1302 RTC library for Arduino
ErriezDS1302.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_DS1302_H_
34 #define ERRIEZ_DS1302_H_
35 
36 #include <Arduino.h>
37 #include <time.h>
38 
40 #define DS1302_ACB 0x80
41 #define DS1302_ACB_RAM 0x40
42 #define DS1302_ACB_CLOCK 0x00
43 #define DS1302_ACB_READ 0x01
44 #define DS1302_ACB_WRITE 0x00
45 
46 #define DS1302_CMD_READ_CLOCK_REG(reg) (DS1302_ACB | DS1302_ACB_CLOCK | (((reg) & 0x1F) << 1) | DS1302_ACB_READ)
48 #define DS1302_CMD_WRITE_CLOCK_REG(reg) (DS1302_ACB | DS1302_ACB_CLOCK | (((reg) & 0x1F) << 1) | DS1302_ACB_WRITE)
50 #define DS1302_CMD_READ_CLOCK_BURST (DS1302_ACB | DS1302_ACB_CLOCK | 0x3E | DS1302_ACB_READ)
52 #define DS1302_CMD_WRITE_CLOCK_BURST (DS1302_ACB | DS1302_ACB_CLOCK | 0x3E | DS1302_ACB_WRITE)
54 #define DS1302_CMD_READ_RAM(addr) (DS1302_ACB | DS1302_ACB_RAM | (((addr) & 0x1F) << 1) | DS1302_ACB_READ)
56 #define DS1302_CMD_WRITE_RAM(addr) (DS1302_ACB | DS1302_ACB_RAM | (((addr) & 0x1F) << 1) | DS1302_ACB_WRITE)
58 #define DS1302_CMD_READ_RAM_BURST (DS1302_ACB | DS1302_ACB_RAM | 0x3E | DS1302_ACB_READ)
60 #define DS1302_CMD_WRITE_RAM_BURST (DS1302_ACB | DS1302_ACB_RAM | 0x3E | DS1302_ACB_WRITE)
62 
64 #define DS1302_REG_SECONDS 0x00
65 #define DS1302_REG_MINUTES 0x01
66 #define DS1302_REG_HOURS 0x02
67 #define DS1302_REG_DAY_MONTH 0x03
68 #define DS1302_REG_MONTH 0x04
69 #define DS1302_REG_DAY_WEEK 0x05
70 #define DS1302_REG_YEAR 0x06
71 #define DS1302_REG_WP 0x07
72 #define DS1302_REG_TC 0x08
73 
74 #define DS1302_NUM_CLOCK_REGS 7
76 #define DS1302_NUM_RAM_REGS 31
77 
79 #define DS1302_SEC_CH 7
80 #define DS1302_BIT_WP 7
81 #define DS1302_BIT_READ 0
82 
83 #define DS1302_TCS_DISABLE 0x5C
84 
85 #ifdef __AVR
86 #define DS1302_CLK_LOW() { *portOutputRegister(_clkPort) &= ~_clkBit; }
87 #define DS1302_CLK_HIGH() { *portOutputRegister(_clkPort) |= _clkBit; }
88 #define DS1302_CLK_INPUT() { *portModeRegister(_clkPort) &= ~_clkBit; }
89 #define DS1302_CLK_OUTPUT() { *portModeRegister(_clkPort) |= _clkBit; }
90 
91 #define DS1302_IO_LOW() { *portOutputRegister(_ioPort) &= ~_ioBit; }
92 #define DS1302_IO_HIGH() { *portOutputRegister(_ioPort) |= _ioBit; }
93 #define DS1302_IO_INPUT() { *portModeRegister(_ioPort) &= ~_ioBit; }
94 #define DS1302_IO_OUTPUT() { *portModeRegister(_ioPort) |= _ioBit; }
95 #define DS1302_IO_READ() ( *portInputRegister(_ioPort) & _ioBit )
96 
97 #define DS1302_CE_LOW() { *portOutputRegister(_cePort) &= ~_ceBit; }
98 #define DS1302_CE_HIGH() { *portOutputRegister(_cePort) |= _ceBit; }
99 #define DS1302_CE_INPUT() { *portModeRegister(_cePort) &= ~_ceBit; }
100 #define DS1302_CE_OUTPUT() { *portModeRegister(_cePort) |= _ceBit; }
101 #else
102 #define DS1302_CLK_LOW() { digitalWrite(_clkPin, LOW); }
103 #define DS1302_CLK_HIGH() { digitalWrite(_clkPin, HIGH); }
104 #define DS1302_CLK_INPUT() { pinMode(_clkPin, INPUT); }
105 #define DS1302_CLK_OUTPUT() { pinMode(_clkPin, OUTPUT); }
106 
107 #define DS1302_IO_LOW() { digitalWrite(_ioPin, LOW); }
108 #define DS1302_IO_HIGH() { digitalWrite(_ioPin, HIGH); }
109 #define DS1302_IO_INPUT() { pinMode(_ioPin, INPUT); }
110 #define DS1302_IO_OUTPUT() { pinMode(_ioPin, OUTPUT); }
111 #define DS1302_IO_READ() ( digitalRead(_ioPin) )
112 
113 #define DS1302_CE_LOW() { digitalWrite(_cePin, LOW); }
114 #define DS1302_CE_HIGH() { digitalWrite(_cePin, HIGH); }
115 #define DS1302_CE_INPUT() { pinMode(_cePin, INPUT); }
116 #define DS1302_CE_OUTPUT() { pinMode(_cePin, OUTPUT); }
117 #endif
118 
119 // Delay defines
120 #if F_CPU >= 20000000UL
121 #define DS1302_PIN_DELAY() { delayMicroseconds(1); }
122 #else
123 #define DS1302_PIN_DELAY()
124 #endif
125 
126 
129 {
130 public:
131  // Constructor
132  ErriezDS1302(uint8_t clkPin, uint8_t ioPin, uint8_t cePin);
133  bool begin();
134 
135  // Oscillator functions
136  bool isRunning();
137  bool clockEnable(bool enable=true);
138 
139  // Set/get date/time
140  time_t getEpoch();
141  bool setEpoch(time_t t);
142  bool read(struct tm *dt);
143  bool write(const struct tm *dt);
144  bool setTime(uint8_t hour, uint8_t min, uint8_t sec);
145  bool getTime(uint8_t *hour, uint8_t *min, uint8_t *sec);
146  bool setDateTime(uint8_t hour, uint8_t min, uint8_t sec,
147  uint8_t mday, uint8_t mon, uint16_t year,
148  uint8_t wday);
149  bool getDateTime(uint8_t *hour, uint8_t *min, uint8_t *sec,
150  uint8_t *mday, uint8_t *mon, uint16_t *year,
151  uint8_t *wday);
152 
153  // BCD conversions
154  uint8_t bcdToDec(uint8_t bcd);
155  uint8_t decToBcd(uint8_t dec);
156 
157  // Read/write register
158  uint8_t readRegister(uint8_t reg);
159  bool writeRegister(uint8_t reg, uint8_t value);
160 
161  // Read/write buffer
162  bool readBuffer(uint8_t reg, void *buffer, uint8_t len);
163  bool writeBuffer(uint8_t reg, void *buffer, uint8_t len);
164 
165  void writeByteRAM(uint8_t addr, uint8_t value);
166  void writeBufferRAM(uint8_t *buf, uint8_t len);
167 
168  uint8_t readByteRAM(uint8_t addr);
169  void readBufferRAM(uint8_t *buf, uint8_t len);
170 
171 private:
172 #ifdef __AVR
173  uint8_t _clkPort;
174  uint8_t _ioPort;
175  uint8_t _cePort;
176 
177  uint8_t _clkBit;
178  uint8_t _ioBit;
179  uint8_t _ceBit;
180 #else
181  uint8_t _clkPin;
182  uint8_t _ioPin;
183  uint8_t _cePin;
184 #endif
185 
186  // RTC interface functions
187  void transferBegin();
188  void transferEnd();
189  void writeAddrCmd(uint8_t value);
190  void writeByte(uint8_t value);
191  uint8_t readByte();
192 };
193 
194 #endif // ERRIEZ_DS1302_H_
void writeBufferRAM(uint8_t *buf, uint8_t len)
Write buffer to RAM address 0x00 (burst write)
void readBufferRAM(uint8_t *buf, uint8_t len)
Read buffer from RAM address 0x00 (burst read)
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.
ErriezDS1302(uint8_t clkPin, uint8_t ioPin, uint8_t cePin)
Constructor DS1302 RTC.
bool isRunning()
Read RTC CH (Clock Halt) from seconds register.
bool clockEnable(bool enable=true)
Enable or disable oscillator.
time_t getEpoch()
Read Unix UTC epoch time_t.
uint8_t bcdToDec(uint8_t bcd)
BCD to decimal conversion.
uint8_t readRegister(uint8_t reg)
Read register.
bool setTime(uint8_t hour, uint8_t min, uint8_t sec)
Write time to RTC.
bool begin()
Initialize and detect DS1302 RTC.
uint8_t readByteRAM(uint8_t addr)
Read byte from RAM.
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 readBuffer(uint8_t reg, void *buffer, uint8_t len)
Read buffer from RTC clock registers.
bool writeBuffer(uint8_t reg, void *buffer, uint8_t len)
Write buffer to RTC clock registers.
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.
void writeByteRAM(uint8_t addr, uint8_t value)
Write a byte to RAM.
uint8_t decToBcd(uint8_t dec)
Decimal to BCD conversion.
bool write(const struct tm *dt)
Write date and time to RTC.
DS1302 RTC class.
Definition: ErriezDS1302.h:128
bool read(struct tm *dt)
Read date and time from RTC.
bool writeRegister(uint8_t reg, uint8_t value)
Write register.