tweety bird
Jul 6 2009, 11:24 AM
i have been working on eeprom interfacing since last few days...
i successfully ran my project on proteus.... the eeprom read n write works fine....
but when i burnt my at89s52, the program didnt run on it as thought of... the eeprom write encounters some problems i dont know wat, but on reading back eeprom i get only garbage characters on lcd screen...
the problem seems to come after write data byte to eeprom, i dont seem to get ack from eeprom...
void mem_write(unsigned char value)
{
char j;
bitchar=value;
for(j=0;j<8;j++)
{
scl=0; // to clock in data
sda=msbc;
scl=1;
bitchar=bitchar<<1;
}
scl=0; // to receive ack from eeprom
scl=1;
scl=0;
delay(3);
}
the above code works fine in proteus but not in real project... i use pagewrite to write to eeprom.... plz help
i successfully ran my project on proteus.... the eeprom read n write works fine....
but when i burnt my at89s52, the program didnt run on it as thought of... the eeprom write encounters some problems i dont know wat, but on reading back eeprom i get only garbage characters on lcd screen...
the problem seems to come after write data byte to eeprom, i dont seem to get ack from eeprom...
void mem_write(unsigned char value)
{
char j;
bitchar=value;
for(j=0;j<8;j++)
{
scl=0; // to clock in data
sda=msbc;
scl=1;
bitchar=bitchar<<1;
}
scl=0; // to receive ack from eeprom
scl=1;
scl=0;
delay(3);
}
the above code works fine in proteus but not in real project... i use pagewrite to write to eeprom.... plz help
DavesGarage
Jul 6 2009, 8:51 PM
What part are you trying to write to? What is the part number - a link to the data sheet would also be helpful.
tweety bird
Jul 7 2009, 12:52 AM
oops.. i have been trying to write to 24c16 atmel eeprom....
first i send dev adress = 0xA0, them mmry word address = 0x00 ... then 16byte page write seq ...
start
devadd(0xa0)
memadd(0x00)
for(i=0;i<16;i++)mem_write(arr[i]);
stop
first i send dev adress = 0xA0, them mmry word address = 0x00 ... then 16byte page write seq ...
start
devadd(0xa0)
memadd(0x00)
for(i=0;i<16;i++)mem_write(arr[i]);
stop
tweety bird
Jul 7 2009, 10:46 AM
????????????????????
DavesGarage
Jul 7 2009, 11:16 AM
Is this your memory device?
http://atmel.com/dyn/products/product_card.asp?part_id=3513
???
http://atmel.com/dyn/products/product_card.asp?part_id=3513
???
DavesGarage
Jul 7 2009, 11:22 AM
tweety bird
Jul 7 2009, 11:33 AM
ya dat is the eeprom .. n dat is how i have connected it ..
tweety bird
Jul 7 2009, 11:38 AM
void mem_datawr() // starts the sequence of writing data to eeprom
{
char i;
rstart();
mem_write(dev_addw); // load dev_add into eeprom
mem_write(mem_addw);
for(i=0;i<16;i++,mem_addw++)
{mem_write(arr[arr_count++]); // writes contents of arr[16] to eeprom using pagewrite
if(sda!=0){led=0; delay(20); led=1; delay(20); }
}
stop();
delay(3);
}
above function is used to write data to eeprom .. in proteus simulator , it works perfectly... but not in my real project .. dont know y...
in my real prog, led keeps on blinkin indicating of not receiving proper ack...
((
{
char i;
rstart();
mem_write(dev_addw); // load dev_add into eeprom
mem_write(mem_addw);
for(i=0;i<16;i++,mem_addw++)
{mem_write(arr[arr_count++]); // writes contents of arr[16] to eeprom using pagewrite
if(sda!=0){led=0; delay(20); led=1; delay(20); }
}
stop();
delay(3);
}
above function is used to write data to eeprom .. in proteus simulator , it works perfectly... but not in my real project .. dont know y...
in my real prog, led keeps on blinkin indicating of not receiving proper ack...
((DavesGarage
Jul 7 2009, 11:47 AM
Next, how fast are you talking to the device? Have you measured your data stream with an o-scope?
The part has a maximum frequency of 400kHz...
Are you within this boundary?
The part has a maximum frequency of 400kHz...
Are you within this boundary?
Ajay Bhargav
Jul 8 2009, 5:35 AM
just make sure your memory is not busy in writing data to EEPROM.
give sufficient delay after writing, normally 5ms or more. try 1 byte at a time to test.
give sufficient delay after writing, normally 5ms or more. try 1 byte at a time to test.
tweety bird
Jul 8 2009, 6:05 AM
ajay i have tried like everything i can think of.. i even increased delay to 50ms n delays bw scl n sda transitions... i tried to write one byte at a time but still no success.. i will post some of my routines, plz c if u can find any anomaly.... it runs fine on proteus simulator.... can it be a possibilty that eeprom is faulty ????
void stop();
void Ack();
void Nak();
void rstart();
unsigned char arr[32];
main()
{
for(i=0;i<32;i++)arr[i]=97+i;
mem_rwmany(2,0xa0,0x00); // writes 2 pages to eeprom
mem_seqrdlcdwr(2,0xa0,0x00); // reads 2 pages n displays on lcd
}
void rstart() // high to low transition of data bus while clock is high
{
scl=0;
sda=1;
scl=1;
sda=0;
scl=0;
}
void stop() // low to high transition of data bus while clock is high
{
scl=0;
sda=0;
scl=1;
sda=1;
scl=0;
}
void Ack() // generates ack
{
scl=0;
sda = 0; // should be 0
scl = 1;
scl = 0;
sda=1; // has to be pulled high again....
}
void Nak() // generates nak
{
scl=0;
sda = 1; // shud be 1
scl = 1;
scl = 0;
}
// i2c mem code starts
void mem_rwmany(unsigned char pages,unsigned char d_add,unsigned char m_add)
// pagewrites 16 bytes to no of pages
{
unsigned char i,page_count;
dev_addw=d_add;
mem_addw=m_add;
for(i=0;i<pages;i++,page_count++) // to write all pages*16 (max 2048) locations
{
if(page_count==16){ dev_addw+=2; page_count=0; } // after every 16*16=256 locations , dev_add inc by 2 to point to nxt block
mem_datawr(); // write 16 bytes of arr as pagewrite to eeprom
}
arr_count=0;
}
void mem_datawr() // starts the sequence of writing data to eeprom
{
char i;
rstart();
mem_write(dev_addw); // load dev_add into eeprom
//if(sda!=0)lcddata('1');
mem_write(mem_addw);
for(i=0;i<16;i++,mem_addw++) mem_write(arr[arr_count++]); // writes contents of arr[16] to eeprom using pagewrite
stop();
delay(3);
}
void mem_write(unsigned char value)
{
char j;
bitchar=value;
for(j=0;j<8;j++)
{
scl=0; // to clock in data
sda=msbc;
scl=1;
bitchar=bitchar<<1;
}
scl=0; // to receive ack from eeprom
scl=1;
scl=0;
delay(3);
}
void mem_seqrdlcdwr(unsigned char pages,unsigned char d_add,unsigned char m_add)
// reads seq 32 bytes from eeprom n writes to LCD till no of pages...
{
char i,j,page_count=0;
mem_addr=m_add; // for read operation 1st set mem_add=0 so as to refer to 0000 mem location
dev_addr=d_add; // dev_add=A0 as 1st dummy write operation to be performed
matrix=0x0F;
for(i=0;i<(pages/2);i++,page_count++) // to read all (pages/2)*32 (max 64*32 = 2048) locations
{
if(on_button==0){lcdclr() ; lcdstring("terminating read"); delay(50); break; }
lcdclr();
if(page_count==8){ dev_addr+=2; page_count=0; } // after every 8*32=256 locations , dev_add inc by 2 to point to nxt block
mem_seqdatard(); // read 16 bytes of eeprom into arr_rd[32] as sequential read
for(j=0;j<32;j++){lcddata(arr[j]); arr[j]=0; }// output 32 bytes of arr_rd to LCD, to clr read array parallely
delay(100);
}
}
void mem_seqdatard() // to read 32 bytes of data using random + seq read
{
unsigned char i;
rstart();
mem_write(dev_addr); // load in dev add
mem_write(mem_addr); // load in mem add
rstart();
mem_write(dev_addr+1); // load in mem add +1 to indicate read operation from dat mem location
for(i=0;i<32;i++)
{
arr[i]=mem_read(); // read 32 bytes in arr_rd
if(i<31)Ack(); // ack till 31 bytes
else Nak(); // nak at the 32nd byte
}
stop();
mem_addr+=32; // inc mem_add by 32 as 32 bytes have been read
delay(5);
}
unsigned char mem_read() // reads a char from eeprom n returns it
{
char i;
bitchar=0;
scl=0;
for(i=0;i<8;i++)
{
bitchar=bitchar<<1;
scl=1; // to clock out data at -ve transition
scl=0;
lsbc=sda;
}
return bitchar;
}
// mem code ends
CODE:
void stop();
void Ack();
void Nak();
void rstart();
unsigned char arr[32];
main()
{
for(i=0;i<32;i++)arr[i]=97+i;
mem_rwmany(2,0xa0,0x00); // writes 2 pages to eeprom
mem_seqrdlcdwr(2,0xa0,0x00); // reads 2 pages n displays on lcd
}
void rstart() // high to low transition of data bus while clock is high
{
scl=0;
sda=1;
scl=1;
sda=0;
scl=0;
}
void stop() // low to high transition of data bus while clock is high
{
scl=0;
sda=0;
scl=1;
sda=1;
scl=0;
}
void Ack() // generates ack
{
scl=0;
sda = 0; // should be 0
scl = 1;
scl = 0;
sda=1; // has to be pulled high again....
}
void Nak() // generates nak
{
scl=0;
sda = 1; // shud be 1
scl = 1;
scl = 0;
}
// i2c mem code starts
void mem_rwmany(unsigned char pages,unsigned char d_add,unsigned char m_add)
// pagewrites 16 bytes to no of pages
{
unsigned char i,page_count;
dev_addw=d_add;
mem_addw=m_add;
for(i=0;i<pages;i++,page_count++) // to write all pages*16 (max 2048) locations
{
if(page_count==16){ dev_addw+=2; page_count=0; } // after every 16*16=256 locations , dev_add inc by 2 to point to nxt block
mem_datawr(); // write 16 bytes of arr as pagewrite to eeprom
}
arr_count=0;
}
void mem_datawr() // starts the sequence of writing data to eeprom
{
char i;
rstart();
mem_write(dev_addw); // load dev_add into eeprom
//if(sda!=0)lcddata('1');
mem_write(mem_addw);
for(i=0;i<16;i++,mem_addw++) mem_write(arr[arr_count++]); // writes contents of arr[16] to eeprom using pagewrite
stop();
delay(3);
}
void mem_write(unsigned char value)
{
char j;
bitchar=value;
for(j=0;j<8;j++)
{
scl=0; // to clock in data
sda=msbc;
scl=1;
bitchar=bitchar<<1;
}
scl=0; // to receive ack from eeprom
scl=1;
scl=0;
delay(3);
}
void mem_seqrdlcdwr(unsigned char pages,unsigned char d_add,unsigned char m_add)
// reads seq 32 bytes from eeprom n writes to LCD till no of pages...
{
char i,j,page_count=0;
mem_addr=m_add; // for read operation 1st set mem_add=0 so as to refer to 0000 mem location
dev_addr=d_add; // dev_add=A0 as 1st dummy write operation to be performed
matrix=0x0F;
for(i=0;i<(pages/2);i++,page_count++) // to read all (pages/2)*32 (max 64*32 = 2048) locations
{
if(on_button==0){lcdclr() ; lcdstring("terminating read"); delay(50); break; }
lcdclr();
if(page_count==8){ dev_addr+=2; page_count=0; } // after every 8*32=256 locations , dev_add inc by 2 to point to nxt block
mem_seqdatard(); // read 16 bytes of eeprom into arr_rd[32] as sequential read
for(j=0;j<32;j++){lcddata(arr[j]); arr[j]=0; }// output 32 bytes of arr_rd to LCD, to clr read array parallely
delay(100);
}
}
void mem_seqdatard() // to read 32 bytes of data using random + seq read
{
unsigned char i;
rstart();
mem_write(dev_addr); // load in dev add
mem_write(mem_addr); // load in mem add
rstart();
mem_write(dev_addr+1); // load in mem add +1 to indicate read operation from dat mem location
for(i=0;i<32;i++)
{
arr[i]=mem_read(); // read 32 bytes in arr_rd
if(i<31)Ack(); // ack till 31 bytes
else Nak(); // nak at the 32nd byte
}
stop();
mem_addr+=32; // inc mem_add by 32 as 32 bytes have been read
delay(5);
}
unsigned char mem_read() // reads a char from eeprom n returns it
{
char i;
bitchar=0;
scl=0;
for(i=0;i<8;i++)
{
bitchar=bitchar<<1;
scl=1; // to clock out data at -ve transition
scl=0;
lsbc=sda;
}
return bitchar;
}
// mem code ends
tweety bird
Jul 8 2009, 6:07 AM
i have included all other function declarations n other needed things... above code is most of the major code....
mem_write() is used to write one byte to eeprom
mem_read() return one byte from eeprom
mem_write() is used to write one byte to eeprom
mem_read() return one byte from eeprom
tweety bird
Jul 8 2009, 11:34 PM
ajay??? dave??? shyam??? anyone ???
Ajay Bhargav
Jul 10 2009, 8:40 AM
did you check the functions separately? are they working fine?
check on proteus if you have. see if there is any wrong transistion.
always write codes in small functional format.
check on proteus if you have. see if there is any wrong transistion.
always write codes in small functional format.
tweety bird
Jul 10 2009, 7:40 PM
ajay i have checked the codes one by one n then joined them 2gether... the program works perfect in proteus.. i get desired read write opertations to eeprom..
but it doesnt work in real life project.. i have a development kit uni51sdk.... it has 24c16 eeprom on board...
i simulated the whole kit circuit on proteus n it works fine, but on the kit it doesnt..
somehow i think byte is not being written to eeprom.....
plz help me, i dont know y it is not workin... it worked well on proteus...
but it doesnt work in real life project.. i have a development kit uni51sdk.... it has 24c16 eeprom on board...
i simulated the whole kit circuit on proteus n it works fine, but on the kit it doesnt..
somehow i think byte is not being written to eeprom.....
plz help me, i dont know y it is not workin... it worked well on proteus...

gopi
Jul 10 2009, 9:17 PM
Try to give some time delay inbetween SCL high-to-low like using,
SCL=1;
;;;;;;;
SCL=0;
(or)
SCL=1;
nop();nop();nop();
SCL=0;
SCL=1;
;;;;;;;
SCL=0;
(or)
SCL=1;
nop();nop();nop();
SCL=0;
tweety bird
Jul 11 2009, 5:04 AM
solved the problem after a lot of debugging.. phewwww,,,, :mad !dance
first i tried to locate whr the problem, after lots of debuggin it apeared dat data was being written properly but not read properly though...
but i cudnt find problem at first coz it was a weird one....
the problem was in mem_read() function...
i dont how but earlier it worked well in proteus but not in real project... then i tried some changes in the function n then it worked in both proteus n real project.....
earlier mem_read() was---
unsigned char mem_read() // reads a char from eeprom n returns it
{
char i;
bitchar=0;
scl=0;
for(i=0;i<8;i++)
{
bitchar=bitchar<<1;
scl=1; // to clock out data at -ve transition
scl=0;
lsbc=sda;
}
return bitchar;
}
i changed it to ----------------------------------
unsigned char mem_read() // reads a char from eeprom n returns it
{
char i;
bitchar=0;
for(i=0;i<8;i++)
{
bitchar=bitchar<<1;
scl=0; // to clock out data at -ve transition
delay(1);
scl=1;
lsbc=sda;
}
return bitchar;
}
and the program worked both in proteus n real project... i think it was due to clock transitions of scl...
in data sheet it was written data is clocked out on the following edge of scl, so i started with scl=1; scl=0; in loop..
but when i changed it to scl=0; delay(1); scl=1; it worked !dance
atlast my eeprom interfacing is finished............ :-) :-)
thank u guys really for helping n being der for me...
just 'thanked' all those who helped....
:bye :-) !dance
first i tried to locate whr the problem, after lots of debuggin it apeared dat data was being written properly but not read properly though...
but i cudnt find problem at first coz it was a weird one....
the problem was in mem_read() function...
i dont how but earlier it worked well in proteus but not in real project... then i tried some changes in the function n then it worked in both proteus n real project.....
earlier mem_read() was---
unsigned char mem_read() // reads a char from eeprom n returns it
{
char i;
bitchar=0;
scl=0;
for(i=0;i<8;i++)
{
bitchar=bitchar<<1;
scl=1; // to clock out data at -ve transition
scl=0;
lsbc=sda;
}
return bitchar;
}
i changed it to ----------------------------------
unsigned char mem_read() // reads a char from eeprom n returns it
{
char i;
bitchar=0;
for(i=0;i<8;i++)
{
bitchar=bitchar<<1;
scl=0; // to clock out data at -ve transition
delay(1);
scl=1;
lsbc=sda;
}
return bitchar;
}
and the program worked both in proteus n real project... i think it was due to clock transitions of scl...
in data sheet it was written data is clocked out on the following edge of scl, so i started with scl=1; scl=0; in loop..
but when i changed it to scl=0; delay(1); scl=1; it worked !dance
atlast my eeprom interfacing is finished............ :-) :-)
thank u guys really for helping n being der for me...
just 'thanked' all those who helped....
:bye :-) !dancegopi
Jul 11 2009, 5:23 AM
Good tweety bird. I am thinking to donate a project on 8051 with a interface of at24c256, ds1307 & smard card. I am in the middle of the project. I named it as "RF ID based attendence system"
tweety bird
Jul 11 2009, 6:25 PM
nice gopi.... best of luck :bye
gopi
Jul 11 2009, 10:07 PM
Thank you...........
