Realtime Clock (RTC) is used to keep track of the current date and time. It is widely used in computers, laptops, cell phones, application devices for embedded systems, etc., GPS coordinates etc. To get timestamps, we need to use RTC (Real Time Clock). Some microcontrollers like LPC2148, LPC1768 etc., have RTC on-chip, but other microcontrollers like PIC, ATmega16 / 32 do not have RTC on-chip. Hence we have to use an external RTC chip. There are different types of ICs used for PSTN like DS1307, DS12C887 etc. In this section, we will look at DS1307.

DS1307 RTC Module

RTC DS1307 Module

RTC DS1307 Pin Diagram

RTC DS1307
RTC DS1307

The RTC DS1307 is an 8-pin IC, as shown in the picture above. 

The RTC DS1307 uses an external crystal with a 32.768 kHz frequency, so we need to connect the crystal with 32.768 kHz to pins X1 and X2. Connect the 3 volt CMOS battery to the Vbat pin. It has a built-in mechanism to detect 5 volts VCC. If there is no external 5 volt VCC, the supply is taken from the 3 volt CMOS battery. SDA (Serial Data) and SCL (Serial Clock) pins are serial I2C communication pins that are used to connect to the I2C pins of the microcontroller. The SQW / OUT pin is the square wave output driver. The SQW / OUT pin outputs one of four square wave frequencies of 1 Hz, 4 kHz, 8 kHz, 32 kHz, which set internal register bits.

In general, the first time we use RTC (Real Time Clock), we need to set the current time and date in RTC. Then RTC updates these values ​​in a matter of seconds. In RTC DS1307, we can configure this time and date in the timekeeper register. By setting the time and date value, the RTC DS1307 will update it in seconds to update it later.

Timekeeper Registers

 Timekeeper register
Timekeeper register

The content of the timer register is in BCD format (binary coded decimal value). There are a total of eight registers in the timer register for setting seconds, minutes, hours, day, date, month, year and control. , they will update themselves, and we can read these registers to get updated values.

Data from the  datasheet

Address – 00H:02H: Clock Registers

Address – 00H: In this register, bit 7 is the CH bit, which is the crystal oscillator enable/disable bit. When it is zero, the crystal oscillator is enabled; otherwise, the oscillator is not activated, so we always set this bit to zero when using RTC. Since the timer register stores, the value in BCD format, bits 4 to 6 store the upper BCD number of seconds (value from 0 to 5), and bits 0 to bit 3 store the lower BCD number of seconds (value from 0 to 9) Because the seconds value starts at 00 and ends at 59.

Address – 01H: This address is used to read/write the minute value. The upper BCD digit of the minutes is stored in Bit4 to Bit6, and the lower BCD digit is stored in Bit0 to Bit 3.

Address – 02H: This address is used to read/write the time. The clock can be operated in a 12-hour or 24-hour format. 12-hour format: To configure the 12-hour format, we need to configure bit 6 in logic 1. In the 12-hour format, bit 5 indicates AM / PM, logic 1 stands for PM and the logical 0 stands for AM Bit4 is displayed as 10 hours, which is to store the highest digit of the hour value, which in the case of a 12-hour clock Systems is 0 or 1. Hour digit (value from 0 to 9). 24-hour format: To configure the 24-hour format, we have to reset bit 6 to logic 0. which is 0 to 2 in the case of a 24-hour system. Bit0 to bit 3 save the value of the lower digit of the hour (value from 0 to 9).

Address – 03H: 06H: Calendar Register

Address – 03H: This address is used to read/write the day value from 1 to 7. Bits 0 to Bit2 are used to read/write the daily value.

Address – 04H: This address is used to read/write the value of the date. Bits 4 and 5 are used to read/write the value of the upper digit of the date (value from 0 to 3). Bits 0 to Bit3 are used for the lower digit of the date value (value 0). to 9).

Address – 05H: This address is used to read/write the month, bit 4 is used for the value of the upper digit of the month, which is 0 or 1, and bits 0 through bit 3 are used for the value of the lower digit of the month to be saved (value from 0 to 9).

Address-06H:  This address is used to read/write the value of the year. Returns only the last two digits of the year value. Bit 0 to bit 3 stores the lowest digit, and bit 4 to bit 7 stores the highest digit of the year.

Address-07H: Control Register

Bit 7 – OUT: Output

This controls the output level of the SQW / OUT pin. When square wave output is disabled, the SQWE bit is zero. Therefore, the logic level in SQW / OUT is high when this OUT bit is high and zero when this OUT bit is zero.

Bit 4 – SQWE

How to enable/disable the square wave output on SQW / OUT

pin 1 = enable the oscillator output

0 = disable the oscillator output

The frequency of a square wave depends on the RS0 and RS1 bit.

Bit 0:1 – RS0 & RS1