DS1307 interfacing with ARM7 LPC2148 using I2C communication protocol ( code problem)

Status
Not open for further replies.

gopi the king

Junior Member level 2
Joined
Nov 20, 2012
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Coimbatore, Tamil Nadu, India
Visit site
Activity points
1,421
Hai,

now i am doing program for interfacing DS1307 with lpc2148 using I2C communication protocol. i got this code from net but it is not working properly. the output is like 00:00:00 then in next line 00-00-00. i think that program have some minute error please clear that.

or else if u have some other program kindly send me, it will be very useful for me

below program is the one i got in internet.

Code:
[syntax=c]/*
PORT 0.3-SDA LINE

PORT 0.2-SCL LINE

LCD DISPLAY
PORT 1.16-1.23 - DATA LINES

PORT 0.4- RS PIN 
PORT 0.5- RW PIN
PORT 0.6- EN PIN




*/



#include<lpc214x.h>

unsigned int val,n;
unsigned char a[8]={0};


void lcdready(void)
 	{  	
			IODIR1=0X007F0000;								/* MAKE D7 AS A INPUT PIN & D0-D6 AS OUTPUT PINS  */
	   	 	IOSET1=0x00800000;								/*MAKE D7=1*/
	   		IOCLR0=0x00000010;	 							/* RS(P0.4)=0  */
			IOSET0=0x00000020;								/* R/W(P0.5)=1  */
	   	   while((IOPIN1&0x00800000)== 0x00800000)			/* IF D7 IS HIGH STAY HERE  */
		   {		   
			   IOCLR0=0x00000040;		  				/* EN(P0.6)=0  */
			   IOSET0=0x00000040;			 	 		/* EN(P0.6)=1  */	   
		   }
	 }

void lcdcommand(void)
 		{
 			 lcdready();
			 IODIR1=0X00FF0000;				/* P1.16..23 defined as Outputs  */	 
			 IOPIN1 = n; 
			 IOCLR0=0x00000010;	   			/* RS(P0.4)=0  */
			 IOCLR0=0x00000020;				/* R/W(P0.5)=0  */
			 IOSET0=0x00000040;				/* EN(P0.6)=1  */
			 IOCLR0=0x00000040;				/* EN(P0.30)=0  */			 
		 }

void lcddatawrt(void)
		{
			lcdready();	
			IODIR1=0X00FF0000;				/* P1.16..23 defined as Outputs  */	  
			IOPIN1 = n;
			IOSET0=0x00000010;				/* RS(P0.4)=1  */
			IOCLR0=0x00000020;				/* R/W(P0.5)=0  */
			IOSET0=0x00000040;				/* EN(P0.6)=1  */
			IOCLR0=0x00000040;				/* EN(P0.6)=0  */
		 }	
void lcd_initialization(void)
		{
		//PINSEL1=0x00000000;				/*MAKE 0.28,0.29,0.30 AS I/O*/
		//PINSEL2=0x00000000;				   /*MAKE 1.16...1.23 AS I/O*/
	   	n=0x00380000;
		lcdcommand();
		n=0x000c0000;
		lcdcommand();
		n=0x00060000;
		lcdcommand();
		n=0x00010000;
		lcdcommand(); 
			   	   
		  }

void year()
{
	unsigned char s;
	
	n=0x00c60000;
	lcdcommand(); 

	s=a[6]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();

	s=a[6] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

	s=' ';
	val=s;
	n=val<<16;
	lcddatawrt();
	

}

void month()
{
	unsigned char s;
	
	n=0x00c30000;
	lcdcommand(); 

	s=a[5]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();

	s=a[5] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

	s='-';
	val=s;
	n=val<<16;
	lcddatawrt();
	

}

void date()
{
	unsigned char s;
	
	n=0x00c00000;
	lcdcommand(); 

	s=a[4]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();

	s=a[4] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

	s='-';
	val=s;
	n=val<<16;
	lcddatawrt();
	

}

void hour()
{
	unsigned char s;
	
	n=0x00800000;
	lcdcommand(); 
	
	s=a[2]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();

	s=a[2] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

	s=':';
	val=s;
	n=val<<16;
	lcddatawrt();


		

}

void min()
{
	unsigned char s;
	
	n=0x00830000;
	lcdcommand(); 
	
	s=a[1]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();

	s=a[1] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

	s=':';
	val=s;
	n=val<<16;
	lcddatawrt();
	

}

void sec()
{
	unsigned char s;
	
	n=0x00860000;
	lcdcommand();

	s=a[0]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();

	s=a[0] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

		

}

