if you are getting confused by the material in the web, then it is better to read data sheet of the eeprom AT24c04 to clarify your doubts. If you tell what is your doubt we can clarify it instead of telling it is confusing.
---------- Post added at 16:36 ---------- Previous post was at 15:24 ----------
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
//*****************************************
#define data_wt 0xa0 //EEPROM address to write the data
#define data_rd 0xa1 //EEPROM address to read the data
//*****************************************
//*****************************************
void lcd_cmd(unsigned char); //To write the commands to the LCD
void lcd_data(unsigned char); //To write the data to the LCD
void msdelay(unsigned int); //Delay for LCD
void start_bit(); //Condition for start bit to the I2C
void stop_bit(); //Condition for stop bit to the I2C
void slave_add(unsigned char); //To send bit by bit data to the slave
unsigned char data_read(); //To read the data from EEPROM and display on LCD
//*****************************************
sbit sda=P2^1; //SDA line
sbit scl=P2^0; //SCL line
sbit rs=P1^0; //LCD Register select
sbit en=P1^1; //LCD Enable
//*******************************************
//*******************************************
void main()
{
unsigned char lcd_cmds[]={0x28,0x0f,0x01,0x06,0x80}; //Commands for the 4 line LCD
unsigned char temp,ch,j=16;
unsigned char addr=0x10;
unsigned char data_to_wt[]="WELCOME"; //Data to store in EEPROM
unsigned int num=0,i=0;
unsigned char p1='1',p2='2',p3='3',k1,k2,k3;
unsigned char a[]="pwd correct",b[]="aces denied";
//*****************************************************
for(i=0x0;i<0x5;i++) //To give LCD commands
{
temp=lcd_cmds
;
lcd_cmd(temp);
msdelay(2);
}
//*****************************************************
lcd_data('s');
lcd_cmd(0x80);
for(i=0;i<7;i++) //To give data to EEPROM
{
start_bit();
slave_add(data_wt);
slave_add(i);
slave_add(data_to_wt);
stop_bit();
msdelay(2);
msdelay(2);
}
msdelay(2);
//*****************************************************
msdelay(2);
for(i=0;i<7;i++) //To read data from EEPROM
{
start_bit();
slave_add(data_wt);
slave_add(i);
start_bit();
slave_add(data_rd);
ch=data_read();
stop_bit();
str=ch;
msdelay(2);
msdelay(2);
}
msdelay(2);
//******************************************************
for(i=0;i<7;i++) //To read data from EEPROM
{
lcd_data(str);
}
while(1);
}
else
{
lcd_cmd(0xc0);
for(i=0;i<=10;i++)
lcd_data(b);
}
msdelay(2);
//*****************************************************
}
//******************************************************
void lcd_cmd(unsigned char cmd) //LCD command
{
unsigned int cmd1;
cmd1=((cmd&0xf0)>>2);
P1=cmd1;
rs=0;
en=1;
msdelay(2);
en=0;
cmd1=((cmd&0x0f)<<2);
P1=cmd1;
rs=0;
en=1;
msdelay(2);
en=0;
msdelay(2);
}
//******************************************************
void lcd_data(unsigned char d1) //LCD data
{
unsigned int d2;
d2=((d1&0xf0)>>2);
P1=d2;
rs=1;
en=1;
msdelay(2);
en=0;
d2=((d1&0x0f)<<2);
P1=d2;
rs=1;
en=1;
msdelay(2);
en=0;
msdelay(2);
}
//*****************************************************
void start_bit() //Start bit condition
{
sda=1;
scl=1;
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}
//*****************************************************
//*****************************************************
void stop_bit() //Stop bit condition
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}
//******************************************************
void slave_add(unsigned char add)
{
unsigned char t,i;
t=add;
for(i=0;i<8;i++)
{
t=(t&0x80);
if(t!=0)
sda=1;
else
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
t=add<<1;
add=t;
}
sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
msdelay(2);
}
//*******************************************************
unsigned char data_read()
{
unsigned char i,x=0;
unsigned char y;
y=0;
sda=1;
for(i=0;i<8;i++)
{
scl=0;
_nop_();
_nop_();
_nop_();
scl=1;
if(sda==1)
x=x|0x01;
y=x;
x=x<<1;
scl=0;
}
sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
msdelay(2);
return;
}
//*******************************************************
void msdelay(unsigned int itime)
{
unsigned int i, j;
for(i=0; i<=itime; i++)
for(j=0; j<=1275; j++);
}