Clock pulse to the RTC is provided by a 32.678kHz crystal. As per convention of I2C interface communication, device address while writing into the RTC chip is 0xD0 and device address while reading data from the RTC chip is 0xD1.

Using SCL and SDA lines, the microcontroller can read/write data from/to the memory of the RTC chip.
Some of the conditions to communicate through the I2C bus are:

Clock and data transition. The SDA line is normally pulled high with an external device. Data on the SDA line can change only when SCL line is low. Data changes during a high SCL line will indicate a start or stop condition.

Start and stop. The start condition is a high-to-low transition of SDA line while SCL line is high. The stop condition is a low-to-high transition of SDA line while SCL line is high.

Fig. 1: Circuit of RTC-based event logger

Acknowledge. All addresses and data words are serially transmitted to and from the RTC chip in 8-bit words. The RTC sends a zero to acknowledge that it has received each word. This happens during the ninth clock cycle.

For example, to set the time ‘8:32:59’ into the RTC, the following interface protocol is followed:
1. Start bit is sent from microcontroller IC2 (master) to RTC IC3 (slave).
2. Then 0xD0 is sent to the slave. This is the address of the RTC (as mentioned in its datasheet). Also, the least significant bit (LSB) of 0xD0 is zero, which means it is a writing command for the slave. The slave sends an acknowledgement (ACK) after receiving this byte.
3. 0x00 is sent to the slave. It is the starting address in the RTC on which we want to write the first byte. The slave sends an ACK after receiving this address byte.
4. 0x59 (value of seconds) is sent to the slave. This is the value we want to save in the RTC on 0x00 address. The slave sends an ACK after receiving this byte.
5. 0x32 (value of minutes) is sent to the slave. This is the value we want to save in the RTC on 0x01 address. The slave sends an ACK after receiving this byte.
6. 0x08 (‘8’ is the value of hours, while ‘0’ means 24-hour time format) is sent to the slave. This is the value we want to save in the RTC on 0x02 address. The slave sends an ACK after receiving this byte.
7. A stop bit is sent to the slave.
8. After receiving this command, slave DS1307 sets its time to 8:32:59.

To read back the data from the RTC using the microcontroller, the interface protocol is:
1. Start bit is sent from microcontroller IC2 (master) to RTC IC3 (slave).
2. 0xD0 is sent to the slave. This is the address of the RTC. The slave sends an ACK after receiving this byte.
3. 0x00 is sent to the slave, setting the address pointer to 00. The slave sends an ACK after receiving this address byte.
4. 0xD1 is sent to the slave. This is the address of the RTC (as mentioned in its datasheet). Also, the LSB of 0xD1 is ‘1,’ which means it is a reading command for the slave. The slave sends an ACK after receiving this byte.
5. The data (like seconds, minutes and hours) is read from slave address location.
6. Finally, a stop bit is sent to the slave.
7. After this command, time, say, 8:33:10, is read and can be displayed on the LCD module.

The procedure is similar for day, month and year too.

To derive the power supply for the circuit, the 230V AC mains is stepped down by transformer X1 to deliver a secondary output of 9V, 500 mA. The transformer output is rectified by a full-wave rectifier comprising diodes D1 through D4, filtered by capacitor C1 and regulated by IC1. Capacitor C2 bypasses the ripples present in the regulated supply. LED1 acts as the power indicator and R1 limits the current through LED1. Rechargeable battery BATT.2 is used for power backup. Battery is charged through resistor R8 and diode D5. Resistor R8 limits the charging current. Diode D6 protects the battery from discharging when mains is present.

SHARE YOUR THOUGHTS & COMMENTS

Please enter your comment!
Please enter your name here