AT24C EEPROM

Actual Hints

This function is no longer maintained by me and not compatible with Arduino 1.0 Development Software.

Description

These functions are for using your Arduino with a bank of EEPROM chips of the type AT24C128, AT24C256, AT24C512 and AT24C1024. You can address the following number of chips with one #define argument.

AT24C Chip Quantity
128 8
256 8
512 4
1024 2

The code is contributed by Oli Kraus, the devoloper of the dogm128 library. It has been tested with my BaroTemp-Shield v2 and 2 AT24C512 chips, but should work with the listed chips.

Code

#define Section:

// *********************************************
// * CONSTANTS
// *********************************************
/* AT24C128 */
//#define EEPROM_ADR_BITS 14
/* AT24C256 */
//#define EEPROM_ADR_BITS 15
/* AT24C512 and at24c1024 */
#define EEPROM_ADR_BITS 16

Variables Section:

// *********************************************
// * VARIABLES
// *********************************************
unsigned long START_ADDRESS = 0x0000000;
unsigned long MIN_ADDRESS = 0x0000000;
unsigned long MAX_ADDRESS = 0x0020000;
byte val = 0x000;
byte origin_val;
unsigned long read_mismatches = 0;

Function Section:

// *********************************************
// * FUNCTIONS
// *********************************************
/*
    Functions for the EEPROM
*/
void AT_EEPROM_write(unsigned long adr, unsigned char val)
{
  unsigned char dev_number;
  unsigned short dev_adr;
  unsigned char high_byte, low_byte;
  dev_number = adr >> EEPROM_ADR_BITS;
  dev_number += 0x050;
  dev_adr = adr & ((1UL<<EEPROM_ADR_BITS)-1UL);

  high_byte = dev_adr >> 8;
  low_byte = dev_adr & 0x0ff;
  Wire.beginTransmission(dev_number);
  Wire.send(high_byte);
  Wire.send(low_byte);
  Wire.send(val);
  Wire.endTransmission();
  delay(5);                             // typical write delay
}

void AT_EEPROM_read(unsigned long adr)
{
  unsigned char dev_number;
  unsigned short dev_adr;
  unsigned char high_byte, low_byte;
  dev_number = adr >> EEPROM_ADR_BITS;
  dev_number += 0x050;
  dev_adr = adr & ((1UL<<EEPROM_ADR_BITS)-1UL);

  high_byte = dev_adr >> 8;
  low_byte = dev_adr & 0x0ff;
  Wire.beginTransmission(dev_number);
  Wire.send(high_byte);
  Wire.send(low_byte);
  Wire.endTransmission();
  Wire.requestFrom(dev_number, (uint8_t)1);  // read a byte
  while(!Wire.available())
  {
   // waiting
  }
  val = Wire.receive();
}

Test Code (complete&tested):

// *********************************************
// INCLUDE
// *********************************************
#include <Wire.h>

// *********************************************
// * CONSTANTS
// *********************************************
/* AT24C128 */
//#define EEPROM_ADR_BITS 14
/* AT24C256 */
//#define EEPROM_ADR_BITS 15
/* AT24C512 and at24c1024 */
#define EEPROM_ADR_BITS 16

// *********************************************
// * VARIABLES
// *********************************************
unsigned long START_ADDRESS = 0x0000000;
unsigned long MIN_ADDRESS = 0x0000000;
unsigned long MAX_ADDRESS = 0x0020000;         // 2 chips AT24C512
byte val = 0x000;
byte origin_val;
unsigned long read_mismatches = 0;

// *********************************************
// * SETUP
// *********************************************
void setup()
{
  Serial.begin(115200);
  Serial.println("AT24C512 EEPROM Bank Test");
  Serial.println("------------------------------");
  Serial.println("Filling Bank with 131072 bytes");
  Wire.begin();
  randomSeed(analogRead(0));
  val = random(1, 254);
  origin_val = val;                // safe val!
  Serial.print("Random value is: 0x0");
  Serial.println(val, HEX);
  Serial.println("------------------------------");
}

// *********************************************
// * BEGIN
// *********************************************
void loop()
{
  for (unsigned long x = MIN_ADDRESS; x < MAX_ADDRESS; x++)
  {
    AT_EEPROM_write(START_ADDRESS, val);
    Serial.print("Address written: 0x0");
    Serial.println(START_ADDRESS, HEX);
    START_ADDRESS = START_ADDRESS + 1;
  }
  Serial.println("------------------------------");
  Serial.println("Writing finished");
  Serial.println("");
  Serial.println("");

  delay(5000);
  Serial.println("Reading 131072 bytes from Bank");
  Serial.println("------------------------------");
  START_ADDRESS = 0x0000000;

  for (unsigned long x = MIN_ADDRESS; x < MAX_ADDRESS; x++)
  {
    AT_EEPROM_read(START_ADDRESS);
    Serial.print("Address: 0x0");
    Serial.print(START_ADDRESS, HEX);
    Serial.print(" Value: 0x0");
    Serial.println(val, HEX);
    if (val != origin_val)
    {
      Serial.print("ERROR: Address 0x0");
      Serial.println(START_ADDRESS, HEX);
      read_mismatches = read_mismatches + 1;
    }
    START_ADDRESS = START_ADDRESS + 1;
  }
  Serial.println("------------------------------");
  Serial.println("Reading finished");
  Serial.println("");
  Serial.println("");
  Serial.print("Mismatches: ");
  Serial.println(read_mismatches, DEC);
  while (1==1)
  {
    // do nothing
  }
}

// *********************************************
// * FUNCTIONS
// *********************************************
/*
    Functions for the EEPROM
*/
void AT_EEPROM_write(unsigned long adr, unsigned char val)
{
  unsigned char dev_number;
  unsigned short dev_adr;
  unsigned char high_byte, low_byte;
  dev_number = adr >> EEPROM_ADR_BITS;
  dev_number += 0x050;
  dev_adr = adr & ((1UL<<EEPROM_ADR_BITS)-1UL);

  high_byte = dev_adr >> 8;
  low_byte = dev_adr & 0x0ff;
  Wire.beginTransmission(dev_number);
  Wire.send(high_byte);
  Wire.send(low_byte);
  Wire.send(val);
  Wire.endTransmission();
  delay(5);                             // typical write delay
}

void AT_EEPROM_read(unsigned long adr)
{
  unsigned char dev_number;
  unsigned short dev_adr;
  unsigned char high_byte, low_byte;
  dev_number = adr >> EEPROM_ADR_BITS;
  dev_number += 0x050;
  dev_adr = adr & ((1UL<<EEPROM_ADR_BITS)-1UL);
  val = 0x000;                               // only for testing purposes
  high_byte = dev_adr >> 8;
  low_byte = dev_adr & 0x0ff;
  Wire.beginTransmission(dev_number);
  Wire.send(high_byte);
  Wire.send(low_byte);
  Wire.endTransmission();
  Wire.requestFrom(dev_number, (uint8_t)1);  // read a byte
  while(!Wire.available())
  {
   // waiting
  }
  val = Wire.receive();
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *


*

Go to Top