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

24LC1025 MULTI-BYTE WRITE driver

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



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

24LC1025 MULTI-BYTE WRITE driver
PostPosted: Sat Apr 14, 2012 7:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 14, 2012 8:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 14, 2012 9:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 16, 2012 6:24 am     Reply with quote

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.

Code:

address&pagemask


becomes

Code:

address % pagesize


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

View user's profile Send private message

PostPosted: Mon Apr 16, 2012 2:58 pm     Reply with quote

Thanks again jeremiah , you advices solved my problem, is working as i want now! ^**^
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