What is real time clock ?
Real time clocks are small integrated circuit from those ICs we can find out what is the current real time and date of the world . Some chip comes with embedded clock some do not . If these IC do not comes with crystal we need to apply crystal externally to make communication with micorcontrollers or other processing circuit .
Real Time Clock RTC DS1307
DS1307 is the one of the most widely used integrated circuit to measure date and time .They use I2C communication protocal to commnicate with external processors or devices some basic properties are .
- The RTC DS1307 uses external crystal of frequency 32.768 kHz, so we have to connect crystal with 32.768 kHz to X1 and X2 pin.
- We have to connect 3 Volt CMOS battery to Vbat pin. RTC DS1307 has inbuilt mechanism to detect 5 volt Vcc, if external 5 volt Vcc is not there, then it takes the supply from 3 volt CMOS battery.
- The SDA (Serial Data) and SCL (Serial Clock) pins are I2C serial communication pins which we have to connect to microcontroller’s I2C pins.
- SQW/OUT pin is square wave output driver. The SQW/OUT pin outputs one of four square-wave frequencies 1Hz, 4kHz, 8kHz, 32kHz by setting internal register bits.
Generally, while using RTC (Real Time Clock) first time, we need to set current time and date in RTC. Then RTC keeps updating these values in seconds. In RTC DS1307, we can set this time and date in the Timekeeper Register. After setting time and date value, RTC DS1307 keeps it updating in seconds so we will get updated time later on.
- The content of Timekeeper registers is in BCD (Binary Coded Decimal value) format.
- There are total eight registers in timekeeper register for setting seconds, Minutes, Hours, Day, Date, Month, year and control.
- Once we set the value of this registers, they will keep updating themselves, and we can read these registers to get updated values.
Address - 00H:02H: Clock Registers
Address - 00H:
- In this register bit- 7 is CH bit, which is crystal oscillator enable / disable bit, when it is zero, the crystal oscillator is enabled otherwise oscillator is not enabled, so we always have to make this bit zero.
- Other bits are used for read / write the second. As timekeeper register stores the value in BCD format, here Bit- 4 to Bit-6 stores the upper BCD digit of the seconds (value from 0 to 5), and Bit- 0 to Bit-3 stores lower BCD digit of the seconds (value from 0 to 9). As seconds value starts from 00 and ends at 59.
Address - 01H:
- Used to read / write minutes’ value.
- Upper BCD digit of minutes is stored in Bit-4 to Bit-6 and lower BCD digit is stored in Bit-0 to Bit- 3
Address - 02H:
- It is used to read / write Hour.
- Clock can run either 12Hr or 24Hr format.
- 12-hour format: To set 12-hour clock format, we have to set Bit- 6 to logic 1. In 12- hour clock format Bit-5 will indicate AM / PM, Logic 1 is for PM and Logic 0 is for AM. Bit-4 is indicated as 10 Hour, which is to store higher digit of hour value, which is 0 or 1 in case of 12-hour system. Bit-0 to Bit- 3 stores the value of lower digit of hour (value from 0 to 9).
- 24-hour Format: To set 24-hour clock format, we have to reset Bit- 6 to logic 0. Bit-4 and Bit- 5 are indicated as 10 Hour, which is to store higher digit of hour value, which is 0 to 2 in case of 24-hour system. Bit-0 to Bit- 3 stores the value of lower digit of hour (value from 0 to 9).
Address - 03H: 06H: Calendar Register
Address - 03H:
Used to read /write day value from 0 to 7. Bit-0 to Bit-2 are used to read /write day value.
Address - 04H:
Used to read / write the date value. Bit- 4 and Bit-5 are used to read / write upper digit value of date (value from 0 to 3). Bit- 0 to Bit-3 are used for lower digit of date value (value from 0 to 9).
Address - 05H:
Used to read / write the month. Bit- 4 used for upper digit value of month that is 0 or 1. And Bit- 0 to Bit- 3 are used to store the lower digit value of the month (value from 0 to 9).
Used to read / write the year value. It provides only last two digits of year value. Bit- 0 to Bit- 3 stores lower digit, and Bit- 4 to Bit- 7 stores higher digit of the year.
Address - 07H: Control Register
Bit 7 - OUT: Output
This control the output level of pin SQW/OUT, when square wave output is disable, that is SQWE bit is zero. So logic level on SQW/OUT is high when this OUT bit is high, and zero when this OUT bit is zero.
Bit 4 – SQWE
To Enable / Disable square wave output on SQW / OUT pin
1 = Enable oscillator output
0 = Disable oscillator output
The frequency of square wave is depending on RS0 and RS1 bit.
Bit 0:1 - RS0 & RS1
An example of Real time clock interfaced with AVR atmega16 and LCD can be found in this link .