adnansarwar2050
Member level 2
- Joined
- Oct 12, 2011
- Messages
- 52
- Helped
- 7
- Reputation
- 14
- Reaction score
- 7
- Trophy points
- 1,288
- Location
- Islamabad
- Activity points
- 1,589
i am using 24c02 eeprom for the first time. i have coded for writing and reading data but the data i am reading is not matching with that i have written. Please check my code where is the mistake. i am using 89c51.
Code:
#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)
{
}
}
Last edited by a moderator: