10 September 2006

Real Time Clock Interfacing


DS1307, a 64 x 8, Serial, I2C Real-Time Clock, is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. It a product of Maxim/Dallas Semiconductor, see the product page.

This is an example how to interface DS1307 with 8051. I use SDCC as C Compiler. My schematic is shown below.

Schematic: 8051 interface to DS1307

Datasheet
- DS1307 [pdf]

Source Code (For SDCC)
- ds1307.h
- test_ds1307.c

Note: require lcd.h and i2c.h


Related Links
- DS1307 Products Page
- Interfacing I2C RTC (DS1307) with MCS-51 (KEIL C51)

25 comments:

Anonymous said...

File "test_ds1307.c" ,Not found.
Pls upload it again.
Thanks...

MCU Programmer said...

I appreciate to let me know that.
Now the problem has been resolved.

Anonymous said...

i try the source code and it's OK.. but the display at LCD is standstill... no counting on time.. it's display

Date: 01/01/...
Time: 00:00:80

can you help me!!!

-m@di

MCU Programmer said...

-m@di

I think, you should recalculate the delaying time for LCD command. It needs 2 ms for each command. My board uses 18.432 MHz of XTAL. So, you should change the number of iterations in LCD delaying function to fit to your XTAL.

Anonymous said...

Hello MCU Programmer,

great work,

can i use this project with DS89C450 MCU

arun(india)

MCU Programmer said...

Yes, sdcc supports DS89C450 MCU, (see here for reference).

But, you have to change #include <p89v51rd2.h> to appropriate header file for your MCU.

Anonymous said...

"I think, you should recalculate the delaying time for LCD command. It needs 2 ms for each command. My board uses 18.432 MHz of XTAL. So, you should change the number of iterations in LCD delaying function to fit to your XTAL."

I was change my delay but still got same result... what is a delay for i2c? may be i want try to change it also...


-m@di

MCU Programmer said...

It's better if we discuss in the Supported Forums. Please register and post your question there. I and others member may can respond for your questions.

Anonymous said...

Hi,
Great work.
but your 8 bit core deosnot support I2C bus than how deos it work as you are interfacing the DS1307 directly to port pins

MCU Programmer said...

It's a very good question. I2C bus does unnecessarily require uC core modules support. I²C uses only two bidirectional open-drain lines, serial data (SDA) and serial clock (SCL), pulled up with resistors.

As you see in my code:

#define SDA P2_7 /* Set P2.7 = SDA */
#define SCL P2_6 /* Set P2.6 = SCL */

The Port 2 of P89V51RD2 are bidirectional open-drain ports. Therefore, I can interface I2C divices with my uC.

If you would like to know more about, see this.

Unknown said...

hi,
i have a problem to read the RTC.after 2 or 3 weeks the time automatically change(drift). i use the quality crystals.pls give the solution for this....

Anonymous said...

very nice article....
With very little changes the code is compiled in keil and successfully interfaced with 89s52 microcontroller.
- Somnath Pandit

Lynn said...

can u show how to interface a DS3234 maxim Real time clock with a microcontroller (M68HC11E1). a motorola type.

SRI said...

hi, my name is sridhar.
i,m trying to display rtc time on lcd but it is not working
i tryed ur code
its showing as (Date: i??/??/??)
(time:??/??/??)
and some time it display some numbers
like(Date: i??/10/??)
(time:??/84/00)
i am in trouble , i should complete it as soon as possible.

please help me.
i tryed n no, of code. but i din't got the right one.

sri3di@gmail.com

fibi miller said...

hi...i am encountering problems as to the obtaining of clock values from the real time clock chip(DS1302), using KEIL C programming.

Can somebody pls enlighten be as to what i have to write in my main program and the subroutine that i should have, in order to display clock values from the real time chip and onto my LCD panel.

annie said...

Please send me full code for displaying time on lcd .......

annie said...

i tried ur code
its showing as (time:??/??/??)
Please help me
my mail id nice_gitu88@gmail.com

Unknown said...

hello sir,
i am trying to generate the 4 Hrs. Delay using DS 1307 RTC interfacing with 89S51.
can you give me the sample code on above problem??
pls help
thnks.

Madhwa said...

Hi,
I want to ask about RTC.
We are interfacing RTC with PIC 16F877 Microcontroller...
RTC functioning properly for some duration.
When we switch on the power supply and after switching it off the RTC will get stuck.
Can anyone help me why this RTC getting stuck.
thank you..

Unknown said...

can you tell the code for the rtc interfacing in assembly language??
my id is crazygal.parul@gmail.com

HENRY said...

hi my name is Adeline and i am trying to interface ds1302 rtc Ic with a atmel at89c51ed2 module. can anyone be kind enough to help me?
Here is my email
adeline-2006@hotmail.com

HENRY said...

hi my name is Adeline and i am trying to interface ds1302 rtc Ic with a atmel at89c51ed2 module. can anyone be kind enough to help me?
Here is my email
adeline-2006@hotmail.com

Unknown said...

any one have assembly program to copy register data from ds1307 to register of 8051.
i want to move data secong to r0 , minute to r1, and so on .
any one have idea then help me & mail me.

Unknown said...

any one know assembly program to copy time register data from ds1307 to register of 8051.
i want to copy second register to r0, min to r1 ,& so on
anyone have idea then help me & mail me.

Unknown said...

I am dooibg project automatic college bell can any one help me for circuit digram &c c codes for ds1307&89s52 ......