problem when writing to odd address in eeprom

Status
Not open for further replies.

freemanantony

Member level 4
Joined
May 19, 2010
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chennai, India
Activity points
2,005
hai all
i am interfacing 8051 with eeprom using i2c .itz getting hanged when iam trying to write to odd address in eeprom. can anyone help me?the code is getting hanged @eeprom_write(0x00,0x05, 0x2E);when i wrote 3E to address 0x00, it is getting read as FE.
what can be the problem can anyone help me?
 

Try to read from a location that you are sure about what is written in. This will help you determine if the problem is in the read procedure or in the write procedure.
 
thank you sivaram my code is below
#include<reg51.h>
#include<stdlib.h>
#include<stdio.h>


sbit SDA=P3^5;
sbit SCL = P3^4;
unsigned char tempdata;
unsigned char check=0x00;
void sent (unsigned char dat);

void delay()
{
unsigned char i;
for(i=0x00;i<0xFF;i++);
}

void longdelay(void)
{
int h,s;
for(s=0;s<0x50;s++)
for(h=0;h<0xFF;h++);

}


void scl_high()
{
hg:
SCL=1;

if(SCL!=1)
goto hg;
}

void start()
{
SDA=1;
scl_high();
SDA=0;
delay();
SCL=0;
}

void stop()
{
SDA=0;
scl_high();
SDA=1;
}

