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

multi slave in I2C bus

Please help me at C code for reading time from DS1407 on I2C bus.I am able to R/W data to an EEPROM on the same bus.But when i read the time i get 0xffff for hrs,mins and secs even after initializing the RTC chip.

Parents Reply Children
  • Exactly I'm taking about DS1307. Well then how to initialize it? is it just to enable by setting the CH bit (second[bit7]) or something else. I'm using an DS1307 IC which is running fine in an another module. so i hope a valid value instead of 0XFFFF.

  • "Well then how to initialize it? is it just to enable by setting the CH bit (second[bit7]) or something else."

    That is not what I asked. I asked:

    "How do you know you initialized it?"

    In other words, what outward response or difference in its operation do you observe that indicates to you that an initialization sequence was successful?

    Even taking it down a level from a 1307-specific response (e.g., reading an expected RTC register value) to the bus level, does the device acknowledge when addressed?

  • your point is absolutely write, when i checked acknowledge status, i find the device don't.

    then what to do. should i put the code for u. please guide me.

  • "when i checked acknowledge status, i find the device don't."

    The first thing that would normally come to mind is verifying that you are using the correct slave address, but you wrote that you have used this device successfully before, so the next thing I'd check is the bus clocking speed. The DS1307 is 100KHz only, whereas many EEPROMs can handle 400KHz. If you have both devices on the same bus, you will have to use 100KHz for everything.

  • " If you have both devices on the same bus, you will have to use 100KHz for everything."

    So kind of your regular and a quick response.

    Yes, As per the specification i bring the clock rate from 400 kHz to 100 kHz by setting the appropriate value in SSPADDR register, the slave doesn't acknowledge to the slave address. I'm am putting I2C_READ module with the initialization. Please verify where the mistake may be.

    /*

    I2C CLOCK RATE W/BRG
    ---------------------
    FCY FCY*2 BRG VALUE FSCL (2 rollovers of BRG)
    --------------------------------------------------------------
    10 MHz 20 MHz 19h 400 kHz(1)
    10 MHz 20 MHz 20h 312.5 kHz
    10 MHz 20 MHz 3Fh 100 kHz
    4 MHz 8 MHz 0Ah 400 kHz(1)
    4 MHz 8 MHz 0Dh 308 kHz
    4 MHz 8 MHz 28h 100 kHz
    1 MHz 2 MHz 03h 333 kHz(1)
    1 MHz 2 MHz 0Ah 100 kHz
    1 MHz 2 MHz 00h 1 MHz(1)

    Note 1: The I2C interface does not conform to the 400 kHz I2C specification (which applies to rates greater than
    100 kHz) in all details, but may be used with care where higher rates are required by the application.
    */

    void i2c_init(void)
    { TRISC3=1; TRISC4=1;

    SSPCON=0x28; SSPCON2=00; SSPADD=0x28;
    }

    unsigned char DS1307_READ(unsigned char addr) // addr=0x03
    { unsigned char read_data; start(); write(0xd0); //while(!ACKSTAT){} // test here whether acknowledged: it hangs up here. write(addr); restart(); write(0xd1); check_idle(); RCEN=1; check_idle(); read_data= SSPBUF; check_idle(); ACKDT=1; ACKEN=1; stop(); return read_data;
    }

  • Please read the instructions on how to post source code:
    www.danlhenry.com/.../keil_code.png

    Note that TABs don't work (well) - use spaces instead,

    and don't forget to check it in the 'Preview' before you post...

  • " If you have both devices on the same bus, you will have to use 100KHz for everything."

    So kind of your regular and a quick response.

    Yes, As per the specification i bring the clock rate from 400 kHz to 100 kHz by setting the appropriate value in SSPADDR register, the slave doesn't acknowledge to the slave address. I'm am putting I2C_READ module with the initialization. Please verify where the mistake may be.

    /*
    
    I2C CLOCK RATE W/BRG
    ---------------------
    FCY FCY*2 BRG VALUE FSCL (2 rollovers of BRG)
    --------------------------------------------------------------
    10 MHz 20 MHz 19h 400 kHz(1)
    10 MHz 20 MHz 20h 312.5 kHz
    10 MHz 20 MHz 3Fh 100 kHz
    4 MHz 8 MHz 0Ah 400 kHz(1)
    4 MHz 8 MHz 0Dh 308 kHz
    4 MHz 8 MHz 28h 100 kHz
    1 MHz 2 MHz 03h 333 kHz(1)
    1 MHz 2 MHz 0Ah 100 kHz
    1 MHz 2 MHz 00h 1 MHz(1)
    
    Note 1: The I2C interface does not conform to the 400 kHz I2C specification (which applies to rates greater than
    100 kHz) in all details, but may be used with care where higher rates are required by the application.
    */
    
    void i2c_init(void)
    {
       TRISC3=1;
       TRISC4=1;
    
       SSPCON=0x28;
       SSPCON2=00;
       SSPADD=0x28;
    }
    
    unsigned char DS1307_READ(unsigned char addr)
    {
       unsigned char read_data;
       start();
       write(0xd0);
       //while(!ACKSTAT){}
       write(addr);
       restart();
       write(0xd1);
       check_idle();
       RCEN=1;
       check_idle();
       read_data= SSPBUF;
       check_idle();
       ACKDT=1;
       ACKEN=1;
       stop();
       return read_data;
    }
    
    

  • "... i bring the clock rate from 400 kHz to 100 kHz by setting the appropriate value in SSPADDR register ..."

    Your code is for a Microchip PIC. Since Keil does not provide PIC development tools, this forum is not the place to be posting. You need to take this discussion to the Microchip forum or your compiler vendor's forum. And when you do take it there, please add what oscillator frequency you are running the PIC at (from SSPADD=0x28, I assume it's 16MHz) and you need to show the code for start(), write(), restart(), check_idle(), and stop().

  • Your code is for a Microchip PIC

    Thank you so much Mr Henry

    A lot of thanks for your co-operation. Indeed i knew that. But i was so ponder with the mistake and i wanted some body to analise. Some of your questionnaire led me to review the hardware and i took the RTC segment to a separate board other than my working mother board which comprise of few more devices like EEPROM and shift registers. Now i con able to communicate with DS1307 and can reed the subsequent registers as well. But as you know still there is more to do. I will let you know the further growth definitely.

    Thanks again.

  • Thanks Mr Henry

    Now its working fine. I hope I'll always find you there in my trouble.

    Thank you again