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

Program to write and read a byte between P89V51RD2 and ST M24C64

Hello All,

I am finding it really hard to perform write and read operations on the EEPROM connected to the MCU by two wires using the I2C Protocol.

I've read the data sheets, written the code exactly matching the datasheet info, but it was of no avail.

Please help me out.

My code looks something like this

void Write_Start(void)
        {
                E2P_SDA = 1;
                E2P_SCL = 1;
                delay_i2c();
                E2P_SDA = 0;
                delay_i2c();
                E2P_SCL = 0;
                delay_i2c();
        }

void Write_to_E2P(void)
        {
                unsigned char DEV_SEL_CODE = 0x0A0;
                unsigned char ADDR_MSB = 0x50;
                unsigned char ADDR_LSB = 0x05;
                unsigned char i;

                Write_Start();

                for(i=7;i>=0;i--)
                        {
                                E2P_SDA = DEV_SEL_CODE^i;
                                E2P_SCL = 0;
                                delay_i2c();
                                E2P_SCL = 1;
                                delay_i2c();
                        }

                E2P_SDA = 1;
                E2P_SCL = 0;
                delay_i2c();
                E2P_SCL = 1;
                delay_i2c();
                delay_i2c();
                E2P_SCL = 0;
                delay_i2c();

                if(E2P_SDA == 0)
                        LED1=1;

                for(i=7;i>=0;i--)
                        {
                                E2P_SDA = ADDR_MSB^i;
                                E2P_SCL = 0;
                                delay_i2c();
                                E2P_SCL = 1;
                                delay_i2c();
                        }

                E2P_SDA = 1;
                E2P_SCL = 0;
                delay_i2c();
                E2P_SCL = 1;
                delay_i2c();
                delay_i2c();
                E2P_SCL = 0;
                delay_i2c();

                if(E2P_SDA == 0)
                        LED2=1;

                for(i=7;i>=0;i--)
                        {
                                E2P_SDA = ADDR_LSB^i;
                                E2P_SCL = 0;
                                delay_i2c();
                                E2P_SCL = 1;
                                delay_i2c();
                        }

                E2P_SDA = 1;
                E2P_SCL = 0;
                delay_i2c();
                E2P_SCL = 1;
                delay_i2c();
                delay_i2c();
                E2P_SCL = 0;
                delay_i2c();

                if(E2P_SDA == 0)
                        LED3=1;

                for(i=7;i>=0;i--)
                        {
                                E2P_SDA = current_channel^i;
                                E2P_SCL = 0;
                                delay_i2c();
                                E2P_SCL = 1;
                                delay_i2c();
                        }

                E2P_SDA = 1;
                E2P_SCL = 0;
                delay_i2c();
                E2P_SCL = 1;
                delay_i2c();
                delay_i2c();
                E2P_SCL = 0;
                delay_i2c();

                if(E2P_SDA == 0)
                        LED4=1;


        }

Parents
  • thanks a lot for your valuable advice captain. i dont disagree with you at all, so, i am posting my commented version right away.

    /* the routine for sending a start bit for i2c initialization */
    
    void Write_Start(void)
            {
                    E2P_SDA = 1;                // sda is set high initially for a Hi to Low transition
                    E2P_SCL = 1;                // scl is made high
                    delay_i2c();                // delay of 5 micro seconds
                    E2P_SDA = 0;                // sda is driven low to complete hi to low signal
                    delay_i2c();                // delay of 5 micro seconds
                    E2P_SCL = 0;                // scl is then made low again to complete one clock period
                    delay_i2c();
            }
    
    /* this is used to write a byte onto the eeprom at location 0x5005 */
    
    void Write_to_E2P(void)
    {
        unsigned char DEV_SEL_CODE = 0x0A0;        // device select code
        unsigned char ADDR_MSB = 0x50;             // MSB of address
        unsigned char ADDR_LSB = 0x05;             // LSB of address
        unsigned char i;                           // loop variable
    
        Write_Start();                             // I2C START
    
        for(i=7;i>=0;i--)                          // transmitting from the bit 7 down to bit 0 in the loop
           {
                E2P_SDA = DEV_SEL_CODE^i;  // data bit on the sda line
                E2P_SCL = 0;               // a rising edge of the pulse to sample the sda line
                delay_i2c();
                E2P_SCL = 1;
                delay_i2c();
           }
    
           E2P_SDA = 1;                // these lines are for the 9th clock pulse when the slave pulls the sda line low
           E2P_SCL = 0;
           delay_i2c();
           E2P_SCL = 1;
           delay_i2c();
           delay_i2c();
           E2P_SCL = 0;
           delay_i2c();
    
           if(E2P_SDA == 0)           // if the acknowledge is recieved then we glow an LED used for debugging on board
                LED1=1;
    
                /* the same story follows for the rest three bytes, which are -
                the address MSB and LSB and the data byte to be transferred */
    
                    for(i=7;i>=0;i--)
                            {
                                    E2P_SDA = ADDR_MSB^i;
                                    E2P_SCL = 0;
                                    delay_i2c();
                                    E2P_SCL = 1;
                                    delay_i2c();
                            }
    
                    E2P_SDA = 1;
                    E2P_SCL = 0;
                    delay_i2c();
                    E2P_SCL = 1;
                    delay_i2c();
                    delay_i2c();
                    E2P_SCL = 0;
                    delay_i2c();
    
                    if(E2P_SDA == 0)
                            LED2=1;
    
                    for(i=7;i>=0;i--)
                            {
                                    E2P_SDA = ADDR_LSB^i;
                                    E2P_SCL = 0;
                                    delay_i2c();
                                    E2P_SCL = 1;
                                    delay_i2c();
                            }
    
                    E2P_SDA = 1;
                    E2P_SCL = 0;
                    delay_i2c();
                    E2P_SCL = 1;
                    delay_i2c();
                    delay_i2c();
                    E2P_SCL = 0;
                    delay_i2c();
    
                    if(E2P_SDA == 0)
                            LED3=1;
    
                    for(i=7;i>=0;i--)
                            {
                                    E2P_SDA = data_byte^i;         // data_byte is a global variable
                                    E2P_SCL = 0;
                                    delay_i2c();
                                    E2P_SCL = 1;
                                    delay_i2c();
                            }
    
                    E2P_SDA = 1;
                    E2P_SCL = 0;
                    delay_i2c();
                    E2P_SCL = 1;
                    delay_i2c();
                    delay_i2c();
                    E2P_SCL = 0;
                    delay_i2c();
    
                    if(E2P_SDA == 0)
                            LED4=1;
    
    
    }
    

