Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C bus communication code... help needed

Status
Not open for further replies.

saur

Member level 2
Joined
Feb 23, 2012
Messages
45
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,621
This is the code I'm using to communicate with a temperature sensor IC LM75A, with in built A/D converter.

Slave address is 0x90.

Code:
while(1)
{
	GLCD_DisplayString(5,10,1,"A");
LPC_I2C0->CONCLR = 0x08;					/*clear SI flag*/

LPC_I2C0->CONSET = 0x60;						/*Start condition has been transmitted*/

while(LPC_I2C0->CONSET & 0x08 != 0x08);/*check if SI flag is set*/
i=0;
	

while(LPC_I2C0->STAT != 0x08);/*CHECK if Start condition sent using STAT staus register*/

	LPC_I2C0->DAT = 0x91;		/*write to slave address*/ 
	
	LPC_I2C0->CONCLR = 0x08;		/*clear status flag SI*/
	
	while(LPC_I2C0->CONSET & 0x08 != 0x08);/*wait till SI flag is set as it indicates a change of status*/
	LPC_I2C0->CONCLR = 0x20;			/* clear the start flag*/

	while(LPC_I2C0->STAT != 0x10);/*check if restart condition is set*/
	
                LPC_I2C0->DAT = 0x91;	/* send slave address with direction (read or write)*/
	
                LPC_I2C0->CONCLR = 0x08;/*clear SI flag*/
	
                while(LPC_I2C0->CONSET & 0x08 != 0x08);/* wait till SI flag is set again on status register(STAT) change*/
	
                LPC_I2C0->CONCLR = 0x20;/*clear the start flag*/
	
                while(LPC_I2C0->STAT != 0x40);/*check if slave address has been sent with read direction*/
	
                LPC_I2C0->CONSET = 0x04;		/*set the acknowledge flag*/
	
                LPC_I2C0->CONCLR = 0x28;		/*clear start flag and SI flag*/
	
                n=LPC_I2C0->DAT;						/* read data from DAT register*/
	
	k=n*8;

	k &= 0xFBFF;						/*clear MSB bit of received data*/

	while(LPC_I2C0->CONSET & 0x08 != 0x08);/* wait till SI flag is set again on status register(STAT) change*/

	while(LPC_I2C0->STAT != 0x50);		/*wait till MSB has been received and acknowledgement has been sent*/ 

	LPC_I2C0->CONCLR = 0x04;				/*clear acknowledge flag*/

	LPC_I2C0->CONCLR = 0x28;		/*clear start and SI flag*/

	n=LPC_I2C0->DAT;		/*receive LSB byte*/

	k =k+(n/32);

	GLCD_DisplayGraph(k);	/*a function used to place a point on the LCD screen according to the temperature*/

	kol++;

		if(kol == 319)

		{

			kol = 0;
		}

//		delay(200);

	while(LPC_I2C0->CONSET & 0x08 != 0x08);

               LPC_I2C0->CONCLR = 0x08;     /*clear SI flag*/

              LPC_I2C0->CONSET = 0x60;      /*set the start flag for repeated start*/

              while(LPC_I2C0->CONSET & 0x08 != 0x08);
}

void GLCD_DisplayGraph(unsigned long int b)
{
	unsigned int l,c;
	
                float j,k;
	
                k=b/8;						/*k is the actual temperature as temperature is decimal/8  */
	j=0.25;						/*bcoz 48/192=0.25, where 192=240-48*/
	
                if(b/8 >48)
	{
		GLCD_DisplayString(8,19,1,"X");
	}
	else
	{
		l=k/j;
		
                                fb[(kol*PHYS_XSZ)+215-l]=Color[0];
		
                               GLCD_DisplayString(5,1,1,"Q");
	}
}

I've the doubt that, whether there is a problem with my GLCD_DisplayGraph function or the I2C communication code, as it displays a straight line on the LCD even if the temperature changes.

So, either I'm only able to read the value once and displaying it again and again leading to a straight line, or I'm not able to read a value and printing some garbage value or my graph code is wrong.

I've made the code for the graph for a screen of size 240*320. Since the top and bottom 16 lines are used for characters, I've limited my no. of lines to 192 and so restricted my temperature to 48 degree celcius.
That is the reason I've taken j=0.25

Thanks for any help regarding the code.

I'm working on MCB1800 kit by Keil.

Thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top