1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

I2C code not working for LPC2138

Discussion in 'Embedded' started by rdineshkumarphl@gmail.com, Jul 9, 2012.

  1. Guest

    Hi all,
    I wrote a code to read temperature from an temperature sensor through i2c,
    but, it is not passing even the start condition and becoming infinite loop in the

    while(I2C1STAT != 0x08);

    Please review my code & specify if i missed any thing. find the code below.

    #include <definitions.h>
    #include <LPC213X.H>

    void mmc_i2c_init()
    {
    put_ch('1');
    PINSEL0 |= PINSEL_MMC_I2C; // SELECTING PINS FOR THE FUNCTION OF SCL1 & SDA1
    PCONP |= (1 << 19);
    I2C1CONCLR |= I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC; //SETTING ALL THE BITS EXCEPT RESERVED BITS
    I2C1CONSET |= I2CONSET_I2EN;
    I2C1SCLH = I2C_SCLH;
    I2C1SCLL = I2C_SCLL;

    }

    int main ( )
    {
    unsigned int t, i = 0, temp_reading;
    unsigned int data_1, data_2;
    init_serial();
    mmc_i2c_init();

    //while(1)
    {

    I2C1CONSET |= 0x20; //Sending START condition

    t = I2C1STAT;
    while(t != 0x08 )
    t = I2C1STAT;


    put_ch('1');

    I2C1DAT = 0x90 | WRITE_BIT;
    I2C1CONSET = I2CONSET_AA;
    I2C1CONCLR = I2CONCLR_SIC;
    while(I2C1STAT != 0x18);

    I2C1DAT = 0x00;
    I2C1CONSET = I2CONSET_AA;
    I2C1CONCLR = I2CONCLR_SIC;
    while(I2C1STAT != 0x28);

    I2C1CONSET = 0x20;
    while(I2C1STAT != 0x10);

    I2C1DAT = 0x90 | READ_BIT; //sending slave address + read bit
    I2C1CONSET = I2CONSET_AA;
    I2C1CONCLR |= I2CONCLR_SIC;
    t = I2C1STAT;
    while(t != 0x40 && t != 0x48)
    t = I2C1STAT;
    put_ch('4');
    if(I2C1STAT == 0x48) //when NACK is received
    {
    I2C1CONSET = I2CONSET_AA | I2CONSET_STO;
    I2C1CONCLR = I2CONCLR_SIC;
    return 0;
    }

    I2C1CONSET = I2CONSET_AA;
    I2C1CONCLR = I2CONCLR_SIC;
    while(I2C1STAT != 0x50); //when data byte1 is received

    put_ch('6'); //for debugging

    data_1 = I2C1DAT;

    I2C1CONSET = I2CONSET_AA;
    I2C1CONCLR = I2CONCLR_SIC;
    while(I2C1STAT != 0x58); //when data byte2 is received

    data_2 = I2C1DAT;

    I2C1CONSET = I2CONSET_AA | I2CONSET_STO; //sending stop bit
    I2C1CONCLR = I2CONCLR_SIC;

    put_ch('8');

    temp_reading = ((data_1 << 8) | (data_2)) >> 4 ; //storing temperature data in a variable

    put_ch(0x0A);
    for (i = 16; i >= 1; i--)
    {
    if (temp_reading & ( 1 << (i-1)))
    put_ch('1');
    else
    put_ch('0');
    }

    }

    }



    Thanks & regards,
    Dinesh.
    , Jul 9, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam

    RTEMS with ARM LPC2138

    sam, Dec 13, 2005, in forum: Embedded
    Replies:
    0
    Views:
    400
  2. abhay
    Replies:
    3
    Views:
    413
    Roland Zitzke
    Feb 1, 2007
  3. mInfo
    Replies:
    0
    Views:
    448
    mInfo
    Feb 16, 2007
  4. Michael
    Replies:
    1
    Views:
    399
    Jim Granville
    Jun 14, 2007
  5. Frank van Eijkelenburg

    LPC2138 I2C

    Frank van Eijkelenburg, Jun 18, 2008, in forum: Embedded
    Replies:
    3
    Views:
    615
    Frank van Eijkelenburg
    Jun 28, 2008
Loading...

Share This Page