
#include<AT89X051.H>
#include <intrins.h>								//Includes _nop_(); function 

sbit scl = P1^7; 									//Some BitWise Definations
sbit sda = P1^6;
sbit tst = P3^7;

void iic_start (void);								//Functions Prototypes
void iic_stop (void);
void ack (void);
void no_ack (void);
void send_data (unsigned char j);
unsigned char read_data (void);
void iic_write_word (unsigned char add,unsigned char dat);
unsigned char iic_read_data (unsigned char add);
void delay (void);
void empty (void)
{;}

void main (void)
{
unsigned char dat,add;								//Two Variables dat=DATA, add=ADDRESS
tst=1;												//set tst (Which is Button)
P1=0xff;											//Making P1 High
add=0x00;											//Set EEPROM add to 0xff;
dat=0xf0;											//Set Data to 0xff
iic_write_word(add,dat);							//Write the word at location add and data is dat
P1=0x00;											//When Data Written Succesffully then make P1 low
while(1)
{
while(tst);											//Wait for tst button to touch ground
while(!tst);										//when pressed then wait for Release
add=0x00;											//Set add to 0x00 of EEPROM
dat=iic_read_data(add);								//Read the Random Read at Location add (0x00)
}
}


void iic_start (void)								//iic start routine
{
sda=1;
scl=1;
_nop_();
sda=0;
_nop_();
scl=0;
}

void iic_stop (void)								//iic stop routine
{
scl=1;
_nop_();
sda=1;
}

void send_data (unsigned char j)					//Routine for Send data placed in "j"
{
unsigned char bit_pos;
//j=0xf0;  
scl = 0; 
for(bit_pos = 0x80; bit_pos != 0; bit_pos >>= 1) // MSB first 
{ 
if (j & bit_pos) 
sda = 1; 
else 
sda = 0; 

scl = 1; 
_nop_(); 
scl = 0;
}
}

unsigned char read_data (void)						//Routine for Reading the data, and return output
{
unsigned char index,x=0x00; 
for(index = 0x80; index != 0; index >>= 1) 			// MSB first 
{ 
_nop_(); 
scl = 1; 
if (sda) 
x |= index; 
scl = 0; 
} 

sda = 1; 
_nop_(); 
scl= 1; 
_nop_(); 
scl = 0; 

return(x); 
}

void ack (void)										//Acknowledge Routine
{
sda=1;
scl=1;
//while(sda);
_nop_();
scl=0;
}

void no_ack (void)									//No Acknowledge Routine
{
sda=1;
scl=0;
//while(!sda);
_nop_();
scl=1;
}

void iic_write_word (unsigned char add,unsigned char dat)	//Routine for writing data=dat at Location=add
{
iic_start();										//start iic	
send_data(0xa0);									//send Address with Write
ack();
send_data(add);										//send the EEPROM address
ack();
send_data(dat);										//send the Data to EEPROM
ack();
iic_stop();											//stop iic
delay();
}

unsigned char iic_read_data (unsigned char add)		//Routine for writing data=dat at Location=add
{
unsigned char value;
iic_start();										//start iic
send_data(0xa0);									//send Address with Write
ack();
send_data(add);										//send the EEPROM address from Data should be collected
ack();
iic_start();										//start iic again
send_data(0xa1);									//send Address with Read
ack();			
value=read_data();									//Get the Data and store it in value
no_ack();
iic_stop();											//stop iic	
//delay();
return(value);
} 

void delay (void)
{
unsigned int x; 
for(x=0;x<=1000;x++)
empty();
}

