![]() |
Erriez DS1307 I2C RTC library for Arduino
1.0.0
This is a DS1307 I2C Real Time Clock library for Arduino by Erriez.
|
DS1307 RTC class. More...
#include <ErriezDS1307.h>
Public Member Functions | |
bool | begin () |
Initialize and detect DS1307 RTC. More... | |
bool | isRunning () |
Read RTC CH (Clock Halt) from seconds register. More... | |
bool | clockEnable (bool enable=true) |
Enable or disable oscillator. More... | |
time_t | getEpoch () |
Read Unix UTC epoch time_t. More... | |
bool | setEpoch (time_t t) |
Write Unix epoch UTC time to RTC. More... | |
bool | read (struct tm *dt) |
Read date and time from RTC. More... | |
bool | write (const struct tm *dt) |
Write date and time to RTC. More... | |
bool | setTime (uint8_t hour, uint8_t min, uint8_t sec) |
Write time to RTC. More... | |
bool | getTime (uint8_t *hour, uint8_t *min, uint8_t *sec) |
Read time from RTC. More... | |
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. More... | |
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. More... | |
bool | setSquareWave (SquareWave squareWave) |
Configure SQW (Square Wave) output pin. More... | |
uint8_t | bcdToDec (uint8_t bcd) |
BCD to decimal conversion. More... | |
uint8_t | decToBcd (uint8_t dec) |
Decimal to BCD conversion. More... | |
uint8_t | readRegister (uint8_t reg) |
Read register. More... | |
bool | writeRegister (uint8_t reg, uint8_t value) |
Write register. More... | |
bool | readBuffer (uint8_t reg, void *buffer, uint8_t len) |
Read buffer from RTC. More... | |
bool | writeBuffer (uint8_t reg, void *buffer, uint8_t len) |
Write buffer to RTC. More... | |
DS1307 RTC class.
Definition at line 80 of file ErriezDS1307.h.
uint8_t ErriezDS1307::bcdToDec | ( | uint8_t | bcd | ) |
BCD to decimal conversion.
bcd | BCD encoded value. |
Definition at line 443 of file ErriezDS1307.cpp.
bool ErriezDS1307::begin | ( | ) |
Initialize and detect DS1307 RTC.
Call this function from setup().
true | RTC detected. |
false | RTC not detected. |
Definition at line 52 of file ErriezDS1307.cpp.
bool ErriezDS1307::clockEnable | ( | bool | enable = true | ) |
Enable or disable oscillator.
Clear or set CH (Clock Halt) bit to seconds register
enable | true: Enable RTC clock. false: Stop RTC clock. |
true | Success. |
false | Oscillator enable failed. |
Definition at line 105 of file ErriezDS1307.cpp.
uint8_t ErriezDS1307::decToBcd | ( | uint8_t | dec | ) |
Decimal to BCD conversion.
dec | Decimal value. |
Definition at line 455 of file ErriezDS1307.cpp.
bool ErriezDS1307::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.
hour | Hours 0..23 |
min | Minutes 0..59 |
sec | Seconds 0..59 |
mday | Day of the month 1..31 |
mon | Month 1..12 (1=January) |
year | Year 2000..2099 |
wday | Day of the week 0..6 (0=Sunday, .. 6=Saturday) |
true | Success. |
false | Get date/time failed. |
Definition at line 391 of file ErriezDS1307.cpp.
time_t ErriezDS1307::getEpoch | ( | ) |
Read Unix UTC epoch time_t.
Definition at line 130 of file ErriezDS1307.cpp.
bool ErriezDS1307::getTime | ( | uint8_t * | hour, |
uint8_t * | min, | ||
uint8_t * | sec | ||
) |
Read time from RTC.
Read hour, minute and second registers from RTC.
hour | Hours 0..23. |
min | Minutes 0..59. |
sec | Seconds 0..59. |
true | Success. |
false | Invalid second, minute or hour read from RTC. The time is set to zero. |
Definition at line 303 of file ErriezDS1307.cpp.
bool ErriezDS1307::isRunning | ( | ) |
Read RTC CH (Clock Halt) from seconds register.
The application is responsible for checking the CH (Clock Halt) bit before reading date/time date. This function may be used to judge the validity of the date/time registers.
true | RTC clock is running. |
false | The date/time data is invalid when the CH bit is set. The application should enable the oscillator, or program a new date/time. |
Definition at line 81 of file ErriezDS1307.cpp.
bool ErriezDS1307::read | ( | struct tm * | dt | ) |
Read date and time from RTC.
Read all RTC registers at once to prevent a time/date register change in the middle of the register read operation.
dt | Date and time struct tm. |
true | Success |
false | Read failed. |
Definition at line 190 of file ErriezDS1307.cpp.
bool ErriezDS1307::readBuffer | ( | uint8_t | reg, |
void * | buffer, | ||
uint8_t | readLen | ||
) |
Read buffer from RTC.
reg | RTC register number 0x00..0x07. |
buffer | Buffer. |
readLen | Buffer length. Reading is only allowed within valid RTC registers. |
true | Success |
false | I2C read failed. |
Definition at line 541 of file ErriezDS1307.cpp.
uint8_t ErriezDS1307::readRegister | ( | uint8_t | reg | ) |
Read register.
Please refer to the RTC datasheet.
reg | RTC register number 0x00..0x12. |
Definition at line 469 of file ErriezDS1307.cpp.
bool ErriezDS1307::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.
hour | Hours 0..23 |
min | Minutes 0..59 |
sec | Seconds 0..59 |
mday | Day of the month 1..31 |
mon | Month 1..12 (1=January) |
year | Year 2000..2099 |
wday | Day of the week 0..6 (0=Sunday, .. 6=Saturday) |
true | Success. |
false | Set date/time failed. |
Definition at line 351 of file ErriezDS1307.cpp.
bool ErriezDS1307::setEpoch | ( | time_t | t | ) |
Write Unix epoch UTC time to RTC.
t | time_t time |
true | Success. |
false | Set epoch failed. |
Definition at line 162 of file ErriezDS1307.cpp.
bool ErriezDS1307::setSquareWave | ( | SquareWave | squareWave | ) |
Configure SQW (Square Wave) output pin.
This will disable or initialize the SQW clock pin.
squareWave | SquareWave configuration: Disable: SquareWaveDisable 1Hz: SquareWave1Hz 4096Hz: SquareWave4096Hz 8192Hz: SquareWave8192Hz 32748Hz: SquareWave32768Hz |
true | Success |
false | Set squareWave failed. |
Definition at line 430 of file ErriezDS1307.cpp.
bool ErriezDS1307::setTime | ( | uint8_t | hour, |
uint8_t | min, | ||
uint8_t | sec | ||
) |
Write time to RTC.
Write hour, minute and second registers to RTC.
hour | Hours 0..23. |
min | Minutes 0..59. |
sec | Seconds 0..59. |
true | Success. |
false | Set time failed. |
Definition at line 277 of file ErriezDS1307.cpp.
bool ErriezDS1307::write | ( | const struct tm * | dt | ) |
Write date and time to RTC.
Write all RTC registers at once to prevent a time/date register change in the middle of the register write operation. This function enables the oscillator.
dt | Date/time struct tm. Providing invalid date/time data may result in unpredictable behavior. |
true | Success. |
false | Write failed. |
Definition at line 245 of file ErriezDS1307.cpp.
bool ErriezDS1307::writeBuffer | ( | uint8_t | reg, |
void * | buffer, | ||
uint8_t | writeLen | ||
) |
Write buffer to RTC.
Please refer to the RTC datasheet.
reg | RTC register number 0x00..0x07. |
buffer | Buffer. |
writeLen | Buffer length. Writing is only allowed within valid RTC registers. |
true | Success |
false | I2C write failed. |
Definition at line 513 of file ErriezDS1307.cpp.
bool ErriezDS1307::writeRegister | ( | uint8_t | reg, |
uint8_t | value | ||
) |
Write register.
Please refer to the RTC datasheet.
reg | RTC register number 0x00..0x12. |
value | 8-bit unsigned register value. |
true | Success |
false | Write register failed |
Definition at line 492 of file ErriezDS1307.cpp.