void day()
{
	unsigned char s;
	
	n=0x00890000;
	lcdcommand();

	/*s=a[]>>4 ;
	s=s+0x30;	
	val=s;
	n=val<<16;
	lcddatawrt();
	*/
	s=a[3] & 0x0f;
	s=s+0x30;
	val=s;
	n=val<<16;
	lcddatawrt();

		

}


 int main()
{
unsigned char i,j=0;

	PINSEL0= 0X00000050;
	PINSEL1= 0x00000000;				/*MAKE 0.28,0.29,0.30 AS I/O*/
	IODIR1 = 0x00FF0000;                     /* P1.16..23 defined as Outputs  */
  	IODIR0 = 0x000000f0;
	
	lcd_initialization();			//INITIALIZATION OF LCD 
		 

	
	I2C0SCLL =0X4B;				//TO SET BUAD RATE OF I2C
	I2C0SCLH=0X4B;				//TO SET BUAD RATE OF I2C
	I2C0CONSET=0X40;				//TO ENABLE I2C

	while(1)
	{

		I2C0CONSET=0X20;							// TO SEND START CONDITION
		while(I2C0STAT!=0X08 || I2C0CONSET!=0X68);
		I2C0CONCLR=0X20;


		I2C0DAT=0XD0;								//SLAVE ADDRESS + WR
		I2C0CONSET=0X08;
		while(I2C0STAT!=0X18 &&I2C0CONSET!=0X48);

		/*
		if(I2STAT!=0X18)						//IF NO ACK FROM SLAVE
		{
			
			I2C0DAT=0X00;
			I2C0CONCLR =0X08;
			while(I2STAT!=0X28 && I2C0CONSET!=0X48);
			I2C0CONCLR =0X08;

			I2C0CONSET=0X10;
			//I2C0CONCLR =0X08;
			while((I2C0CONSET&0x08)!=0X08);
			//I2C0CONCLR =0X20;
			while((I2C0CONSET&0x08)!=0X08);
			n=0x00010000;
			lcdcommand(); 
			n=0x00300000;
			lcddatawrt();
			while(1);
		}
		else;
	//	 */
		
		
		I2C0DAT=0X00;										 // DATA WRITE TO SALVE
		I2C0CONCLR=0X08;
		while(I2C0STAT!=0X28 &&I2C0CONSET!=0X48);
		
		

		/*
		if(j==0)
		{	

		I2C0DAT=0X00;										 // DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X15;										 // DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X18;										 // DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X00;										// DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X07;										// DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X11;										// DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X12;										// DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		I2C0DAT=0X10;										// DATA WRITE TO SALVE
		I2C0CONCLR =0X08;
		while(I2STAT!=0X28 && I2C0CONSET!=0X48);

		
		j++;
		}
		else;
		
		*/


		I2C0CONSET=0X20;									//REPEATED START
		I2C0CONCLR =0X08;
		while(I2C0STAT!=0X10 || I2C0CONSET!=0X68);
		I2C0CONCLR =0X20;

		

		I2C0DAT=0XD1;										//SLAVE ADDRESS + READ
		I2C0CONCLR =0X08;
		while(I2C0STAT!=0X40 && I2C0CONSET!=0X48);

		/*
		if(I2STAT!=0X40)								//IF NOT ACK FROM SLAVE
		{
			
			I2C0DAT=0X00;
			I2C0CONCLR =0X08;
			while(I2STAT!=0X28 && I2C0CONSET!=0X48);
			I2C0CONCLR =0X08;

			I2C0CONSET=0X10;
			//I2C0CONCLR =0X08;
			while((I2C0CONSET&0x08)!=0X08);
			//I2C0CONCLR =0X20;
			//while((I2C0CONSET&0x08)!=0X08);
			n=0x00010000;
			lcdcommand(); 
			n=0x00320000;
			lcddatawrt();
			while(1);
		}
		  */


		for(i=0;i<6;i++)					 		//REPEATE FOR 6 TIMES
		{
			I2C0CONCLR =0X08;							//CLEAR SI
			I2C0CONSET=0X04;							//TO SEND ACK FROM MASTER TO SLAVE
			while((I2C0CONSET&0x08)!=0X08);
			a[i]=I2C0DAT;								//TAKING THE DATA RECEIVED FROM SLAVE
		}


		I2C0CONCLR =0X0C;								//CLEAR SI AND AA BIT
		while((I2C0CONSET&0x08)!=0X08);				
		a[i]=I2C0DAT;									//TAKING THE DATA RECEIVED FROM SLAVE
											 		
		
		I2C0CONSET=0X10;								//TO SEND STOP SEQUENCE
		I2C0CONCLR =0X08;
		while((I2C0CONSET&0x10)==0X10);
		I2C0CONCLR =0X08;

	
		hour();										//TO DISPLAY HOUR
		min();										//TO DISPLAY MINS 
		sec();										//TO DISPLAY SECONDS
		date();										//TO DISPLAY DATE
		month();									//TO DISPLAY MONTH
		year();										//TO DISPLAY YEAR
		//day();
			
	}

}[/syntax]
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…