How to interface Real Time Clock DS1307 to PIC16F84

Microcontroller Topics
Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to interface Real Time Clock DS1307 to PIC16F84

Post by Shane » Wed Jul 06, 2011 10:30 am

DS1307 is one of the devices that used widely in micrcontroller applications. It provide Serial Real Time Clock is a low–power, full BCD clock/calendar plus 56 bytes of nonvolatile SRAM. Address and data are transferred serially via the 2–wire bi–directional bus.The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with less than 31 days, including corrections for leap year.The clock operates in either the 24–hour or 12–hour format with AM/PM indicator. The DS1307 has a built–in power sense circuit which detects power failures and automatically switches to the battery supply.

Figure 1. DS1307 PIN OUT
1.gif
1.gif (1.27 KiB) Viewed 8593 times
It 's importance to have some knowledge about I2C when we want to interface with DS1307because the DS1307 operates as a I2C slave device on the serial bus. Access is obtained by(see fig. 3)implementing a START condition and providing a device identification code followed by a register address. Subsequent registers can be accessed sequentially until a STOP condition is executed. The START and STOP conditions are generated using the low level drives, SEND_START and SEND_STOP found in the attached microcontroller code. Also the subroutines SEND_BYTE and READ_BYTE provide the 2–wire handshaking required for writing and reading 8–bit words to and from the DS1307.For more information about I2C see at Phillips web site.

Figure 2. I2C BUS CONFIGURATION
2.gif
2.gif (9.1 KiB) Viewed 8593 times
Figure 3. DATA TRANSFER ON 2–WIRE SERIAL BUS
3.gif
3.gif (11.47 KiB) Viewed 8593 times
2-wire timing interface (See Fig.3)

Start data transfer: A change in the state of the data line from high to low, while the clock line is high, defines a START condition.

Stop data transfer: A change in the state of the data line from low to high, while the clock line is high, defines the STOP condition.

Data valid: The state of the data line represents valid data when, after a START condition, the data line is stable for the duration of the high period of the clock signal. The data on the line must be changed during the low period of the clock signal. There is one clock pulse per bit of data. Each data transfer is initiated with a START condition and terminated with a STOP condition. The number of data bytes transferred between the START and the STOP conditions is not limited, and is determined by the master device. The information is transferred byte–wise and each receiver acknowledges with a ninth bit.

Acknowledge: Each receiving device, when addressed, is obliged to generate an acknowledge after the reception of each byte. The master device must generate an extra clock pulse which is associated with this acknowledge bit. A device that acknowledges must pull down the SDA line during the acknowledge clock pulse in such a way that the SDA line is stable low during the high period of the acknowledge related clock pulse. Of course, setup and hold times must be taken into account. A master must signal an end of data to the slave by not generating an acknowledge bit on the last byte that has been clocked out of the slave. In this case, the slave must leave the data line high to enable the master to generate the STOP condition.

Interfacing with PIC
There are two method to programming to interface I2C devices with PICMicro :Software I2C and Hardware I2C interface.But,here is only software I2C which programming with CCS. The DS1307 signal pin must pulled up with external resistor about 10K to ensure logic level.

The software will reads all data from RTC then send to PC via RS232 interface with buad rate 9600,8,N,1. In main loop just read and send data every 500 mS to refresh Hyper terminal program.

Figure 4.Connecting DS1307 with PIC for this example
4.gif
4.gif (5.9 KiB) Viewed 8593 times
In this example I use Hyper terminal sotware to show all data received from DS1307 and you can use other terminal programs that you can find.
5.gif
5.gif (6.58 KiB) Viewed 8593 times
CCS code
ds1307.zip
(1.58 KiB) Downloaded 936 times
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to interface Real Time Clock DS1307 to PIC16F84

Post by Rksk » Sun Aug 28, 2011 12:34 pm

Can't we count time without DS1307 (Only using Micro)?
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

Re: How to interface Real Time Clock DS1307 to PIC16F84

Post by Shane » Sun Aug 28, 2011 6:20 pm

I thought the guys of ROBOT.LK are not much interested with my articles ;)

Ok, a real time clock is basically just like a watch - it runs on a battery and keeps time for you even when there is a power outage! Using an RTC, you can keep track of long timelines, even if you reprogram your uC (microcontroller) or disconnect it from USB or a power plug.

Most uCs have built-in timers that can keep track of longer time periods like minutes or days. So why would you want to have a separate RTC chip? Well, the biggest reason is that a micro can only keeps track of time since it was last powered. That means when the power is turned on, the timer is set back to 0. The uC doesn't know that it's 'Tuesday' or 'March 8th', all it can tell is 'It's been 18,000 milliseconds since I was last turned on'.

OK, so what if you wanted to set the time on the uC? You'd have to program in the date and time and you could have it count from that point on. But if it lost power, you'd have to reset the time. Much like very cheap alarm clocks: every time they lose power they blink 12:00

While this sort of basic timekeeping is OK for most of the projects, some projects such as data-loggers, clocks, etc need to have consistent timekeeping that doesn't reset when the uC battery dies or is reprogrammed. Thus, we include a separate RTC! The RTC chip is a specialised chip that just keeps track of time. It can count leap-years and knows how many days are in a month, but it doesn't take care of Daylight Savings Time (because it changes from place to place).

See this image with the inbuilt battery.
clock.jpg
clock.jpg (62.46 KiB) Viewed 8522 times
clock2.jpg
clock2.jpg (68.02 KiB) Viewed 8522 times
Is it clear now?
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to interface Real Time Clock DS1307 to PIC16F84

Post by Rksk » Sun Aug 28, 2011 8:13 pm

It is a very clear answer. :clap:

Thank You very much.
Post Reply

Return to “Microcontrollers”