#include <AT89x51.h>
#include<intrins.h>
sbit sda = P1^0;
sbit clk = P1^1;
int i;
void EEPROM_Write(char address,char dat)
{
char device = 0xA0;
sda = 1;
clk = 1;
_nop_();
sda = 0;
clk = 0;
for (i = 0; i<8; i++)
{
clk = 0;
if(device & 0x80)
sda = 1;
else
sda = 0;
clk = 1;
device = device << 1;
}
clk = 0;
_nop_();
clk = 1;
_nop_();
if(!sda)
{
clk = 0;
_nop_();
clk = 1;
_nop_();
clk = 0;
for (i = 0; i<8; i++)
{
clk = 0;
if(address & 0x80)
sda = 1;
else
sda = 0;
clk = 1;
address = address << 1;
}
if(!sda)
{
clk = 0;
_nop_();
clk = 1;
_nop_();
clk = 0;
P2 = 0x29;
for (i = 0; i<8; i++)
{
clk = 0;
if(dat & 0x80)
sda = 1;
else
sda = 0;
clk = 1;
dat = dat << 1;
}
if(!sda)
{
clk = 0;
_nop_();
clk = 1;
_nop_();
clk = 0;
}
sda = 0;
_nop_();
clk = 1;
sda = 1;
_nop_();
clk = 0;
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
char EEPROM_Read(char address)
{
char device = 0xA1;
char readvalue;
sda = 1;
clk = 1;
_nop_();
sda = 0;
clk = 0;
for (i = 0; i<8; i++)
{
clk = 0;
if(device & 0x80)
sda = 1;
else
sda = 0;
clk = 1;
device = device << 1;
}
clk = 0;
_nop_();
clk = 1;
_nop_();
if(!sda)
{
clk = 0;
_nop_();
clk = 1;
_nop_();
clk = 0;
for (i = 0; i<8; i++)
{
clk = 0;
if(address & 0x80)
sda = 1;
else
sda = 0;
clk = 1;
address = address << 1;
}
if(!sda)
{
clk = 0;
_nop_();
clk = 1;
_nop_();
clk = 0;
for (i = 0; i<8; i++)
{
clk=1;
_nop_();
if(sda == 1)
readvalue++;
clk=0;
readvalue = readvalue<<1;
}
if(!sda)
{
clk = 0;
_nop_();
clk = 1;
_nop_();
clk = 0;
}
sda = 0;
_nop_();
clk = 1;
sda = 1;
_nop_();
clk = 0;
}
}
return (readvalue);
}
void main ()
{
char address = 0x05;
char dat = 0x68;
char read;
P0 = 0;
P1 = 0;
P2 = 0;
P3 = 0;
EEPROM_Write(address,dat);
read = EEPROM_Read(address);
if (read == dat)
P1_5 = 1;
while (1)
{
}
}