free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Rickey's World :: Discussion Forums :: Discuss and Learn :: 8051 Discussion Forum
 
<< Previous thread | Next thread >>
problem with eeprom write.. plz help..... :(
Go to page   <<      
Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph, ExperimenterUK, DavesGarage
Author Post
tweety bird
Wed Jul 08 2009, 06:05AM
 User Offline
Registered Member #7329
Joined: Sat Apr 19 2008, 08:49PM

Posts: 70
Thanked 8 times in 7 posts
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 ????
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

 




Back to top

tweety bird
Wed Jul 08 2009, 06:07AM
 User Offline
Registered Member #7329
Joined: Sat Apr 19 2008, 08:49PM

Posts: 70
Thanked 8 times in 7 posts
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
Back to top

tweety bird
Wed Jul 08 2009, 11:34PM
 User Offline
Registered Member #7329
Joined: Sat Apr 19 2008, 08:49PM

Posts: 70
Thanked 8 times in 7 posts
ajay??? dave??? shyam??? anyone ???
Back to top

Ajay Bhargav
Fri Jul 10 2009, 08:40AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1329 times in 1253 posts
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.

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


This post has been thanked 1 time
tweety bird
Fri Jul 10 2009, 07:40PM
 User Offline
Registered Member #7329
Joined: Sat Apr 19 2008, 08:49PM

Posts: 70
Thanked 8 times in 7 posts
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...
Back to top

gopi
Fri Jul 10 2009, 09:17PM
 User Offline
Registered Member #9289
Joined: Fri Jul 25 2008, 05:04AM

Posts: 192
Thanked 12 times in 12 posts
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;




Never cry in front of others...........
Don't give them the pleasure of knowing how much they hurt you.
Back to top


This post has been thanked 1 time
tweety bird
Sat Jul 11 2009, 05:04AM
 User Offline
Registered Member #7329
Joined: Sat Apr 19 2008, 08:49PM

Posts: 70
Thanked 8 times in 7 posts
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
Back to top

gopi
Sat Jul 11 2009, 05:23AM
 User Offline
Registered Member #9289
Joined: Fri Jul 25 2008, 05:04AM

Posts: 192
Thanked 12 times in 12 posts
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"

Never cry in front of others...........
Don't give them the pleasure of knowing how much they hurt you.
Back to top

tweety bird
Sat Jul 11 2009, 06:25PM
 User Offline
Registered Member #7329
Joined: Sat Apr 19 2008, 08:49PM

Posts: 70
Thanked 8 times in 7 posts
nice gopi.... best of luck :bye
Back to top

gopi
Sat Jul 11 2009, 10:07PM
 User Offline
Registered Member #9289
Joined: Fri Jul 25 2008, 05:04AM

Posts: 192
Thanked 12 times in 12 posts
Thank you...........

Never cry in front of others...........
Don't give them the pleasure of knowing how much they hurt you.
Back to top

Go to page   <<       

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems