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

How to Read/Write the External I2C-based EEPROM?

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



Joined: 21 Jul 2008
Posts: 15
Location: Shanghai, China

View user's profile Send private message

How to Read/Write the External I2C-based EEPROM?
PostPosted: Thu Oct 16, 2008 2:27 am     Reply with quote

Hello, guys,

There are some pair of read/write functions for memory/EEPROMs. And I2C-based 24LC02B is used as the external EEPROM in my design.

So I am wondering which following pair(s) of Read/Write functions would be suitable for 24LC02B.

WRITE_EXTERNAL_MEMORY/READ_EXTERNAL_MEMORY?

Or WRITE_PROGRAM_EEPROM/READ_PROGRAM_EEPROM?

Or WRITE_PROGRAM_MEMORY/READ_PROGRAM_MEMORY?

Or I2C_WRITE/I2C_READ (which could not assign the specific address for reading/writing)


Thanks & Regards,
_________________
Don Quixote
Ttelmah
Guest







PostPosted: Thu Oct 16, 2008 2:56 am     Reply with quote

Taking them 'out of order'.

Write/read program memory, do exactly what they say. Read and write the _program_ memory. Nothing to do with external devices.
The same applies to the read/write program eeprom functions.

The external memory functions, are for chips that support directly connecting an external memory to them, not I2C EEPROMs. Look at the manual entry for 'external memory'.

I2C_READ, and I2C_WRITE, are the _raw_ functions for talking to I2C. You can set the address. Read how I2C works. The _first_byte sent after the IC_START, is the device address (and the direction control flag). Look at the code in the manual, for the I2C_START function, and see how the address is sent. In the 'drivers' directory, you will find a file called 2402.c, which is complete code to talk to the 2402 chips, using these functions.

Best Wishes
MarcosAmbrose



Joined: 25 Sep 2006
Posts: 38
Location: Adelaide, Australia

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 6:55 pm     Reply with quote

Try this...
Make sure you initialise the I2C bus before calling these routines

Code:

#define EE_I2C_ADDR 0xA0
#define EEPROM_SIZE   32768

//INITIALISE I2C Bus lines
void Init_I2C(void)
{
   output_float(PIN_C3);      //Float I2C lines on pins C3 and C4
   output_float(PIN_C4);
}


////////////////////////////////////////////////////////////////////////////////
// Function:   EEready()
//
// Purpose:    Routine to test if external EEprom is ready
//
// Parameters: None
//
// Returns:    Returns TRUE is ext EEprom is ready, otherwise returns FALSE.
////////////////////////////////////////////////////////////////////////////////
int1 EEready(void)
{
   int8 I2CTimeOut;
   int1 EEstatus;

   I2CTimeOut=0;
      i2c_start();
   EEstatus=i2c_write(EE_I2C_ADDR);

   while(EEstatus==1 && I2CTimeOut<10)
   {
      i2c_start();
      EEstatus=i2c_write(EE_I2C_ADDR);
      if(EEstatus)
         I2CTimeOut++;
   }
   i2c_stop();

   if(EEstatus)
      return(0);
   else
      return(1);
}


////////////////////////////////////////////////////////////////////////////////
// Function:   write_ext_eeprom(int16 address, int8 data)
//
// Purpose:    Writes a byte of data to a 16 bit address in the external EEprom
//
// Parameters: pAddress - 16 bit address where data is to be written.
//
//             pData - Data to be written to ext EEprom.
//
// Returns:    None.
////////////////////////////////////////////////////////////////////////////////
void write_ext_eeprom(int16 pAddress, int8 pData)
{
   short int status;

   i2c_start();
   i2c_write(EE_I2C_ADDR);
   i2c_write(pAddress>>8);
   i2c_write(pAddress);
   i2c_write(pData);
   i2c_stop();

   i2c_start();
   status=i2c_write(EE_I2C_ADDR);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(EE_I2C_ADDR);
   }
   i2c_stop();
}


////////////////////////////////////////////////////////////////////////////////
// Function:   read_ext_eeprom(int16 pAddress)
//
// Purpose:    Routine to read a byte of data from the external EEprom
//
// Parameters: pAddress - 16 bit address where data is to be read from.
//
// Returns:    8bit Data from ext EEprom
////////////////////////////////////////////////////////////////////////////////
int8 read_ext_eeprom(int16 pAddress)
{
  int8  data;
   data=0;

   if(EEready())
   {
      i2c_start();
      i2c_write(EE_I2C_ADDR);
      i2c_write(pAddress>>8);
      i2c_write(pAddress);
      i2c_start();
      i2c_write(EE_I2C_ADDR | 0x01);
      data=i2c_read(0);
      i2c_stop();
   }
   return(data);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 17, 2008 1:13 am     Reply with quote

Your routines use a 16-bit address. He is using the 24LC02B eeprom.
That eeprom uses an 8-bit address.

He should use the CCS driver. It's in this directory:
Quote:
c:\program files\picc\drivers\2402.c
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