Skip to main content
Visitor II
April 6, 2007
Question

I2C help

  • April 6, 2007
  • 3 replies
  • 732 views
Posted on April 06, 2007 at 13:01

I2C help

    This topic has been closed for replies.

    3 replies

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:40

    Hello,

    in the following code I use the I2C0 in IRQ mode.

    I have to read the temperature from a LM75

    The device is adressed 0x91 (readmode) followed with 2 bytes.

    I analyzed the scl and sck line with the oscilloscope and the signal seem

    correct.

    The Temp sensor send a data but I always receive FF

    The port configuration reflect the following configuration:

    // ****************************************************************

    // GPIO input register setting

    // ****************************************************************

    #define GPIOIN2 0x41

    // ****************************************************************

    // GPIO output register setting

    // ****************************************************************

    #define GPIOOUT2 0x8A5A

    // ****************************************************************

    // GPIO type register setting

    // ****************************************************************

    #define GPIOTYPE2 0x3

    --------

    the code

    --------

    void main()

    {

    DeviceInit();

    /* I2C0 Configuration */

    I2C_Cmd(I2C0, ENABLE);

    SCU_APBPeriphClockConfig(__I2C0,ENABLE);

    I2C_DeInit(I2C0);

    SCU_AHBPeriphClockConfig(__VIC, ENABLE);

    VIC_DeInit();

    /* I2C0 Configuration */

    I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;

    I2C_Struct.I2C_Ack = I2C_Ack_Enable;

    I2C_Struct.I2C_CLKSpeed = 20000;

    I2C_Init(I2C0, &I2C_Struct);

    /* VIC Configuration */

    VIC_Config(I2C0_ITLine,VIC_IRQ , 1);

    VIC_ITCmd(I2C0_ITLine, ENABLE);

    I2C_ITConfig(I2C0, ENABLE);

    /*--------------------------------------------------*/

    /* Reception Phase----------------------------------*/

    /*--------------------------------------------------*/

    /*reset counters*/

    Tx_Idx = Rx_Idx = 0;

    Direction = I2C_MODE_RECEIVER;

    while(1)

    {

    Rx_Idx = 0;

    I2C_GenerateStart(I2C0, ENABLE);

    while (Rx_Idx < BUFFER_SIZE);

    /* Check if the transmitted data is read correctly */

    Delay(100000);

    }

    while(1)

    {

    }

    }

    void I2C0_IRQHandler(void)

    {

    u16 event1 = I2C0->SR1;

    u16 event2 = I2C0->SR2;

    if ( event1 & 0x01 )

    {

    // case I2C_EVENT_MASTER_MODE_SELECT: // EV5

    I2C_Send7bitAddress(I2C0, I2C_SLAVE_ADDRESS7, Direction);

    }

    else if ( event2 & 0x20 )

    {

    // case I2C_EVENT_MASTER_MODE_SELECTED: // EV6

    // Clear EV6 by set again the PE bit

    I2C0->CR |= 0x20;

    if (Direction == I2C_MODE_TRANSMITTER)

    I2C_SendData(I2C0, I2C0_Buffer_Tx[Tx_Idx++]);//EV8 just after EV6

    }

    else if ( (event1 & 0x08) && (Direction == I2C_MODE_TRANSMITTER) )

    {

    // case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8

    if ( Tx_Idx == BUFFER_SIZE )

    {

    I2C_GenerateSTOP (I2C0, ENABLE);

    Tx_Idx++;

    }

    else

    {

    I2C_SendData(I2C0, I2C0_Buffer_Tx[Tx_Idx]);

    Tx_Idx++;

    }

    }

    else if ( event1 & 0x08 )

    {

    // case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7

    I2C0_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData(I2C0);

    if ( Rx_Idx == BUFFER_SIZE-2 )

    {

    I2C_AcknowledgeConfig (I2C0, DISABLE);

    }

    if ( Rx_Idx == BUFFER_SIZE-1 )

    {

    /* Send STOP Condition */

    I2C_GenerateSTOP(I2C0, ENABLE);

    }

    }

    }

    Any suggestion is appreciated!!

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:40

    I did look at your code but here is what I can suggest to you :

    If you try read the DR register with a breakpoint, you will read FF but if you set your breakpoint after transfering the DR content to a variable, you will read the correct value.

    An other point, check to LM75 answer on the scope, if this is correct. Ensure that you did all the EVx step in order.

    Hope this will help

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:40

    Hi camelon,

    I tryed to debug the sw in different ways, I didn't check the

    I2C0 register directly but I try to see the values stored in the buffer.

    In any case I just solved the problem in the following way:

    the initial configuration suggested to me using CAPS for the GPIOIN2

    was 0x41

    I enabled the pin 2.1 as alternate input 1 and the i2c0 now it's works.

    hoping this thing could help someone!