23 #include "DS1307RTC.h"
24 #include "../I2C/I2CMaster.h"
25 #if defined(ARDUINO) && ARDUINO >= 100
55 #define DS1307_I2C_ADDRESS 0x68
58 #define DS1307_SECOND 0x00
59 #define DS1307_MINUTE 0x01
60 #define DS1307_HOUR 0x02
61 #define DS1307_DAY_OF_WEEK 0x03
62 #define DS1307_DATE 0x04
63 #define DS1307_MONTH 0x05
64 #define DS1307_YEAR 0x06
65 #define DS1307_CONTROL 0x07
66 #define DS1307_NVRAM 0x08
69 #define DS1307_ALARM_SIZE 3
70 #define DS1307_ALARMS (64 - RTC::ALARM_COUNT * DS1307_ALARM_SIZE - 1)
71 #define DS1307_ALARM_MAGIC 63
91 _bus->
write(DS1307_SECOND);
92 if (_bus->
startRead(DS1307_I2C_ADDRESS, 1)) {
93 uint8_t value = _bus->
read();
94 if ((value & 0x80) != 0)
95 writeRegister(DS1307_SECOND, value & 0x7F);
102 if (oneHzPin != 255 && _isRealTime) {
103 pinMode(oneHzPin, INPUT);
104 digitalWrite(oneHzPin, HIGH);
105 writeRegister(DS1307_CONTROL, 0x10);
122 if (_oneHzPin == 255 || !_isRealTime)
127 bool value = digitalRead(_oneHzPin);
128 if (value && !prevOneHz) {
137 inline uint8_t fromBCD(uint8_t value)
139 return (value >> 4) * 10 + (value & 0x0F);
142 inline uint8_t fromHourBCD(uint8_t value)
144 if ((value & 0x40) != 0) {
146 uint8_t result = ((value >> 4) & 0x01) * 10 + (value & 0x0F);
147 if ((value & 0x20) != 0)
148 return (result == 12) ? 12 : (result + 12);
150 return (result == 12) ? 0 : result;
153 return fromBCD(value);
161 _bus->
write(DS1307_SECOND);
162 if (_bus->
startRead(DS1307_I2C_ADDRESS, 3)) {
165 value->
hour = fromHourBCD(_bus->
read());
184 _bus->
write(DS1307_DATE);
185 if (_bus->
startRead(DS1307_I2C_ADDRESS, 3)) {
186 value->
day = fromBCD(_bus->
read());
188 value->
year = fromBCD(_bus->
read()) + 2000;
197 inline uint8_t toBCD(uint8_t value)
199 return ((value / 10) << 4) + (value % 10);
206 _bus->
write(DS1307_SECOND);
220 _bus->
write(DS1307_DATE);
234 _bus->
write(DS1307_ALARMS + alarmNum * DS1307_ALARM_SIZE);
235 if (_bus->
startRead(DS1307_I2C_ADDRESS, 3)) {
236 value->
hour = fromBCD(_bus->
read());
254 _bus->
write(DS1307_ALARMS + alarmNum * DS1307_ALARM_SIZE);
266 return DS1307_ALARMS - DS1307_NVRAM;
272 return readRegister(DS1307_NVRAM + offset);
280 writeRegister(DS1307_NVRAM + offset, value);
285 void DS1307RTC::initAlarms()
287 uint8_t value = readRegister(DS1307_ALARM_MAGIC);
295 for (uint8_t index = 0; index <
ALARM_COUNT; ++index)
297 writeRegister(DS1307_ALARM_MAGIC, 0xB0 + ALARM_COUNT);
302 _bus->
write(DS1307_NVRAM);
303 for (uint8_t index = DS1307_NVRAM; index < DS1307_ALARMS; ++index)
309 uint8_t DS1307RTC::readRegister(uint8_t reg)
313 if (!_bus->
startRead(DS1307_I2C_ADDRESS, 1))
318 bool DS1307RTC::writeRegister(uint8_t reg, uint8_t value)
uint8_t month
Month of the year (1-12)
virtual void writeTime(const RTCTime *value)
Updates the time in the realtime clock to match value.
uint8_t minute
Minute within the hour (0-59)
virtual void readAlarm(uint8_t alarmNum, RTCAlarm *value)
Reads the details of the alarm with index alarmNum into value.
virtual void readDate(RTCDate *value)
Reads the current date from the realtime clock into value.
void readTime(RTCTime *value)
Reads the current time from the realtime clock into value.
bool hasUpdates()
Returns true if the realtime clock has updated since the last call to this function.
void writeAlarm(uint8_t alarmNum, const RTCAlarm *value)
Updates the details of the alarm with index alarmNum from value.
virtual void write(uint8_t value)=0
Writes a single byte value on the I2C bus.
virtual void writeAlarm(uint8_t alarmNum, const RTCAlarm *value)
Updates the details of the alarm with index alarmNum from value.
static const uint8_t ALARM_COUNT
Number of alarms that are supported by RTC::readAlarm() and RTC::writeAlarm().
void writeTime(const RTCTime *value)
Updates the time in the realtime clock to match value.
virtual void writeDate(const RTCDate *value)
Updates the date in the realtime clock to match value.
virtual bool startRead(unsigned int address, unsigned int count)=0
Starts a read operation for count bytes by sending the start condition and the I2C control byte...
uint8_t hour
Hour of the day for the alarm (0-23).
uint8_t flags
Additional flags for the alarm.
Stores date information from a realtime clock chip.
void readDate(RTCDate *value)
Reads the current date from the realtime clock into value.
virtual void startWrite(unsigned int address)
Starts a write operation by sending a start condition and the I2C control byte.
virtual bool endWrite()=0
Ends the current write operation.
unsigned int year
Year (4-digit)
virtual void writeByte(uint8_t offset, uint8_t value)
Writes value to offset within the realtime clock's non-volatile memory.
uint8_t minute
Minute of the hour for the alarm (0-59).
virtual uint8_t readByte(uint8_t offset)
Reads the byte at offset within the realtime clock's non-volatile memory.
Stores time information from a realtime clock chip.
Abstract base class for I2C master implementations.
void writeDate(const RTCDate *value)
Updates the date in the realtime clock to match value.
Stores alarm information from a realtime clock chip.
void readAlarm(uint8_t alarmNum, RTCAlarm *value)
Reads the details of the alarm with index alarmNum into value.
DS1307RTC(I2CMaster &bus, uint8_t oneHzPin=255)
Attaches to a realtime clock slave device on bus.
int byteCount() const
Returns the number of bytes of non-volatile memory that can be used for storage of arbitrary settings...
virtual uint8_t read()=0
Reads a single byte from the I2C bus.
uint8_t readByte(uint8_t offset)
Reads the byte at offset within the realtime clock's non-volatile memory.
uint8_t hour
Hour of the day (0-23)
uint8_t day
Day of the month (1-31)
uint8_t second
Second within the minute (0-59)
virtual void readTime(RTCTime *value)
Reads the current time from the realtime clock into value.
void writeByte(uint8_t offset, uint8_t value)
Writes value to offset within the realtime clock's non-volatile memory.