Reply
  • thanks a lot for your valuable advice captain. i dont disagree with you at all, so, i am posting my commented version right away.

    /* the routine for sending a start bit for i2c initialization */
    
    void Write_Start(void)
            {
                    E2P_SDA = 1;                // sda is set high initially for a Hi to Low transition
                    E2P_SCL = 1;                // scl is made high
                    delay_i2c();                // delay of 5 micro seconds
                    E2P_SDA = 0;                // sda is driven low to complete hi to low signal
                    delay_i2c();                // delay of 5 micro seconds
                    E2P_SCL = 0;                // scl is then made low again to complete one clock period
                    delay_i2c();
            }
    
    /* this is used to write a byte onto the eeprom at location 0x5005 */
    
    void Write_to_E2P(void)
    {
        unsigned char DEV_SEL_CODE = 0x0A0;        // device select code
        unsigned char ADDR_MSB = 0x50;             // MSB of address
        unsigned char ADDR_LSB = 0x05;             // LSB of address
        unsigned char i;                           // loop variable
    
        Write_Start();                             // I2C START
    
        for(i=7;i>=0;i--)                          // transmitting from the bit 7 down to bit 0 in the loop
           {
                E2P_SDA = DEV_SEL_CODE^i;  // data bit on the sda line
                E2P_SCL = 0;               // a rising edge of the pulse to sample the sda line
                delay_i2c();
                E2P_SCL = 1;
                delay_i2c();
           }
    
           E2P_SDA = 1;                // these lines are for the 9th clock pulse when the slave pulls the sda line low
           E2P_SCL = 0;
           delay_i2c();
           E2P_SCL = 1;
           delay_i2c();
           delay_i2c();
           E2P_SCL = 0;
           delay_i2c();
    
           if(E2P_SDA == 0)           // if the acknowledge is recieved then we glow an LED used for debugging on board
                LED1=1;
    
                /* the same story follows for the rest three bytes, which are -
                the address MSB and LSB and the data byte to be transferred */
    
                    for(i=7;i>=0;i--)
                            {
                                    E2P_SDA = ADDR_MSB^i;
                                    E2P_SCL = 0;
                                    delay_i2c();
                                    E2P_SCL = 1;
                                    delay_i2c();
                            }
    
                    E2P_SDA = 1;
                    E2P_SCL = 0;
                    delay_i2c();
                    E2P_SCL = 1;
                    delay_i2c();
                    delay_i2c();
                    E2P_SCL = 0;
                    delay_i2c();
    
                    if(E2P_SDA == 0)
                            LED2=1;
    
                    for(i=7;i>=0;i--)
                            {
                                    E2P_SDA = ADDR_LSB^i;
                                    E2P_SCL = 0;
                                    delay_i2c();
                                    E2P_SCL = 1;
                                    delay_i2c();
                            }
    
                    E2P_SDA = 1;
                    E2P_SCL = 0;
                    delay_i2c();
                    E2P_SCL = 1;
                    delay_i2c();
                    delay_i2c();
                    E2P_SCL = 0;
                    delay_i2c();
    
                    if(E2P_SDA == 0)
                            LED3=1;
    
                    for(i=7;i>=0;i--)
                            {
                                    E2P_SDA = data_byte^i;         // data_byte is a global variable
                                    E2P_SCL = 0;
                                    delay_i2c();
                                    E2P_SCL = 1;
                                    delay_i2c();
                            }
    
                    E2P_SDA = 1;
                    E2P_SCL = 0;
                    delay_i2c();
                    E2P_SCL = 1;
                    delay_i2c();
                    delay_i2c();
                    E2P_SCL = 0;
                    delay_i2c();
    
                    if(E2P_SDA == 0)
                            LED4=1;
    
    
    }
    

Children
No data