Skip to main content
Visitor II
May 24, 2006
Question

I2C routines help !!!

  • May 24, 2006
  • 1 reply
  • 674 views
Posted on May 25, 2006 at 01:30

I2C routines help !!!

    This topic has been closed for replies.

    1 reply

    jsayavongAuthor
    Visitor II
    May 24, 2006
    Posted on May 25, 2006 at 01:30

    Could some experts in this forum kindly

    take a look at my I2C routines below.

    The problems are:

    config_I2C1() ..... not sure if this correct

    write_I2C_configuration_PCA9555() ..... write the wrong value !!!

    read_I2C_PCA9555() ..... my system hang as soon as the

    function is called !!!

    Here is the code:

    ////////////////////////////////////////////////////////////////////////

    void config_I2C1(void)

    {

    GPIO_Config(GPIO0, I2C_SCL|I2C_SDA, GPIO_AF_OD); //Open-drain output for SCL (P0.2), SDA (P0.3)

    I2C_Init(I2C1); //Initialize I2C1

    I2C_FCLKConfig(I2C1); //Config I2C freq. according for FCLK freq.

    I2C_OnOffConfig(I2C1, ENABLE); //Enable I2C Peripheral

    I2C_SpeedConfig(I2C1,90000); //Select I2C Clock speed = 100khz

    I2C_AcknowledgeConfig(I2C1, ENABLE); //Enable Acknowledge feature

    }

    ///======================================================//

    void write_I2C_configuration_PCA9555(void) // Set both PORTS as INPUT --- Send 4-bytes

    {

    I2C_STARTGenerate(I2C1, ENABLE); //start condition

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_SB) == RESET); //Check Start bit Flag

    I2C_AddressSend(I2C1, 0x40, I2C_Mode7, I2C_TX); //Send PCA9555 ADX Write = 0x40

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_ENDAD) == RESET); //Check End-of-address xmit

    I2C_FlagClear(I2C1, I2C_ENDAD); //Clear Flag

    I2C_ByteSend(I2C1, 0x06); //Send CMD 0x06 -- Port Configuration

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_BTF) == RESET); //Check byte-transfer-finish flag

    I2C_ByteSend(I2C1, 0xff); //Send 0xff -- Set Port0 as Input

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_BTF) == RESET); //Check byte-transfer-finish flag

    I2C_ByteSend(I2C1, 0xff); //Send 0xff --Set Port1 as Input

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_BTF) == RESET); //Check byte-transfer-finish flag

    I2C_STOPGenerate(I2C1,ENABLE); //Stop Condition

    }

    ////=====================================================//

    void read_I2C_PCA9555(void)

    {

    I2C_STARTGenerate(I2C1, ENABLE); //start condition

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_SB) == RESET);

    I2C_AddressSend(I2C1, 0x40, I2C_Mode7, I2C_TX); //Send PCA9555 ADX Write = 0x40

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_ENDAD) == RESET);

    I2C_FlagClear(I2C1, I2C_ENDAD);

    I2C_ByteSend(I2C1, 0x00); //Send CMD 0x00 -- Port0 Input Register

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_BTF) == RESET);

    I2C_STARTGenerate(I2C1, ENABLE); //Re-start condition

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_SB) == RESET);

    I2C_AddressSend(I2C1, 0x41, I2C_Mode7, I2C_RX); //Send PCA9555 ADX Read = 0x41

    while (I2C_FlagStatus (I2C1,DIRECT,I2C_ENDAD) == RESET);

    I2C_FlagClear(I2C1, I2C_ENDAD);

    PCA9555_port0 = I2C_ByteReceive(I2C1); //Receive 1st Byte (Port0 of PCA9555)

    PCA9555_port1 = I2C_ByteReceive(I2C1); //Receive 2nd Byte (Port1 of PCA9555)

    I2C_AcknowledgeConfig(I2C1, DISABLE); //none-ACK

    I2C_STOPGenerate(I2C1,ENABLE); //Stop Condition

    ///========================================================//