Friday, February 10, 2017

AT24C256 EEPROM with Arduino

AT24C256 EEPROM CHIP Demo
2/10/2017

Project Scope:
Test EEPROM with Arduino with basic write and read function.

Circuit connection:
Arduino ----  24C256
A5 ----- 6
 A4 ----- 5
GND ----- 4, 7
      VCC ----- 1, 2 & 8



Circuit Layout

Test Result




Arduino Code

//Modified from http://playground.arduino.cc/Code/I2CEEPROM
//Note: Device address
//if A1-->GND, A0-->GND -->0x50
//if A1-->GND, A0-->Vcc -->0x51
//if A1-->Vcc, A0-->GND -->0x52
//if A1-->Vcc, A0-->Vcc -->0x53

#include<Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  char somedata[]="This is only a test.";
 
  //Write a page
  i2c_eeprom_write_page(0x53, 0, (byte*)somedata, sizeof(somedata));delay(10);

  //Wirte number 125 to address 100
  i2c_eeprom_write_byte(0x53, 100, 123);delay(10);
  Serial.println("Data written.");
}


void loop() {
  int addr=0;

  //Read page of data
  byte b=i2c_eeprom_read_byte(0x53,addr);
  while (b!=0){
    Serial.print((char)b);
    addr++;
    b=i2c_eeprom_read_byte(0x53,addr);
  }
  Serial.println("  ");

  //Read a byte
  b=i2c_eeprom_read_byte(0x53,100);
  Serial.println((int)b);

  delay(10000);
}

void i2c_eeprom_write_byte(int device_address, unsigned int ee_address, byte data){
  Wire.beginTransmission(device_address);
  Wire.write((int)(ee_address >> 8)); //MSB
  Wire.write((int)(ee_address & 0xFF)); //LSB
  Wire.write(data);
  Wire.endTransmission();
}

//Cannot longer then 30 byte.
void i2c_eeprom_write_page(int device_address, unsigned int ee_address_page, byte* data,byte lengths){
  Wire.beginTransmission(device_address);
  Wire.write((int)(ee_address_page >> 8)); //MSB
  Wire.write((int)(ee_address_page & 0xFF)); //LSB
  byte c;
  for(c=0;c<lengths;c++)Wire.write(data[c]);
  Wire.endTransmission();
}

byte i2c_eeprom_read_byte(int device_address, unsigned int ee_address){
  byte rdata=0xFF;
  Wire.beginTransmission(device_address);
  Wire.write((int)(ee_address >> 8)); //MSB
  Wire.write((int)(ee_address & 0xFF)); //LSB
  Wire.endTransmission();
  Wire.requestFrom(device_address,1);
  while(!Wire.available()){ }
  rdata=Wire.read();
  return rdata;
}

No comments:

Post a Comment