void write (unsigned char data1)
{
unsigned char i;
for(i=0;i<8;i++)
{
// SCL=0;
if (data1 & 0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
scl_high();
data1 = data1 << 1;
delay();
SCL= 0;
}
sent(0x30+check++); //1,3 6 9 b e n

//SDA = 1;
delay();
scl_high();

sa:
if (SDA != 1)
{
goto sb;
sent('k');
}
else
{
goto sa;
sent('h');
}
//sent(0x30+check++); //2 4 7 a c f
sb:
delay();
SCL=0;
delay();
sent('z');
}

void read()
{
unsigned char j;
unsigned char tp;
unsigned char dat;
SDA = 1;
for(j=0;j<8;j++)
{
scl_high();
tp = SDA;
if (tp==1)
dat = (tempdata | 0x01);
else
dat = (tempdata & 0xFE);
tempdata = dat<<1;

SCL = 0;
delay();
delay();
delay();
}
sent(0x30+check++);
scl_high();
SCL=0;
sent(0x30+check++);
sent('y');
}

void eeprom_write(unsigned char address1,unsigned char address2, unsigned char dat)
{
start();
sent(0x30+check++); //0 c
write(0xA2);//1 d
write(address1);//2 e
sent(0x30+check++);//3 f
write(address2);//4 g
sent(0x30+check++);//5 h

write(dat);//6 i
stop();
}


void eeprom_read(unsigned char address1,unsigned char address2)
{
start();
write(0xA2);//7 j
sent(0x30+check++); //8 k
write(address1); //9 l
sent(0x30+check++); //10 m
write(address2);//11 n
sent('x');
sent(0x30+check++);//12

stop();
sent(0x30+check++);//13
start();
write(0xA3);//14
sent(0x30+check++); //15
read();
sent(0x30+check++);//B
stop();
}

void sent (unsigned char dat)
{
SBUF = dat;
while(!TI);
TI=0;
}

void send_data()
{
unsigned char R;
unsigned char J;

R = tempdata & 0xF0;
R = R >> 4;
R = R + 0x30;
sent (R);
J = tempdata & 0x0F;
J = J + 0x30;
sent (J);
}

void main(void)
{
//unsigned char address ;
//unsigned char dat ;
EA=0;
longdelay();
longdelay();
longdelay();
longdelay();
TMOD = 0X20;
SCON = 0X50;
TH1 = 0XFD;
TR1=1;

while(1)
{

eeprom_write(0x00,0x00,0x3E);
sent(0x01);
eeprom_read(0x00,0x00);
sent(0x02);
send_data(); //FE
eeprom_write(0x00,0x01, 0x2E);
eeprom_read(0x00,0x01);
send_data();
eeprom_write(0x00,0x08, 0xEA);
eeprom_read(0x00,0x08);
send_data();
eeprom_write(0x0F,0xFF, 0XFE);
eeprom_read(0x0F,0xFF);
send_data();

sent('E');
sent('N');
sent('D');
sent(' ');
}
} and i am getting ouput on hyperterminal as
01z2z34z56z7z89z:;zx<=>z?@AyB?>CDzEzFGzHIzJzKLzMN

---------- Post added at 08:25 ---------- Previous post was at 08:24 ----------

thank you seadolphine
 

i am getting ouput on hyperterminal as
01z2z34z56z7z89z:;zx<=>z?@AyB?>CDzEzFGzHIzJzKLzM N
Is this what you've written?

Try the following steps:
1- Read the EEPROM data several times and make sure that you have consistent results.
2- If the results were consistent, then your reading procedure is most probably working. Now, try to write a specific address and read it.
3- Keep trying to write a specific address and read. If you succeeded, then you can write the whole address space.

Hope this helps.
 
i would suggest you to write and read only once... instead of while(1) { code }

code while(1); would be better to test with changing data everytime, and see what is the consistency, then you can do as suggested by seadolphine.....

---------- Post added at 13:25 ---------- Previous post was at 13:25 ----------

I also feel there is timing issue in the program... i will check it and tell........
 
thank you shivaram and seadolphine. yes thats supposed to be the output except for?> and it was n't supposed to be stopped at N.

---------- Post added at 09:41 ---------- Previous post was at 09:11 ----------

when i tried to write and read once also i am getting wrong answer. when i wrote 0x3E @0x00, its getting read as 0xFE
 

I suppose you are using KEIL IDE... can you simulate the write and read operation by monitoring the watch variable and the SDA line...

for read simulation store a data as0x3e and execute read operation and see if the data is getting read properly,,, do step by step execution and check...
 
sorry shivaram the problem persisits.in the next hex download itz reading 0x3E as 0x5C

you might be reading some other memory locations instead of that particular memory location

---------- Post added at 05:21 ---------- Previous post was at 05:20 ----------

write and read according to I2C protocol diagrams as they are mentioned in EEPROM datasheet
 
hi
my code is trying to write and read like this
void write (unsigned char data1)
{
unsigned char i;
for(i=0;i<8;i++)
{
if (data1 & 0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
scl_high();
data1 = data1 << 1;
delay();
SCL= 0;
}
delay();
scl_high();

sa:
if (SDA != 1)
{
goto sb;
}
else
{
goto sa;

}
sb:
delay();
SCL=0;
delay();

}

void read()
{
unsigned char j;
unsigned char tp;
unsigned char dat;
SDA = 1;
tempdata=0x00;
for(j=0;j<8;j++)
{
scl_high();
tp = SDA;
if (tp==1)
dat = (tempdata | 0x01);
else
dat = (tempdata & 0xFE);
tempdata = dat<<1;

SCL = 0;
delay();
delay();
delay();
}

scl_high();
SCL=0;


}

void eeprom_write(unsigned char address1,unsigned char address2, unsigned char dat)
{
start();

write(0xA2);
write(address1);
write(address2);

write(dat);
stop();
}


void eeprom_read(unsigned char address1,unsigned char address2)
{
start();
write(0xA2);
write(address1);
write(address2);

stop();
start();
write(0xA3);
read();
stop();
}

void main(void)
{
EA=0;
longdelay();
longdelay();
longdelay();
longdelay();
TMOD = 0X20;
SCON = 0X50;
TH1 = 0XFD;
TR1=1;



eeprom_write(0x00,0x00,0x3E);
eeprom_read(0x00,0x00);
while(1)
{
send_data();
}
}, is there any thing wrong in this can anyone help me?when i changed eeprom ic first it was reading 0x3e correctly, then after second hex download itz not reading 0x3e
 
Last edited:

keep some delay in EEPROM memory read function after writing the memory address to the eeprom

i.e,
void eeprom_read(unsigned char address1,unsigned char address2)
{
start();
write(0xA2);
write(address1);
write(address2);

stop();

delay();//keep some delay here

start();

write(0xA3);
read();
stop();
}
 

thank you embedded partner for your time, my code is working with microchips 24c01 which is using single word address but its not working with atmel's AT24c256 which is using double word address
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…