#include <REG51.h>
#include <generic1.h>

//---------------------------------Defining Macros---------------------------------------------------------------------------
sbit sclk = P1^0;	
sbit sda = P1^1;	
sbit GREEN_LED = P3^6;
sbit YELLOW_LED = P3^7;

//---------------------------------------------------------------------------------------------------------------------------

//******************************FXN prototypes******************************/
void start_s_eeprom();
void send_byte_s_eeprom(char);
char get_byte_s_eeprom();
void stop_s_eeprom();
void send_to_mem(char, char);
unsigned int get_from_mem(char);
void wait();
void acknowledge();

//***************************************************************************/

//*********************fxn to send data to serial eeprom*********************/
// give the adress and the data as the parametrs to the fxn

 void send_to_mem(char s_address, char s_data)
 {
    start_s_eeprom();             // sending start condition to eeprom 
    send_byte_s_eeprom(0XA0);     // A0 = 10100000 = sending device address word for write
    acknowledge();
    send_byte_s_eeprom(s_address); // sending data address
    acknowledge();
    send_byte_s_eeprom(s_data);   // sending data 
    acknowledge();
    stop_s_eeprom();              // sending stop condition to eeprom
	acknowledge();
	return;
 }
//********************************************************************************************/

//*********************fxn to get the data back frm the serial eeprom****************/
// just give the adress from where the data is to be retrieved
 unsigned int get_from_mem(char s_address)
 {
    char i = 0;
//-------dummy write seq----+ word address------------------------------------
    start_s_eeprom();             // sending start condition to eeprom 
    send_byte_s_eeprom(0XA0);     // sending A0 = 10100000 = device address word for write
    acknowledge();
    send_byte_s_eeprom(s_address); // sending data address
    acknowledge();
//----------------dummy over----------------------------------------------------
    start_s_eeprom();
    send_byte_s_eeprom(0XA1);     // sending A1 =10100001 = device adress word for read
    acknowledge();
    i = get_byte_s_eeprom(); // sending data 
    acknowledge();
    stop_s_eeprom();              // sending stop condition to eeprom
	acknowledge();

    return(i); 
 }
//************************************************************************/

//*********************fxn to transmit a byte to the eeprom *****************************/
//this fxn just send the 8 bits serialy on the SDA line
//this fnx does not store the data in eeprom but just transmits it, this fxn is used by the storing fxn
//just pass the byte to be transmitted as parameter to this fxn
 void send_byte_s_eeprom(char s_byte)
 {
    char temp = s_byte;
    char i ;

	for(i = 7 ; i >= 0 ; i--)
    {
 
        temp = s_byte;
        temp = temp >> i;
        temp = temp & 0X01;

     
        if(temp == 0)
            sda =   0;
        else
            sda =   1;

		sclk   =   1;
        wait();
        sclk   =   0;
	   
    }
	return;
 }
//************************************************************************/

//*********************fxn to receive 8 bits serialy from sda line***************
// this is not a fxn to read from eeprom
// it just receives 8 bits serialy and retuns the byte received to the calling fxn
 char get_byte_s_eeprom()
 {
    char temp, temp_h, i;
    temp = 0;
    temp_h = 1;	

    sda = 1;    // making SDA as input pin for microcontroller
  	sclk	=	0;

    for(i = 7; i >=0 ; i--)
    {
        sclk = 1;
     	if(sda == 1)
        {
            temp = temp | temp_h<<i ;                      
        }
        wait();
		sclk = 0;

    }
    
    sclk = 0;
    return(temp);
 }
//************************************************************************/

//************************fxn to send the start condition*****************************/
 void start_s_eeprom()
 {
    sda     =   1;
    sclk    =   1;
    wait();
	sda     =   0;
	sclk    =   0;  
	return; 
 }
//************************************************************************/

//**************************fxn to send stop condition**********************/
 void stop_s_eeprom()
 {
    sda     =   0;
    sclk    =   1;
    wait();
    sda     =   1;
    sclk    =   0;
	return;   
 }
//**************************************************************************************************************/

//**************************fxn for acknowledging the eeprom***************************************************
// this fxn actualy does not read the acknowledge signal
// it just waits for sufficient time and assumes that the eeprom has given tha ack by the time the wait gets over
 void acknowledge()
 {
 	sclk = 1;
   	wait();
	sclk = 0;
	return;
 }
//**************************************************************************************************************/

//*************************a small delay fxn to ensure the line settels down after transition********************
 void wait()
 {
    char i;
    for(i=0;i<=20;i++)
		i++;
    return;
 }
//**************************************************************************************************************/

void main()
{
char COMPARING_BYTE = 0,ADDRESS_=0x11;
while(1)
{
send_to_mem(0x11,0XAA);//void send_to_mem(char address, char data);
usec_wait(10);
COMPARING_BYTE = get_from_mem(0x11);
if(COMPARING_BYTE == 0xAA)
GREEN_LED = 0;
else
YELLOW_LED = 0;
sec_wait(1);
GREEN_LED = 1;
YELLOW_LED = 1;
send_to_mem(0x11,0X00);
COMPARING_BYTE = 0;
sec_wait(1);
}
}insert code here