Created
July 4, 2013 08:30
-
-
Save dpslwk/5925944 to your computer and use it in GitHub Desktop.
Arduino 24LC256 I2C EEPROM Test sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
const int theDeviceAddress = 0x50; | |
void setup(void) { | |
Serial.begin(9600); | |
Wire.begin(); | |
//power the board | |
pinMode(A2,OUTPUT); | |
digitalWrite(A2,LOW); | |
pinMode(A3,OUTPUT); | |
digitalWrite(A3,HIGH); | |
} | |
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.write((byte)((theMemoryAddress + theByteIndex) >> 8)); | |
Wire.write((byte)((theMemoryAddress + theByteIndex) >> 0)); | |
Wire.endTransmission(); | |
delay(5); | |
Wire.requestFrom(theDeviceAddress, sizeof(byte)); | |
theByteArray[theByteIndex] = Wire.read(); | |
} | |
} | |
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.write((byte)((theMemoryAddress + theByteIndex) >> 8)); | |
Wire.write((byte)((theMemoryAddress + theByteIndex) >> 0)); | |
Wire.write(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); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment