24LC512 I2C EEPROM Driver

Code rewritten for Arduino 1.0 Just added basic byte read and write feature. Inluding Hardware pin information

Three Files are needed here for the library:


eeprom24LC512.h

#ifndef _EEPROM24LC512_h_
#define _EEPROM24LC512_h_

// SETTINGS 
#define EE24LC512MAXBYTES        64000

#endif


eeprom24LC512.ino


/*

---------------- Changelog -------------------------------------
1.0    s.a.   Write single bytes and read single bytes

---------------- Requirements -------------------------------------
 - datatypes.h



------------------- Hardware --------------------------------------
Tested Part: MICROCHIP - 24LC512-I/P - EEPROM SERIELL 512K,24LC512, DIP8

Number Name ConnectTo     Note
1       a0     GND
2       a1     GND
3       a2     GND
4       GND    GND
5       SDA    SDA         2kOhm PullUp
6       SCL    SCL
7       WP     GND
8       VCC    (+3v3 ... +5V0)


*/

#include <Wire.h>
#include "eeprom24LC512.h"

// ##########  SETTINGS ###########################################
#define DEVICEADDRESS   0x50 // 0b0101 0 a2 a1 a0

// ##########  IMPLEMENTATION #####################################
void WireEepromBegin(void)
{
  Wire.begin();
}
// ----------------------------------------------------------------
void WireEepromWriteByte(uint16_t theMemoryAddress, uint8_t u8Byte) 
{

    Wire.beginTransmission(DEVICEADDRESS);
    Wire.write( (theMemoryAddress >> 8) & 0xFF );
    Wire.write( (theMemoryAddress >> 0) & 0xFF );
    Wire.write(u8Byte);
    Wire.endTransmission();
    delay(5);

}

// ----------------------------------------------------------------
uint8_t WireEepromRead(uint16_t theMemoryAddress) 
{
  uint8_t u8retVal = 0;
  Wire.beginTransmission(DEVICEADDRESS);
  Wire.write( (theMemoryAddress >> 8) & 0xFF );
  Wire.write( (theMemoryAddress >> 0) & 0xFF );
  Wire.endTransmission();
  delay(5);
  Wire.requestFrom(DEVICEADDRESS, 1);
  u8retVal = Wire.read();
  return u8retVal ;
}



datatypes.h

#ifndef _DATATYPES_H_
#define _DATATYPES_H_
/*
---------------- Changelog -------------------------------------
1.0    s.a.   Start of file
*/
typedef unsigned char uint8_t;
typedef int int16_t;
typedef unsigned int uint16_t;
typedef long int32_t;
typedef unsigned long uint32_t;

#endif


Old Text from this site, was used as starting point


24LC512 I2C EEPROM Driver

A simple example of interfacing with the 24LC512 I2C EEPROM.

With just two pins, 3.3V and ground and no other electronics, the following sketch will interface with an I2C EEPROM, reading and writing bytes and ints.

It builds on the work of others (wayoda and JohnZero) whilst cutting the cruft. (It was my experience that the paging didn't work as hoped and that, without the delays, reading & writing was unreliable.) More details and data types to follow.


#include <Wire.h>

const int theDeviceAddress = 80;

void setup(void) {
  Serial.begin(9600);
  Wire.begin();
}

void loop(void) {
  for (unsigned int theMemoryAddress = 0; theMemoryAddress < 256; theMemoryAddress++) {
    WireEepromWriteByte(theDeviceAddress, theMemoryAddress, (byte)theMemoryAddress);
  }
  for (unsigned int theMemoryAddress = 0; theMemoryAddress < 256; theMemoryAddress++) {
    Serial.print("Byte: ");
    Serial.print(theMemoryAddress);
    Serial.print(", ");
    Serial.print((int)WireEepromReadByte(theDeviceAddress, theMemoryAddress));
    Serial.println(".");
  }
  for (unsigned int theMemoryAddress = 0; theMemoryAddress < 1024; theMemoryAddress++) {
   WireEepromWriteInt(theDeviceAddress, theMemoryAddress * 2, (int)theMemoryAddress);
   }
   for (unsigned int theMemoryAddress = 0; theMemoryAddress < 1024; theMemoryAddress++) {
   Serial.print("Int: ");
   Serial.print(theMemoryAddress);
   Serial.print(", ");
   Serial.print(WireEepromReadInt(theDeviceAddress, theMemoryAddress * 2));
   Serial.println(".");
   }
}

void WireEepromRead(int theDeviceAddress, unsigned int theMemoryAddress, int theByteCount, byte* theByteArray) {
  for (int theByteIndex = 0; theByteIndex < theByteCount; theByteIndex++) {
    Wire.beginTransmission(theDeviceAddress);
    Wire.send((byte)((theMemoryAddress + theByteIndex) >> 8));
    Wire.send((byte)((theMemoryAddress + theByteIndex) >> 0));
    Wire.endTransmission();
    delay(5);
    Wire.requestFrom(theDeviceAddress, sizeof(byte));
    theByteArray[theByteIndex] = Wire.receive();
  }
}

byte WireEepromReadByte(int theDeviceAddress, unsigned int theMemoryAddress) {
  byte theByteArray[sizeof(byte)];
  WireEepromRead(theDeviceAddress, theMemoryAddress, sizeof(byte), theByteArray);
  return (byte)(((theByteArray[0] << 0)));
}

int WireEepromReadInt(int theDeviceAddress, unsigned int theMemoryAddress) {
  byte theByteArray[sizeof(int)];
  WireEepromRead(theDeviceAddress, theMemoryAddress, sizeof(int), theByteArray);
  return (int)(((theByteArray[0] << 8)) | (int)((theByteArray[1] << 0)));
}

void WireEepromWrite(int theDeviceAddress, unsigned int theMemoryAddress, int theByteCount, byte* theByteArray) {
  for (int theByteIndex = 0; theByteIndex < theByteCount; theByteIndex++) {
    Wire.beginTransmission(theDeviceAddress);
    Wire.send((byte)((theMemoryAddress + theByteIndex) >> 8));
    Wire.send((byte)((theMemoryAddress + theByteIndex) >> 0));
    Wire.send(theByteArray[theByteIndex]);
    Wire.endTransmission();
    delay(5);
  }
}

void WireEepromWriteByte(int theDeviceAddress, unsigned int theMemoryAddress, byte theByte) {
  byte theByteArray[sizeof(byte)] = {(byte)(theByte >> 0)};
  WireEepromWrite(theDeviceAddress, theMemoryAddress, sizeof(byte), theByteArray);
}

void WireEepromWriteInt(int theDeviceAddress, unsigned int theMemoryAddress, int theInt) {
  byte theByteArray[sizeof(int)] = {(byte)(theInt >> 8), (byte)(theInt >> 0)};
  WireEepromWrite(theDeviceAddress, theMemoryAddress, sizeof(int), theByteArray);
}


Share