View previous topic :: View next topic |
Author |
Message |
donquixote
Joined: 21 Jul 2008 Posts: 15 Location: Shanghai, China
|
How to Read/Write the External I2C-based EEPROM? |
Posted: Thu Oct 16, 2008 2:27 am |
|
|
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
|
|
Posted: Thu Oct 16, 2008 2:56 am |
|
|
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
|
|
Posted: Thu Oct 16, 2008 6:55 pm |
|
|
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
|
|
Posted: Fri Oct 17, 2008 1:13 am |
|
|
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 |
|
|
|
|