View previous topic :: View next topic |
Author |
Message |
maria100
Joined: 01 Feb 2012 Posts: 63
|
24LC1025 MULTI-BYTE WRITE driver |
Posted: Sat Apr 14, 2012 7:14 am |
|
|
Hello , please if anybody has already made the driver for 24LC1025 that supports multi-byte ( page read & write) , please share it , it will be very helpful ! i have tried to make it myself based on the code posted by dvdb for
24lc512:
Code: |
#define pagesize 128
#define pagemask ~((long)pagesize-1) //this is for the 24LC512
#define hi(x) (*(&x+1))
#define EEPROM2 0b00001100
#define control_byte_read 0xa1
#define control_byte_write 0xa0
void init_ext_eeprom() {
output_float(PIN_C4);
output_float(PIN_C3);
}
BOOLEAN ext_eeprom2_ready() {
int1 ack;
i2c_start(); // If the write command is acknowledged,
ack = i2c_write(control_byte_write|EEPROM2); // then the device is ready.
i2c_stop();
return !ack;
}
void write_ext_eeprom2(long int address, BYTE data) {
while(!ext_eeprom2_ready());
i2c_start();
i2c_write(control_byte_write|EEPROM2);
i2c_write(hi(address));
i2c_write(address);
i2c_write(data);
i2c_stop();
}
BYTE read_ext_eeprom2(long int address) {
BYTE data;
while(!ext_eeprom2_ready());
i2c_start();
i2c_write(control_byte_write|EEPROM2);
i2c_write(hi(address));
i2c_write(address);
i2c_start();
i2c_write(control_byte_read|EEPROM2);
data=i2c_read(0);
i2c_stop();
return(data);
}
void multiread_ext_eeprom2(long int address, int number, int* dest )
{
if(number>0)
{
while(!ext_eeprom2_ready());
i2c_start();
i2c_write(control_byte_write|EEPROM2);
i2c_write(hi(address));
i2c_write(address);
i2c_start();
i2c_write(control_byte_read|EEPROM2);
while (number>1)
{*dest=i2c_read(1); //1 because do ACK after each byte read
dest++;
number--;
}
*dest=i2c_read(0); //0 because no ACK
i2c_stop();
};
}
void multiwrite_ext_eeprom2(long int address, int number, int* data )
{ long strt_adress;
while(number!=0)
{
strt_adress=address&pagemask;
while(!ext_eeprom2_ready());
i2c_start();
i2c_write(control_byte_write|EEPROM2);
i2c_write(hi(address));
i2c_write(address);
while ((number!=0)&&((address&pagemask)==strt_adress))
{i2c_write(*data);
data++;
number--;
address++;
};
i2c_stop();
};
|
but i did not succeeded to make it work for my 24LC1025..Thank you very much! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Apr 14, 2012 8:35 am |
|
|
Please quit opening new threads on the same topic.
Please update the one you already started here:
http://www.ccsinfo.com/forum/viewtopic.php?t=47719 _________________ Google and Forum Search are some of your best tools!!!! |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Sat Apr 14, 2012 9:13 am |
|
|
hmm..yea..sorry about that i did not realise that...will keep this in mind in the future..as for this one..if i had already posted it..i will go forward with it, thank you. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Mon Apr 16, 2012 6:24 am |
|
|
Somethings:
1. Instead of your hi() macro, try this:
Code: |
#define hi(value) ((value >> 16) & 0x0000FFFF)
|
The code should optimize to a word write.
2. If you are going to go through the trouble of and'ing the page mask, just do the modulus operation. The compiler will do that optimization for you.
becomes
3. Make #define constants use all caps. Easier to read and figure out what you are doing. This is a very standard thing.
4. Look very carefully at this code block, specifically the 2nd condition in the while statement:
Code: |
while ((number!=0)&&((address&pagemask)==strt_adress)){
i2c_write(*data);
data++;
number--;
address++;
};
|
When address does the increment after the first write, what happens to that condition? It looks like it only runs once from a cursory glance. |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Mon Apr 16, 2012 2:58 pm |
|
|
Thanks again jeremiah , you advices solved my problem, is working as i want now! ^**^ |
|
|
|