EmbeddedRelated.com
Forums

I2C code not working for LPC2138

Started by Unknown July 9, 2012
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.