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. Advertisements

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. sam

    RTEMS with ARM LPC2138

    sam, Dec 13, 2005, in forum: Embedded
    Replies:
    0
    Views:
    519
  2. Frank van Eijkelenburg

    LPC2138 timers

    Frank van Eijkelenburg, Jun 29, 2006, in forum: Embedded
    Replies:
    9
    Views:
    1,093
    Frank van Eijkelenburg
    Jul 1, 2006
  3. Xarion
    Replies:
    3
    Views:
    729
    Donald
    Nov 24, 2006
  4. abhay
    Replies:
    3
    Views:
    717
    Roland Zitzke
    Feb 1, 2007
  5. mInfo
    Replies:
    0
    Views:
    741
    mInfo
    Feb 16, 2007
  6. abhay
    Replies:
    3
    Views:
    1,000
    Tom Lucas
    Apr 4, 2007
  7. Michael
    Replies:
    1
    Views:
    727
    Jim Granville
    Jun 14, 2007
  8. Frank van Eijkelenburg

    LPC2138 I2C

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