Wednesday, August 13, 2008

DS1307 : Setting Time

There is a question from charlie about how to set time in DS1307 . The code here shows how to write time to DS1307. My C compiler is MikroC 8.0.
void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C_Wr(0xD0); // send byte via I2C (device address + W)
I2C_Wr(address); // send byte (address of DS1307 location)
I2C_Wr(w_data); // send data (data to be written)
I2C_Stop(); // issue I2C stop signal
}

//Set Time
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x27); //write min 27
write_ds1307(2,0x14); //write hour 14
write_ds1307(3,0x02); //write day of week 2:Monday
write_ds1307(4,0x17); // write date 17
write_ds1307(5,0x06); // write month 6 June
write_ds1307(6,0x08); // write year 8 --> 2008
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator



It is your task to incorporate the code with buttons for setting time. I have written code for taking care of the buttons but there are some bugs so I cannot show it now.

5 comments:

Prabin P.B said...

i am doing a project based on pic (16F877a) and rtc. Please send me a sample program in writing and reading from RTC (DS1307).
My email id is: maninmanoj@yahoo.co.in

Cyril Davis-Ade said...

What is the Value of the crystal attached to the DS1307? is it 32.7...kHz or?

kenny90 said...

Please send me a sample program in writing and reading from RTC (DS1307).
my email is kenny_teoh90@hotmail.com
ur reply will be appreciate...

Rully_JR said...

thank's for information
now, i can acces RTC DS1307 with ATmega 16...
finally, my first job succesfully

Arathi Ajay said...

hi,
i'm doing a project on pic16f877a and rtc. can u please send me d code to send an interrupt to pic for a particular time on rtc.i.e.say when the rtc shows 10pm, i want the pic to be in d active mode. so howdo i set that time in rtc thru microc code.pls help me.
my mail id is: arathyleo@yahoo.co.in