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

i cant able to communicate with DS1307 and 89v51rd2fbc.can any one help me

void main(void)
{
   unsigned char sec, min, hour, date, month, year;
        Sys_Init();
    watch_dog();
        DS1307_init();
        //DS1307_settime(0x67,0x45,0x00);               /* Set Time (hh:mm:ss) */
        //DS1307_setdate(0x03,0x08,0x06);       /* Set Date (dd/mm/yy) */
        while (1)
        {
                watch_dog();
                /* Get Date & Time */
                sec   = DS1307_get(SEC);
                min   = DS1307_get(MIN);
                hour  = DS1307_get(HOUR);
                date  = DS1307_get(DATE);
                month = DS1307_get(MONTH);
                year  = DS1307_get(YEAR);
        }
}

void DS1307_init(void)
{
        I2C_start();
        I2C_write(DS1307_ID);
        I2C_write(0x00);
        //I2C_write(0x7F);
        //I2C_write(0x80);
        I2C_write(0x00);
        I2C_stop();
        datas[0]='I';
        com_t_index=0;
                com_t_length=1;
                TX_RX=1;
                watch_dog();
                TI=1;

}
unsigned char DS1307_get(unsigned char addr)
{
        unsigned char ret;
        DS1307_init();
        I2C_start();            /* Start i2c bus */
        I2C_write(DS1307_ID);
        I2C_write(IN);
        I2C_write(IN);
        I2C_stop();
        I2C_write(DS1307_ID);   /* Connect to DS1307 */
        I2C_write(addr);                /* Request RAM address on DS1307 */

        I2C_start();                    /* Start i2c bus */

        I2C_write(DS1307_ID+1); /* Connect to DS1307 for Read */
        ret = I2C_read();               /* Receive data */

        //I2C_noack();
         //delay_10ms(10);
        I2C_stop();                             /* Stop i2c bus */

   return ret;
        datas[0]=hextoascii((ret>>4)&0x0F);
                datas[1]=hextoascii((ret)&0x0F);
                com_t_index=0;
                com_t_length=2;
                TX_RX=1;
                watch_dog();
                TI=1;
}

void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
        I2C_start();

        I2C_write(DS1307_ID);   /* connect to DS1307 */
        I2C_write(0x00);                /* Request RAM address at 00H */

        I2C_write(ss);                  /* Write sec on RAM address 00H */
        I2C_write(mm);                  /* Write min on RAM address 01H */
        I2C_write(hh);                  /* Write hour on RAM address 02H */
        //delay_10ms(10);
        I2C_stop();             /* Stop i2c bus */
}

void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
        I2C_start();

        I2C_write(DS1307_ID);   /* connect to DS1307 */
        I2C_write(0x04);                /* Request RAM address at 04H */

        I2C_write(dd);                  /* Write date on RAM address 04H */
        I2C_write(mm);                  /* Write month on RAM address 05H */
        I2C_write(yy);                  /* Write year on RAM address 06H */
        //delay_10ms(10);
        I2C_stop();                             /* Stop i2c bus */
}

void I2C_delay(void)
{
        unsigned char i;

        for(i=0; i<I2C_DELAY; i++);
}

void I2C_clock(void)
{
        I2C_delay();

        SCL_1 = 1;              /* Start clock */

        I2C_delay();

        SCL_1 = 0;              /* Clear SCL */
}

void I2C_start(void)
{
        if(SCL_1)
        SCL_1 = 0;              /* Clear SCL */

        SDA_1 = 1;        /* Set SDA */
        SCL_1 = 1;              /* Set SCL */

        I2C_delay();

        SDA_1 = 0;        /* Clear SDA */

        I2C_delay();

        SCL_1 = 0;        /* Clear SCL */
}

void I2C_stop(void)
{
        if(SCL_1)
        SCL_1 = 0;                      /* Clear SCL */

        SDA_1 = 0;                      /* Clear SDA */
        I2C_delay();

        SCL_1 = 1;                      /* Set SCL */

        I2C_delay();

        SDA_1 = 1;                      /* Set SDA */
}

bit I2C_write(unsigned char dat)
{
        bit data_bit;
        unsigned char i;

        for(i=0;i<8;i++)                             /* For loop 8 time(send data 1 byte) */
        {
                data_bit = dat & 0x80;              /* Filter MSB bit keep to data_bit */
                SDA_1 = data_bit;                               /* Send data_bit to SDA */

                I2C_clock();                            /* Call for send data to i2c bus */

                dat = dat<<1;
        }

        SDA_1 = 1;                      /* Set SDA */

        I2C_delay();

        SCL_1 = 1;                      /* Set SCL */

        I2C_delay();

        data_bit = SDA_1;       /* Check acknowledge */
//      I2C_ack();

        SCL_1 = 0;                      /* Clear SCL */

        I2C_delay();

        return data_bit;        /* If send_bit = 0 i2c is valid */
}

unsigned char I2C_read(void)
{
        bit rd_bit;
        unsigned char i, dat;

        dat = 0x00;

        for(i=0;i<8;i++)             /* For loop read data 1 byte */
        {
                I2C_delay();

                SCL_1 = 1;                      /* Set SCL */

                I2C_delay();

                rd_bit = SDA_1;         /* Keep for check acknowledge   */
                dat = dat<<1;
                dat = dat | rd_bit;     /* Keep bit data in dat */

                SCL_1 = 0;                      /* Clear SCL */
        }

        return dat;
}

void I2C_ack()
{
        SDA_1 = 0;              // Clear SDA

        I2C_delay();

        I2C_clock();    //Call for send data to i2c bus

        SDA_1 = 1;              // Set SDA
}

void I2C_noack()
{
        SDA_1 = 1;              // Set SDA

        I2C_delay();

        I2C_clock();    // Call for send data to i2c bus

        SCL_1 = 1;              // Set SCL
}

unsigned short int hextoascii (unsigned char c)
{
        unsigned char ch = c;
        unsigned char as1;
        unsigned char as2;

        // take the char and turn it to ASCII

        as1 = ch;
        as1 >>= 4;
        as1 &= 0x0F;
        if ( as1<10 )
                as1 += 48;
        else
                as1 += 55;

        as2 = ch;
        as2 &= 0x0F;
        if ( as2<10 )
                as2 += 48;
        else
                as2 += 55;

        return( ((unsigned short int)(as1)<<8) + as2 );


}

Parents
  • "i think so"

    Why do you think so?

    What does the DS1307 Datasheet tell you about software control of the oscillator?

    "in my program the DS1307 is initialized"

    But is it initialised correctly?
    Again, what does the DS1307 Datasheet tell you?

    Have you looked at examples, application notes, etc, from the chip manufacturer (Maxim)...?

Reply
  • "i think so"

    Why do you think so?

    What does the DS1307 Datasheet tell you about software control of the oscillator?

    "in my program the DS1307 is initialized"

    But is it initialised correctly?
    Again, what does the DS1307 Datasheet tell you?

    Have you looked at examples, application notes, etc, from the chip manufacturer (Maxim)...?

Children
No data