Skip to content

Commit

Permalink
added EEPROM interface class, for use with 24LC16B EEPROM chip
Browse files Browse the repository at this point in the history
  • Loading branch information
npisenti committed Apr 30, 2013
1 parent ed39cec commit 7d6c3f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 13 additions & 9 deletions MyEEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,37 @@ JQI - Strontium - UMD

// The *device* address is 1010 (no way to change on 24LC16B) -- these need to
// be in MSB position --> use 0x50.
// I'm leaving this in as a parameter though, for later extension perhaps.

// I'm leaving this in as a #define parameter for now.

#define deviceAddress 0x50


// data: an array of bytes of data you'd like to write!
// length: how many bytes you're writing...
// NOTE!! currently limited to reads and writes of (up to) 16 bytes at a time
void MyEEPROMClass::write(int memAddress, const byte * data, byte length){
byte devAddr = deviceAddress | ((memAddress >> 8) & 0x07); // extracts block, bitwise OR onto device addr.
byte addr = lowByte(memAddress); // extracts low byte off memAddress
// this is the 7-bit address arduino Wire wants.

Wire.beginTransmission(devAddr); // begin I2C transfer
Wire.write(int(addr)); // specify which byte you're starting on
for(int i = 0; i < length; i++){
Wire.write(memAddress); // specify which memory address you're starting on

// loops through and writes data; note, will cut you short if you try to write more than
// 16 bytes! This preserves the data integrity of what you've already written on the chip.
for(int i = 0; i < length && i < 16; i++){
Wire.write(data[i]); // write data
}
Wire.endTransmission();
delay(10); // delay so EEPROM chip can persist buffer to memory


}

byte MyEEPROMClass::read(int memAddress, byte * buffer, byte length){
byte devAddr = deviceAddress | ((memAddress >> 8) & 0x07); // extracts block, bitwise OR onto device addr.
byte addr = lowByte(memAddress); // extracts low byte off memAddress

// make like you're going to do a write, so EEPROM moves pointer to beginning of readout block
Wire.beginTransmission(devAddr);
Wire.write(int(addr));
Wire.write(memAddress);
Wire.endTransmission();

// request bytes from EEPROM
Expand All @@ -81,5 +85,5 @@ byte MyEEPROMClass::read(int memAddress, byte * buffer, byte length){
return i;
}

// instantiate class, to use as MyEEPROM in arduino sketches.
MyEEPROMClass MyEEPROM;

14 changes: 13 additions & 1 deletion examples/EEPROMTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@

void setup(){
byte msg[] = {1,2,5};
byte mystr[] = "testing yo!";
Wire.begin();
Serial.begin(9600);

Serial.println("Writing to 0x100");
MyEEPROM.write(0x100, msg, 3);

Serial.println("Writing to 0x200");
MyEEPROM.write(0x100, mystr, sizeof(mystr));


}

void loop(){
byte buffer[3];
byte buffer2[16];
Serial.println("Reading from 0x100");
MyEEPROM.read(0x100, buffer, sizeof(buffer));

for (int i = 0; i < sizeof(buffer); i++){

Serial.println(buffer[i]);
}

Serial.println("Reading from 0x200");
MyEEPROM.read(0x200, buffer22, sizeof(buffer2));

for int(i=0; i<sizeof(buffer2); i++){
Serial.write(buffer2[i]);
}
delay(5000);
}

0 comments on commit 7d6c3f7

Please sign in to comment.