Working with the DS1307 Serial Real-Time Clock using the USITWIX library for I2C / TWI on Atmel ATtiny85 / Tinusaur.
Let’s see how can we work with the DS1307 serial real-time clock using the USITWIX library for I2C / TWI on Atmel ATtiny85 / Tinusaur.
Bellow is the testing setup.
NOTE: We need the USB-to-Serial just for debugging – it isn’t essential part of the setup.
There is no library yet to do that but with in the testing code there are few functions that could do the job.
Here is brief a description of the functions:
- uint8_t ds1307_read_reg8(uint8_t reg_addr) – reads one byte of data from the specified register.
- uint8_t ds1307_write_reg8(uint8_t reg_addr, uint8_t reg_data) – write one byte of data to the specified register.
- uint8_t ds1307_init(void) – initializes the circuit by writing specific data into the registers.
- uint8_t ds1307_setdatetime(uint16_t year, uint8_t month, uint8_t date, uint8_t weekday, uint8_t hour, uint8_t min, uint8_t sec) – sets date and time.
- uint8_t ds1307_adjust(void) – this is helper function – it sets the date & time to a specific value – let’s not forget that a brand new circuit does not have correct date and time set.
There are 2 additional functions that are used to convert data byte to and from BCD format.
- static uint8_t bcd2bin (uint8_t val)
- static uint8_t bin2bcd (uint8_t val)
This is direct link to the source code of the testing program:
Source code available at: https://bitbucket.org/tinusaur/ds1307tiny
More information about the library will be available at its own page: DS1307tiny