Monday, February 16, 2015

Write Data to DS1307 Real Time Clock

Interfacing DS1307 Real Time Clock with PIC16F877A Microcontroller
Write Data to DS1307 Real Time Clock
The DS1307 is real time clock integrated circuit which uses I2C protocol for interfacing with master devices. The DS1307 can save time and date and uses battery to keep the time correct when the entire system is off for example when the electricity goes off.
The pin configuration of this IC is shown below:
ds1307 iic pin rtc
The following table shows how we are going to work with the ds1307. The time and calender read or write is obtained by accessing appropriate register. The format of the time and date are in BCD format. For example if you want to write minute 34 you have to write decimal 67or 0x34  on register 1.
ds1307 i2c mikroc
Write data:
To set the time and calender you have to do the following:
1 - Start I2C protocol,
2 - Send the DS1307 address which is in binary 11010000,
3 - Send register address according to the above table (for example hour register is 0x02),
4 - Write register value,
5 - Stop the I2C protocol.
ds1307 mikroc
Write data to DS1307 mikroC code:
The following code written in MicroC pro for PIC writes the following data:
Monday 10:20:00 , 24-hours mode ;16/02/2015 .

 void main() {
 I2C1_Init(100000); // initialize I2C protocol at 100KHz
 I2C1_Start();      // start signal
 I2C1_Wr(0xD0);   // address DS1307
 I2C1_Wr(0);     // start from word at address (REG0)
 I2C1_Wr(0x80);  // write $80 to REG0. (pause counter + 0 sec)
 I2C1_Wr(0x20);  // write 20 to minutes word to (REG1)
 I2C1_Wr(0x10);  // write 10 to hours word (24-hours mode)(REG2)
 I2C1_Wr(0x02);  // write 2 - Monday (REG3)
 I2C1_Wr(0x16);  // write 16 to date word (REG4)
 I2C1_Wr(0x02);  // write 2 (February) to month word (REG5)
 I2C1_Wr(0x15);  // write 15 to year word (REG6)
 I2C1_Stop();    // issue stop signal

 I2C1_Start();   // issue start signal
 I2C1_Wr(0xD0);  // address DS1307
 I2C1_Wr(0);     // start from word at address 0
 I2C1_Wr(0);     // write 0 to REG0 (enable counting + 0 sec)
 I2C1_Stop();    // issue stop signal