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

output of ds1307 to uart using 89v51rd2 is not correct

i have done the interface between ds1307 and 89v51rd2.the output to uart after reading time and date from the rtc ic not correct i am getting like Time=36:32:0D,Date=1F/1C/3F.can any one help me.

  • i have did the BCD2HEX and HEX2ascii convertion.

  • i am getting the output but it is not correct..can u help me

  • Do you really think that's going to be in any way possible based on the information you've provided?

  • the ds1307 is working fine.i am getting the 1 HZ square wave in pin 7 of ds1307.i am getting the data in serial but it is not proper (time=36:32:00 and date=1F/1C/3F).what is the problem with the code...i loaded the code as

    void main(void)
    {
            unsigned int sec, min, hour, date, month, year;
       //uchar sec=0x00, min=0x00, hour=0x00, date=0x00, month=0x00, year=0x00;
            Sys_Init();
            DS1307_init();
            DS1307_settime(0x20,0x20,0x20);         /* Set Time (hh:mm:ss) */
            DS1307_setdate(0x01,0x01,0x01);         /* Set Date (dd/mm/yy) */
            while (1)
            {
                    //watch_dog();
                            GLED=0;
                            delay_10ms(10);
                            GLED=1;
                    /* Get Date & Time */
                    sec   = (DS1307_get(SEC));
                    delay_10ms(10);
                    min   = (DS1307_get(MIN));
                    delay_10ms(10);
                    hour  = (DS1307_get(HOUR));
                    delay_10ms(10);
                    date  = (DS1307_get(DATE));
                    delay_10ms(10);
                    month = (DS1307_get(MONTH));
                    delay_10ms(10);
                    year  = (DS1307_get(YEAR));
                    delay_10ms(10);
    
    
    
    
                    datas[0] = hextoascii(((BCD2HEX(hour))>>4)&0x0F);
                    datas[1] = hextoascii((BCD2HEX(hour))&0x0f);
                    datas[2]=':';
                    datas[3]=hextoascii(((BCD2HEX(min))>>4)&0x0F);
                    datas[4]=hextoascii((BCD2HEX(min))&0x0f);
                    datas[5]=':';
                    datas[6]=hextoascii(((BCD2HEX(sec))>>4)&0x0F);
                    datas[7]=hextoascii((BCD2HEX(sec))&0x0f);
                    datas[8]=' ';
                    datas[9]=hextoascii(((BCD2HEX(date))>>4)&0x0F);
                    datas[10]=hextoascii((BCD2HEX(date))&0x0f);
                    datas[11]='/';
                    datas[12]=hextoascii(((BCD2HEX(month))>>4)&0x0F);
                    datas[13]=hextoascii((BCD2HEX(month))&0x0f);
                    datas[14]='/';
                    datas[15]=hextoascii(((BCD2HEX(year))>>4)&0x0F);
                    datas[16]=hextoascii((BCD2HEX(year))&0x0f);
                    datas[17]='\r';
                    com_t_index=0;
                    com_t_length=18;
                    TX_RX=1;
                    watch_dog();
                    TI=1;
    
                delay_50ms(50);
            }
    }
    
    void DS1307_init(void)
    {
            I2C_start();
            I2C_write(DS1307_ID);
            I2C_write(0x00);
            I2C_write(0x00);
            I2C_stop();
            I2C_start();
            I2C_write(DS1307_ID);
            I2C_write(0x07);
            I2C_write(0x10);
            I2C_stop();
    }
    unsigned int DS1307_get(unsigned char addr)
    {
            unsigned int ret=0;
            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();
    
            I2C_stop();                             /* Stop i2c bus */
    
       return ((((ret>>4)&0x0F)*10)+(ret&0x0F));;
    }
    
    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_20ms(20);
            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_20ms(20);
            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();
    /*      datas[0]=hextoascii((data_bit));
             com_t_index=0;
                    com_t_length=1;
                    TX_RX=1;
                    watch_dog();
                    TI=1;*/
            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;
    
            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;
    }
    

  • i'm not familiarize with C just ASM (A51) but i see in your source that
    you wrote
    for example
    min = (DS1307_get(MIN));
    and in the subroutine DS1307_get(MIN) min means the adress on the DS1307
    but in the main prog you made MIN=0 it should be 1
    sec=0 ; min = 1 ; hour = 2 ; day = 4 ; month = 5 ; year = 6

    try to be sure that the voltage in the "pin 3" < 1.25 * Vcc

  • unsigned int DS1307_get(unsigned char addr)
    {
            unsigned int ret=0;
    
    /* shouldn't you issue a start bit before the write ? */
    
            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();
    
            I2C_stop();                             /* Stop i2c bus */
    
       return ((((ret>>4)&0x0F)*10)+(ret&0x0F));;
    }
    

  • The most important tool for debugging is the one between your ears - you need to learn to think about the problem that you are seeing, think about what could possibly cause those problems, think about what tests & observations to make to identify the problems, etc, etc,...

    Again, here are some tips:

    www.8052.com/.../120313

    www.eetimes.com/.../Developing-a-good-bedside-manner

    Debugging is an essential part of the development process.

    Developing software is not just a matter of writing code - you need to be able to debug it.

    Similarly for hardware development.

  • i noticed on the unsigned int DS1307_get(unsigned char addr) that you return with this:
    ret is on BCD but when you apply return ((((ret>>4)&0x0F)*10)+(ret&0x0F))
    that mean that the returned value is not on BCD why you apply BCD2HEX after this

  • "why you apply BCD2HEX after this"

    Because the posted code is copy/paste from different sources, sprinkled with some manual changes?

  • "Because the posted code is copy/paste from different sources, sprinkled with some manual changes?"

    Before you can start debugging you do, of course, have to understand what the system should do!

    So the first step in debugging is to think about what should be happening. Then you need to think how that differs from what actually happens.

    But it all still comes down to thinking: it's no use pressing the 'run' button and then just saying, "it doesn't work".

  • "Because the posted code is copy/paste from different sources, sprinkled with some manual changes?"

    Now i'm sure that your software is wrong

    first as tell rank amateur
    unsigned int DS1307_get(unsigned char addr)
    { unsigned int ret=0;

    /* shouldn't you issue a start bit before the write ? */

    I2C_write(DS1307_ID); /* Connect to DS1307 */

    second : try too see with me this
    When you set the time you made

    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_20ms(20);
            I2C_stop();             /* Stop i2c bus */
    }
    

    you write on the adress 0 that means the start of second and you write on it ss,mm,hh
    when you write on the DS1307 the adress is incremented automatically 0x00,0x01,0x02 the same thing when you read
    but on your read subroutine you are reading just one value from the specified adress (adr)
    and each time you read the same adress
    where are the value of SEC,MIN,HOUR,DATE,MOTNH,YEAR ?
    i think if you don't specifie it will be take as '0'

    
    
                    sec   = (DS1307_get(SEC));
                    delay_10ms(10);
                    min   = (DS1307_get(MIN));
                    delay_10ms(10);
                    hour  = (DS1307_get(HOUR));
                    delay_10ms(10);
                    date  = (DS1307_get(DATE));
                    delay_10ms(10);
                    month = (DS1307_get(MONTH));
                    delay_10ms(10);
                    year  = (DS1307_get(YEAR));
                    delay_10ms(10);