Created
February 8, 2015 03:06
-
-
Save hpux735/53abdafbab9eeeeb0329 to your computer and use it in GitHub Desktop.
Writing a string to an i2c eprom with Arduino
This file contains hidden or 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
/* | |
* Use the I2C bus with EEPROM 24LC64 | |
* | |
*/ | |
#include <Wire.h> //I2C library | |
void writeEEPROM(char *string, unsigned char block, unsigned char address); | |
void printEEPRIOM(); | |
void eraseEEPROM(); | |
void setup() | |
{ | |
Serial.begin(115200); | |
Wire.begin(); // initialise the connection | |
//CLEAR EEPROM | |
eraseEEPROM(); | |
//WRITE DATA | |
// writeEEPROM("STRING1", 80, 0x00); | |
writeEEPROM("STRING2", 81, 0x00); | |
writeEEPROM("STRING3", 82, 0x00); | |
writeEEPROM("STRING4", 83, 0x00); | |
// Verify the written data | |
printEEPROM(); | |
} | |
void loop() { delay(1000); } | |
void writeEEPROM(char *string, unsigned char block, unsigned char address) { | |
// Is this how you set the address? | |
// The bytes written can't be more than 16. | |
int bytesWritten = 0; | |
// Begin the transaction with the given i2c address | |
Wire.beginTransmission(block); | |
// Send the address to write | |
Wire.write(address); | |
// Write the preamble of 4 0xFFs | |
for (int i = 0; i < 4; i++) { Wire.write(0xFF); } | |
bytesWritten += 4; | |
// Write the string | |
for (int i = 0; string[i] != '\0'; i++) { | |
Wire.write(string[i]); | |
bytesWritten++; | |
} | |
// Write the postamble | |
Wire.write('\0'); | |
bytesWritten++; | |
for (int i = 0; i < 4 && bytesWritten < 16; i++) { | |
Wire.write(0xFF); | |
bytesWritten++; | |
} | |
// End the transcation | |
Wire.endTransmission(); | |
// Make sure the device is done | |
delay(10); | |
} | |
void printEEPROM() { | |
// Print it to the console in columns of 16 bytes. | |
// Convert 0xFF to '.' and 0x00 to * | |
// Leave ASCII as ASCII, and print addresses | |
Serial.println("Begin EEPROM Read Back"); | |
// The address of the first block is 80 | |
for(int i = 80; i < 84; i++) { | |
Serial.print("Block 0x"); | |
Serial.print(i, HEX); | |
Serial.println(": "); | |
for(int j = 0; j < 255; j += 16) { | |
Wire.beginTransmission(i); | |
Wire.write(j); | |
Wire.endTransmission(); | |
// Print the address | |
Serial.print("0x"); | |
Serial.print(j, HEX); | |
if(j == 0) Serial.print(" "); | |
else Serial.print(" "); | |
// Get the data | |
Wire.requestFrom(i, 16); | |
//Serial.print(i, DEC); | |
for(int k = 0; k < 16; k++) { | |
char retval = Wire.read(); | |
// Perform the pretty printing | |
switch(retval) { | |
case 0x00: | |
Serial.print("*"); | |
break; | |
case 0xFF: | |
Serial.print("."); | |
break; | |
default: | |
Serial.print(retval); | |
} | |
} | |
Serial.println(""); | |
} | |
} | |
} | |
void eraseEEPROM() { | |
//Write data to all locations | |
int data = 255; //Data to write to EEPROM | |
int block = 80; //dec 2 hex address conversion 80=0x50, 81=0x51, 82=0x52, 83=0x53 | |
// We need to write 4 blocks to the EEPROM | |
for (int i = 0; i < 4; i++) { | |
// Each block is 255 sets of 16 words | |
for (int j = 0; j < 255; j += 16) { | |
// Each transaction can be 16 words long | |
// The i2c address is the "block" | |
Wire.beginTransmission(block + i); | |
// Send the lower address for the transaction | |
Wire.write(j); | |
for (int k = 0; k < 16; k++) { | |
Wire.write(data); | |
} | |
Wire.endTransmission(); | |
delay(10); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment