Motherboard Forums


Reply
Thread Tools Display Modes

I2C code not working for LPC2138

 
 
rdineshkumarphl@gmail.com
Guest
Posts: n/a
 
      07-09-2012, 01:15 PM
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LPC2138 I2C Frank van Eijkelenburg Embedded 3 06-28-2008 01:26 PM
I2C high speed mode and I2C range extending Michael Embedded 1 06-14-2007 07:29 PM
Devantech USB-I2C R286-USB-I2C board mInfo Embedded 0 02-16-2007 10:57 PM
I2C - Do i need to check all the states of I2C bus? abhay Embedded 3 02-01-2007 03:47 PM
RTEMS with ARM LPC2138 sam Embedded 0 12-13-2005 01:05 PM


All times are GMT. The time now is 07:50 PM.


Welcome!
Welcome to Motherboard Point
 

Advertisment