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 support@ccsinfo.com

page writes to eeprom

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








page writes to eeprom
PostPosted: Sun Mar 06, 2005 12:35 am     Reply with quote

does anyone know how write to the external eeprom in page mode?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

Re: page writes to eeprom
PostPosted: Sun Mar 06, 2005 4:41 am     Reply with quote

Anonymous wrote:
does anyone know how write to the external eeprom in page mode?
Which external eeprom do you mean? Do you have a type number for us?
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Sun Mar 06, 2005 8:53 am     Reply with quote

If you mean something related to the 24C64/128/256 (the only ones I've tested this with) it is pretty easy but I was a little bit lazy and didn't try writing it in a more RAM efficient way since this particular routine is being used on a PIC18 and I am only expecting to be using about 25% of total RAM anyway.

Code:
/*
 * void writepage_ext_eeprom(long int address, char *data)
 *
 * Purpose    : Write 64 byte page to  external EEPROM at particular address.
 *              Uses ACK polling to find end of eeprom write cycle.
 * Parameters : address - 16 bit int for address
 *              *data - pointer to array of 64 bytes to write
 * Calls      : various
 * Returns    : none
 * Notes      : TODO : change device address to #def'd value
 * Tested     :
 */
void writepage_ext_eeprom(long int address, char *data)
{
   int8 i;

   i2c_start();
   i2c_write(0xA0);
   i2c_write(make8(address,1));
   i2c_write(make8(address,0));
   for(i=0;i<64;i++)
   {
      i2c_write(data[i]);
   }
   i2c_stop();
   // eeprom ACK polling
   do {
      i2c_start();
   } while(i2c_write(0xA0));
}

_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Mar 06, 2005 9:21 am     Reply with quote

Here are some code snippets that if you read them will give you idea of what to do. Note that the page size is dependant upon the chip used. I believe this code was for a 24C08 (actually 2 of them)
Code:

#define I2C_IDLE()  while ((SSPCON2 & 0x1F) || (SSPSTATbits.R_W))
#define I2C_START() \
  SSPCON2bits.SEN = 1; \
while (SSPCON2bits.SEN) \
{ \
  _asm nop _endasm \
}

#define I2C_RESTART() \
  SSPCON2bits.RSEN = 1; \
  while (SSPCON2bits.RSEN) \
  { \
    _asm nop _endasm \
  }

#define I2C_STOP() \
  SSPCON2bits.PEN = 1; \
  while (SSPCON2bits.PEN) \
  { \
    _asm nop _endasm \
  }

#define I2C_WRITE(x) \
  SSPBUF = (x); \
  while (SSPSTATbits.BF) \
  { \
    _asm nop _endasm \
  } \
  I2C_IDLE()
#define I2C_READ(x) \
  SSPCON2bits.RCEN = 1; \
  while (SSPCON2bits.RCEN) \
  { \
    _asm nop _endasm \
  } \
  SSPCON2bits.ACKDT = (x); \
  SSPCON2bits.ACKEN = 1; \
  while (SSPCON2bits.ACKEN) \
  { \
    _asm nop _endasm \
  }
/* *************************************************************************
  DESCRIPTION:  This function writes a block of data to the SEEPROM

  RETURN: none

  ALGORITHM:  none

  NOTES:  The routine should handle going across chip boundaries
 *************************************************************************** */
void I2C_Write_Block(
  uint8_t   device,   /* I2C address of the device that we are writing to */
  char    *data,    /* pointer to the block of data to write */
  uint8_t   len,      /* number of bytes to write */
  uint16_t  address)  /* EEPROM address to write data */
{
  uint8_t control;
  uint8_t lastpage;
  uint8_t page;

  /* determine the control word for the device */
  control = device | ((uint8_t) (address >> 8) << 1);

  /* Wait until our device is ready */
  while (!I2C_Device_Ready(control));

  /* Set the address pointer in the device */
  I2C_WRITE((uint8_t) address);

  page = (uint8_t) address >> 4;
  lastpage = page;
  while (len)
  {
    if (page != lastpage)
    {
      /* Issuing a STOP will start the write sequence */
      I2C_STOP();
      lastpage = page;

      /* determine the control word for the device */
      control = device | ((uint8_t) (address >> 8) << 1);

      /* Wait until our device is ready */
      while (!I2C_Device_Ready(control));

      /* Set the address pointer in the device */
      I2C_WRITE((uint8_t) address);
    }

    I2C_WRITE(*data);
    data++;
    address++;

    /* The 24C08 has a 16 byte page for writing. */
    page = (uint8_t) address >> 4;
    len--;
  }

  I2C_STOP();
}

/* *************************************************************************
  DESCRIPTION:  This function checks to see if an i2c device will ACK

  RETURN: TRUE if device ACKed otherwise FALSE

  ALGORITHM:  none

  NOTES:  none
 *************************************************************************** */
static bool I2C_Device_Ready(
  uint8_t id) /* FIX ME: add comment */
{
  do
  {
    /* Important to clear these bits before we attempt to write */
    PIR2bits.BCLIF = 0;
    SSPCON1bits.WCOL = 0;
    I2C_IDLE();             /* ensure module is idle */
    I2C_START();
  } while (PIR2bits.BCLIF);

  I2C_WRITE(id);

  if (!SSPCON2bits.ACKSTAT) /* test for ACK condition, if received */
    return (TRUE);
  return (FALSE);
}

drolleman
Guest







PostPosted: Sun Mar 06, 2005 11:43 am     Reply with quote

thank for the info, it's just what i needed
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