24C1024 I2C EEPROM 128KB

24CXX I2C EEPROM Interface using SoftI2C lib

In this tutorial I will show you how to use our easy to use open source Soft I2C library to access a 24C64 EEPROM chip. We discussed the library in our previous tutorial. The functions developed in this tutorial will use services of the I2C library to read and write the EEPROM. The EEPROM access mainly contains only two routines.

BOOL EEWriteByte(uint16_t address,uint8_t data);
BOOL EEReadByte(uint16_t address ,uint8_t* data);

The first one write a 8 bit data to a memory location specified by the variable address, since a EEPROM can have more that 256 bytes of storage that is why the address variable is 16 bit.

The second function reads the memory location specified by the variable address and stores the read data in variable data.

Both the function return EE_SUCCESS on success and EE_IO_ERROR on failure.

How to develop the EEPROM Write function?

We open the datasheet of 24C64A (64kbit) EEPROM and try to understand the random read and random write function. The figure 8 on page number 11 clearly describes the write steps. You just need to translate the figure into proper call to our Soft I2C lib API’s.




BOOL EEWriteByte(uint16_t address,uint8_t data)
{
   SoftI2CStart();

   //SLA+W
   if(!SoftI2CWriteByte(EEPROM_WRITE_ADDRESS))
   {
      SoftI2CStop();

      //I2C i/o error

      return EE_IO_ERROR;
   }

   //Address HIGH
   if(!SoftI2CWriteByte(address>>8))
   {
      SoftI2CStop();

      //I2C i/o error
      return EE_IO_ERROR;
   }

   //Address LOW

   if(!SoftI2CWriteByte(address))
   {
      SoftI2CStop();

      //I2C i/o error
      return EE_IO_ERROR;
   }

   //Data
   if(!SoftI2CWriteByte(data))
   {
      SoftI2CStop();

      //I2C i/o error
      return EE_IO_ERROR;
   }

   SoftI2CStop();

   _delay_ms(5);

   return EE_SUCCESS;
}

As you can see in the above code how we have done so. Yes their is a bit of error checking that may confuse a newbie. Other wise the steps are straight foreword. First we write the SLA+W (I2C slave address of the EEPROM, +W means a write request). The SLA+W for our EEPROM is 0xA0. This is defined in our eeprom.h file.

Then we write the high part of 16 bit address, followed by the lower byte. Finally we write the data to be written to that address and issue a I2C stop state. After all that we wait 5ms so that EEPROM can have time to complete the write.

We check for ACK for every byte we send to the EEPROM, in case ACK is not received it means some error. ACK checking is done by verifying the return value from SoftI2CWriteByte() function. In case ACK is not received for any data transfer we return EE_IO_ERROR, this informs the caller that function has failed.

How to develop the EEPROM Read function?

Writing the EEPROM read function is similar to EEPROM write function. We refer to the figure 11 on page 12 of datasheet.




BOOL EEReadByte(uint16_t address,uint8_t *data)
{

   SoftI2CStart();

   //SLA+W

   if(!SoftI2CWriteByte(EEPROM_WRITE_ADDRESS))
   {
      SoftI2CStop();

      //I2C i/o error
      return EE_IO_ERROR;
   }

   //Address HIGH
   if(!SoftI2CWriteByte(address>>8))
   {
      SoftI2CStop();

      //I2C i/o error

      return EE_IO_ERROR;
   }
   //Address LOW     
   if(!SoftI2CWriteByte(address))
   {
      SoftI2CStop();

      //I2C i/o error
      return EE_IO_ERROR;
   }

   //Repeat Start

   SoftI2CStart();

   //SLA+R
   if(!SoftI2CWriteByte(EEPROM_READ_ADDRESS))
   {
      SoftI2CStop();

      //I2C i/o error
      return EE_IO_ERROR;
   }

   //Read + NAK   
   *data=SoftI2CReadByte(0);

   SoftI2CStop();

   return EE_SUCCESS;
}


You just need to notice one thing that In SoftI2CReadByte() function we pass 0 as the argument. This means we do not wish to acknowledge the byte read. According to the datasheet this is a signal to EEPROM to stop sending more bytes. In case you wised to read more than one byte sequentially, you should keep on sending ACK until you have received the last byte, then you should respond with a NAK.

So you just saw how easy it was to develop the EEPROM read/write support over the core I2C lib. In a similar way we will develop code for I2C RTC, Port Expander, Temperature sensor etc on coming tutorials.

Help Us!

We try to publish beginner friendly tutorials for latest subjects in embedded system as fast as we can. If you like these tutorials and they have helped you solve problems, please help us in return. You can donate any amount as you like securely using a Credit or Debit Card or Paypal.

We would be very thankful for your kind help.

By
Avinash Gupta
Facebook, Follow on Twitter.
www.AvinashGupta.com
me@avinashgupta.com

Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.

Avinash

Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

4 thoughts on “24CXX I2C EEPROM Interface using SoftI2C lib

  • Pingback: Software I2C Library for AVR MCUs | eXtreme Electronics

  • By oshodin john - Reply

    pls i need more treatise on d above topic,d basics and fundamental of 24xx12c and where 2 get software 2 develop it

    • By sudagar - Reply

      respected sr
      i’m worke dun now electronics service center
      and like interseted embeded sys but i hav no proper gide please explain
      1)how interface extenel eprom (24cxx)
      2)how to communicat MCU at89c 51 & 24CXX ‘iic’bus sys
      3) how to develope ASM language program

      thanks for ur kindly info
      contact 9787427273

  • By Sameer - Reply

    Thanks For this,These lib are very useful for me because I am working on ATMEGA162 which not support I2C

Leave a Reply

Your email address will not be published. Required fields are marked *


4 + five =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>