8051 interfacing with EEPROM 24c04 is not able to find the read and write data

Status
Not open for further replies.

MachaniGoutham

Newbie level 5
Joined
Nov 10, 2011
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,458
Code:
#include<reg51.h>

sbit SCL = P2^2;    /* connect to SCL pin (Clock)	  */
sbit SDA = P2^1;    /* connect to SDA pin (Data)	  */



void InitSerial(void)
{

  SCON = 0x50;   // setup serial port control

  TMOD = 0x20;   // hardware (9600 BAUD @11.0592MHZ)

  TH1 = 0xFD;   // TH1

  TR1 = 1;       // Timer 1 on

}

void Serial_TX(unsigned char a)
{		
  	SBUF=a;		 //place value  TX value
	while(TI==0);//wait untill transmission
	TI=0;

}

void MSDelay(unsigned int count)

{                     // mSec Delay 11.0592 Mhz
unsigned int i;		                  
   while(count)

    {
       i = 1105;
       while(i>0) i--;
       count--;

   }
}

void Start(void)

{				  /* making of High to low transations of SDA and SCL=High in a start condition  */

   SDA = 1;
   SCL = 1;
   
   MSDelay(1);  

   SDA = 0;
   SCL = 1;

   MSDelay(1);
   Serial_TX('S');
}


void WriteI2C(unsigned char Data)
{  	   int i; unsigned char temp[8];
  		Serial_TX('W');
     for (i=0;i<8;i++)	  /*Do for 8-bits of SDA iterations */
	 {	
         SDA = (Data & 0x80) ? 1:0;
		 SCL=1;
		 MSDelay(1);
		 SCL=0;
		 temp[i]=Data;
		 Serial_TX(temp[i]);
		 Data<<=1;
		 
      }	 
}
void Ack(void)  /*Acknowledge bit*/
{		
        SCL=1;
		SDA =0;
	    MSDelay(1);
        SCL=0;
		Serial_TX('A');
}
void  NoAck(void)      /*Reverse acknowledge bit*/
{
        SDA=1; 
        SCL=1;
		MSDelay(1);
        SCL=0;
		Serial_TX('N');
}


void Stop(void)

{

    SDA = 0;   
	SCL = 1;			  /* a low to high transation of SDA  with SCL high */
	
	MSDelay(1);
    
	SDA = 1;
		 
	MSDelay(1);
	Serial_TX('Z');
}
	 		 
int ReadI2C(unsigned char Data)
{	
   int i;unsigned char temp[8];
    Serial_TX('R'); 
  SDA = 1;

   for (i=0;i<8;i++){
		 SCL   = 1;      
		 Data<<= 1;
		 Data = (Data | SDA);    
		 SCL   = 0;
		 temp[i]=Data;
		 Serial_TX(temp[i]);
    }



return Data;
}
 

int main()
{	unsigned char EData[2];
    InitSerial();        /* Initialize serial port */
	Start();
	WriteI2C(0xA0);		/* Sending the device base address */
	WriteI2C(0x00);  	/* taken location at EEPROM */
	WriteI2C(0x06);		/* Loading value in to 00 Location */ 
    Ack();
	MSDelay(10);		 /*maintained by some delay */
	Start();
	WriteI2C(0xA0);		/* Sending the device base address */
	WriteI2C(0x00);  	/* taken location at EEPROM */
	EData[0]=ReadI2C(0XA1);	  /* reading the value and storing of Edata */
	EData[1]=ReadI2C(0X00);
	Serial_TX(EData[0]);
	Serial_TX(EData[1]);
	NoAck();
	Stop();
 
 while(1);
return(0); 
}


leave the serial tx its just for debugging how can i will get the acknowledge from EEPROM device
 
Last edited by a moderator:

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…