CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Is this code for page read write to 24LC512 EEPROM correct ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

Is this code for page read write to 24LC512 EEPROM correct ?
PostPosted: Mon Nov 11, 2013 8:59 am     Reply with quote

Hi,

I am trying to develop page read and write functions for the 24LC512 EEPROM. The EEPROM is i2c based.

My code is given below.

Code:

devaddress=0xA0;
int nMMCBuffer[512];

//reads 512 bytes from the EERPROM
void pageread_ext_eeprom(int devaddrs,long int address){

   int16 bytecntr;
       
        //device address
   bit_clear(devaddrs,0);

   i2c_start();
   i2c_write(devaddrs);
   i2c_write(address>>8);
   i2c_write(address);
   i2c_start();

   //read mode
   bit_set(devaddrs,0);
   i2c_write(devaddrs);

   //read in the data
   bytecntr=0;
   for (bytecntr=0;bytecntr<=511;bytecntr++){   
      nMMCBuffer[bytecntr]=i2c_read(1);
      bytecntr++;
   }

   //no acknowledge, stops transfer
   i2c_read(0);
   i2c_stop();

}

//writes 512 bytes of data to the eeprom
void pagewrite_ext_eeprom(int devaddrs,long int address){

   int16 bytecntr;
   int pgcnt,i;
   short int status;
   
   bit_clear(devaddrs,0);

   i2c_start();
   i2c_write(devaddrs);
   i2c_write(address>>8);
   i2c_write(address);

   bytecntr=0;

   for (pgcnt=0;pgcnt<4;pgcnt++){
      //transfer 128 bytes to on-chip page buffer
      for (i=0;i<=127;i++){   
         i2c_write(nMMCBuffer[bytecntr]);
         bytecntr++;
      }
   
      //transfers data from page buffer to EEPROM (actual write)
      i2c_stop();

      //poll for status of write
      i2c_start();
      status=i2c_write(devaddrs);
      while(status==1)
      {
         i2c_start();
         status=i2c_write(devaddrs);
      }

   }

   i2c_stop();

}



I just want an opinion, I have not tried this yet I will do so tomorrow.

thanks
a
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Mon Nov 11, 2013 10:21 am     Reply with quote

Several little comments.

Your read code, physically sets the low bit of devaddress. Means it won't work a second time, nor will the write, since the address is now set to 'read'...

Better to use:
Code:

#define READ 1

//Then to select read mode:

   //read mode
   i2c_write(devaddrs | READ);


This way the physical address is never changed.

Then you'll have a problem if the ROM address given to the functions, is not the start of a page.... Need to test for this.

I'd suggest you don't do the extra read to stop the transaction, but instead include this in the read:
Code:

   //read in the data
   for (bytecntr=0;bytecntr<=510;){   
      nMMCBuffer[bytecntr++]=i2c_read(1);
   }

   //no acknowledge, stops transfer
   nMMCBuffer[bytecntr]= i2c_read(0); //last read stops the transfer
   i2c_stop();


You are incrementing the counter twice in the loop, and clearing it twice before starting....

Best Wishes
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Mon Nov 11, 2013 11:40 am     Reply with quote

Thanks, I didn't see those errors...

The ROM address is addressed like this.

address=512*Index

Index =0,1,2...n

so address will be= 0, 512, 1024 etc...

Do you think this will avoid the page boundaries...??

thanks
a
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Mon Nov 11, 2013 2:34 pm     Reply with quote

Looks good.

Why not make the functions just take the index then?.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group