AT24CXXX EEPROM
AT24CXXX EEPROM
This page is to Setup and Config IR AT24CXXX EEPROM
Hardware and Software
Hardware
AT24CXXX EEPROM
Software
Arduino IDE
Sanki Notes
AT24CXXX EEPROM
------------ I2C ----------
GND : 1,2,3,4
V++ : 8
SCL : A5
SDA : A4
Examples
This example is ..........
Examples
This example is ..........
/*
* Use the I2C bus with EEPROM 24LC64
* Sketch: eeprom.pde
*
* Author: hkhijhe
* Date: 01/10/2010
*
*
*/
#include <Wire.h> //I2C library
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
#include <Wire.h>
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // MSB
#define EEPROM_ADDR 0x50 // I2C Buss address of 24LC256 256K EEPROM
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
/*
byte c;
* Read and Write Buffer Page MAX is 28byte / Page
for ( c = 0; c < length; c++)
*
Wire.write(data[c]);
*/
Wire.endTransmission();
}
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
{
byte rdata = 0xFF;
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress >> 8)); // Address High Byte
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.endTransmission();
Wire.write(rdata);
Wire.requestFrom(deviceaddress,1);
Wire.endTransmission();
if (Wire.available()) rdata = Wire.read();
}
return rdata;
}
// Address is a page address, 6-bit (63). More and end will wrap around
// But data can be maximum of 28 bytes, because the Wire library has a buffer of 32 bytes
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_write_page
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )
{
Wire.beginTransmission(deviceaddress);
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddresspage >> 8)); // Address High Byte
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write((int)(eeaddresspage & 0xFF)); // Address Low Byte
Wire.endTransmission();
byte c;
Wire.requestFrom(deviceaddress,length);
for ( c = 0; c < length; c++)
int c = 0;
Wire.write(data[c]);
for ( c = 0; c < length; c++ )
Wire.endTransmission();
if (Wire.available()) buffer[c] = Wire.read();
delay(10); // need some delay
}
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
void setup()
Wire.beginTransmission(deviceaddress);
{
Wire.write((int)(eeaddress >> 8)); // Address High Byte
char somedata[] = "this is data from the eeprom"; // data to write
Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte
Wire.begin(); // initialise the connection
Wire.endTransmission();
Serial.begin(9600);
Wire.requestFrom(deviceaddress,1);
i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
if (Wire.available()) rdata = Wire.read();
return rdata;
}
delay(10); //add a small delay
// should not read more than 28 bytes at a time!
Serial.println("Memory written");
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length )
}
{
void loop()
Wire.beginTransmission(deviceaddress);
{
Wire.write((int)(eeaddress >> 8)); // Address High Byte
int addr=0; //first address
Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte
byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
while (b!=0)
//int c = 0;
{
for ( int c = 0; c < length; c++ )
Serial.print(addr, HEX); //print content to serial port
if (Wire.available()) buffer[c] = Wire.read();
Serial.print(" : "); //print content to serial port
}
Serial.print(b); //print content to serial port
Serial.print(" : "); //print content to serial port
void setup()
Serial.println((char)b); //print content to serial port
{
addr++; //increase address
Wire.begin(); // join I2C bus (address optional for master)
b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
Serial.begin(9600);
}
Serial.println(" ");
// TESTS FOR EACH FUNCTION BEGIN HERE
delay(2000);
Serial.print("Writing Test:");
for (int i=0; i<20; i++){ // loop for first 20 slots
}
i2c_eeprom_write_byte(EEPROM_ADDR,i,i+65); // write address + 65 A or 97 a
Serial.print((char)(i+65));
Serial.print(" ");
delay(10); // NEED THIS DELAY!
}
Serial.println("");
delay(500);
Serial.print("Reading Test:");
for (int i=0; i<20; i++){ // loop for first 20 slots
Serial.write(i2c_eeprom_read_byte(EEPROM_ADDR, i));
Serial.print(" ");
}
// setup for page tests . . .
byte PageData[30]; // array that will hold test data for a page
byte PageRead[30]; // array that will hold result of data for a page
for (int i=0; i<30; i++){ // zero both arrays for next test
PageData[i] = 0;
PageRead[i] = 0;
}
Serial.println("\n");
for (int i=0; i<30; i++) PageData[i] = i+33; // fill up array for next test char 33 = !
Serial.print("Writing Page Test:");
for (int i=0; i<28; i++){ // zero both arrays for next test
Serial.print((char)PageData[i]);
Serial.print(" ");
}
Serial.println("\n");
i2c_eeprom_write_page(EEPROM_ADDR, 100, PageData, 28 ); // 28 bytes/page is max
Serial.print("Reading Page Test:");
i2c_eeprom_read_buffer( EEPROM_ADDR, 100, PageRead, 28);
for (int i=0; i<28; i++){
Serial.write(PageRead[i]); // display the array read
Serial.print(" ");
}
}
void loop()
{
}