I2C Configuration , Read and Write

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi,

   

i have configured the controller as I2C master and communicating with two slaves(RTC and EEPROM).

   

am not able to read the data from EEPROM (I have written data to the 0x0002 address).

   

please let me know about I2C configuration and Read and write function use.

   

 

   

 int main()
{
    
    CyGlobalIntEnable; /* Enable global interrupts. */

    I2CMasterIntilization();
    g_ucEEPROMWriteBuffer[0] = 0x00;
    g_ucEEPROMWriteBuffer[1] = 0x02;
    g_ucEEPROMWriteBuffer[2] = 0x05;
    g_ucEEPROMWriteBuffer[3] = 0x45;
    while(1)
    {
        WriteDataToEEPROM(3);
        g_ucEEPROMWriteBuffer[0] = 0x00;
        g_ucEEPROMWriteBuffer[1] = 0x02;
        ReadDataFromEEPROM(2);
    }

   

}

   

 

   

void I2CMasterIntilization(void)
{
    I2C_Master_Start();
}

   

void WriteDataToEEPROM(char chDataByte)
{
         I2C_Master_I2CMasterWriteBuf(EEPROM_SLAVE_ADDRESS,(unsigned                             char*)g_ucEEPROMWriteBuffer,chDataByte,I2C_Master_I2C_MODE_COMPLETE_XFER);
    while((I2C_Master_I2CMasterStatus() & I2C_Master_I2C_MSTAT_WR_CMPLT) == LOW)
    {
    }
    CyDelay(3);
}

   

void ReadDataFromEEPROM(char unNoOfDataByteNeedToReceive)
{
   
    I2C_Master_I2CMasterWriteBuf (EEPROM_SLAVE_ADDRESS,(unsigned char     *)g_ucEEPROMWriteBuffer,2,I2C_Master_I2C_MODE_COMPLETE_XFER);
    //CyDelay(1000);

   


    while((I2C_Master_I2CMasterStatus() & I2C_Master_I2C_MSTAT_WR_CMPLT) == LOW)
    {
    }

   


    I2C_Master_I2CMasterReadBuf(EEPROM_SLAVE_ADDRESS,(unsigned char *)g_ucEEPROMReadBuffer,unNoOfDataByteNeedToReceive,I2C_Master_I2C_MODE_REPEAT_START);
    while((I2C_Master_I2CMasterStatus() & I2C_Master_I2C_MSTAT_RD_CMPLT) == LOW)
    {
    }
}

   

 

   

if I use I2C_Master_I2C_MODE_NO_STOP while writing data from master , then clock will become low after transmitting data (SCL line is low). I used I2C_Master_I2C_MODE_COMPLETE_XFER to write the data , then wring is working fine and SCL and SDC lines are high after writing data to EEPROM.

   

 

   

kindly let me know about API's which I used in my coding.

   

please find the attached I2C module setting image. 

   

 

   

Regards,

   

Ambarish BH 

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The I2C high-level APIs must be used in this way:

   

Writing to slave Count bytes
I2C_MasterWriteBuf(SlaveAddress,DataPtr,Count,I2C_MODE_COMPLETE_XFER);

   

Reading from Slave sending address first:
I2C_MasterWriteBuf(SlaveAddress,&EEPromAddress,2,I2C_MODE_NO_STOP);
I2C_MasterReadBuf(SlaveAddress,DataPtr,Count,I2C_MODE_REPEAT_START);

   


Bob

View solution in original post

0 Likes
11 Replies