|
|
View previous topic :: View next topic |
Author |
Message |
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
Is this code for page read write to 24LC512 EEPROM correct ? |
Posted: Mon Nov 11, 2013 8:59 am |
|
|
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: 19538
|
|
Posted: Mon Nov 11, 2013 10:21 am |
|
|
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
|
RE: |
Posted: Mon Nov 11, 2013 11:40 am |
|
|
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: 19538
|
|
Posted: Mon Nov 11, 2013 2:34 pm |
|
|
Looks good.
Why not make the functions just take the index then?.
Best Wishes |
|
|
|
|
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
|