This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Interfacing 89c51 with DS 1307(Real Time Clock)

Hi I am trying to interface AT89c51 with DS 1307( a real time clock) which uses I2C Communication protocol. The problem is that Real Time Clock could'nt be triggered(started) after setting up the initial register values.I can read the same Data which i've sended to DS 1307 Registers again & again.

Here is the Source Code is Please Help

#include<lcd4bitx51.h> //library file to drive LCD
sbit SDA=P3^0;
sbit SCL=P3^1;
void delay(unsigned int x)
{
    unsigned int i,j;
    for (i=0;i<=x;i++)
    for (j=0;j<=500;j++);
}

void i2c_dly(void)
{
    int i;
    for(i=0;i<=20;i++);
}


void i2c_start(void)
{
  SDA = 1;             // i2c start bit sequence
  i2c_dly();
  SCL = 1;
  i2c_dly();
  SDA = 0;
  i2c_dly();
  SCL = 0;
  i2c_dly();
}

void i2c_stop(void)
{
  SDA = 0;             // i2c stop bit sequence
  i2c_dly();
  SCL = 1;
  i2c_dly();
  SDA = 1;
  i2c_dly();
}
unsigned char i2c_rx(char ack)
{
char x, d=0;
  SDA = 1;
  for(x=0; x<8; x++) {
    d <<= 1;
    do {
      SCL = 1;
    }
    while(SCL==0);    // wait for any SCL clock stretching
    i2c_dly();
    if(SDA) d |= 1;
    SCL = 0;
  }
  if(ack) SDA = 0;
  else SDA = 1;
  SCL = 1;
  i2c_dly();             // send (N)ACK bit
  SCL = 0;
  SDA = 1;
  return d;
}
bit i2c_tx(unsigned char d)
{
char x;
static bit b;
  for(x=8; x; x--) {
    if(d&0x80) SDA = 1;
    else SDA = 0;
    SCL = 1;
    d <<= 1;
    SCL = 0;
  }
  SDA = 1;
  SCL = 1;
  i2c_dly();
  b = SDA;          // possible ACK bit
  SCL = 0;
  return b;
}

void display(unsigned char ch,int pos)
{
        unsigned char a,b;
        a=ch/10;
        b=ch%10;
        LocateLCD(1,pos);
        PutCharLCD(a+48);
        PutCharLCD(b+48);
}


void main(void)
{


   unsigned char ch0,ch1,ch2,ch3,ch4,ch5,ch6 ;
   InitLCD_rimsDEV2763 ();
        PrintLCD("DIGITAL CLOCK");

    ResetLCD();

  //-----------------intialization----------------------

    i2c_start();              // send start sequence
    i2c_tx(0XD0);             // eeprom I2C address with R/W bit clear
    i2c_tx(0x00);             // data word register address
    i2c_tx(0x00);             // data to be sent(Seconds)
    i2c_dly();


        i2c_tx(0x01);//minutes
         i2c_dly();

         i2c_tx(0x07);//hours
         i2c_dly();

         i2c_tx(0x02);//days
         i2c_dly();

         i2c_tx(0x03);//date
         i2c_dly();

         i2c_tx(0x03);//month
         i2c_dly();

         i2c_tx(0x07);//year
         i2c_dly();

        i2c_tx(0x80);//control
         i2c_dly();

         i2c_stop();


    delay(100);

//---------------------------Read
        while(1)
{
        i2c_start();              // send start sequence
        i2c_tx(0XD0);             // eeprom I2C address with R/W bit clear
        i2c_tx(0x00);             // data word register address




        i2c_start();
    i2c_tx(0xD1);
    ch0= i2c_rx(1);
         i2c_dly();

         ch1=i2c_rx(1) ;
                 i2c_dly();

                 ch2= i2c_rx(1);
         i2c_dly();

         ch3=i2c_rx(1) ;
         i2c_dly();

         ch4= i2c_rx(1);
         i2c_dly();

         ch5=i2c_rx(1) ;
     i2c_dly();

         ch6= i2c_rx(1);
          i2c_dly();


          i2c_stop();
    //i2c_stop();


        display(ch2,2);
        display(ch1,5);
        display(ch0,8);

delay (100);

}

}

0