Created
February 19, 2014 19:17
-
-
Save pklaus/9099457 to your computer and use it in GitHub Desktop.
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
/************************************************************************ | |
* Arduino Due: Reading & Writing to I2C EEPROM Version: 1.00 * | |
* Function : Reads and Writes to Atmel 24C64 Serial EEPROM * | |
* Exp. Level : Beginner/Elementary * | |
************************************************************************ | |
* Arduino IDE v1.5.2 from http://arduino.cc/en/Main/Software * | |
* Configuration: Arduino Due (Programming Port) on /dev/ttyACM0 * | |
* Operating System: Xubuntu Linux 13.10 Saucy Salamander (32-bit) * | |
* Created: June 13, 2013 Latest Revision: June 13, 2013 * | |
************************************************************************ | |
* The Main Ingredients * | |
* Arduino Due (element14 Order #: 2250861) * | |
* Atmel 24C64 Serial EEPROM 64K (element14 Order #: 1362657) * | |
************************************************************************ | |
* Reference Pages * | |
* Arduino DUE : http://goo.gl/4YRP8 * | |
* Wire Library: http://goo.gl/6wKis * | |
* I2C EEPROM : http://goo.gl/vlOR3 * | |
************************************************************************ | |
*-----------------------Wiring Configuration---------------------------* | |
* Arduino DUE pin 20 (SDL) to EEPROM pin 5 * | |
* Arduino DUE pin 21 (SCL) to EEPROM pin 6 * | |
* Arduino DUE 3.3VDC (VCC) to EEPROM pin 8 <<< ADVISORY!!! * | |
* Arduino DUE Ground (GND) to EEPROM pins 1, 2, 3, and 4 * | |
************************************************************************ | |
* MirandaSoft! - Pasig City, Metro Manila, Philippines * | |
* http://www.element14.com/community/blogs/mirandasoft * | |
************************************************************************/ | |
#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 ) { | |
Wire.beginTransmission(deviceaddress); | |
Wire.write((int)(eeaddresspage >> 8)); // MSB | |
Wire.write((int)(eeaddresspage & 0xFF)); // LSB | |
byte c; | |
for ( c = 0; c < length; c++) | |
Wire.write(data[c]); | |
Wire.endTransmission(); | |
} | |
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) { | |
byte rdata = 0xFF; | |
Wire.beginTransmission(deviceaddress); | |
Wire.write((int)(eeaddress >> 8)); // MSB | |
Wire.write((int)(eeaddress & 0xFF)); // LSB | |
Wire.endTransmission(); | |
Wire.requestFrom(deviceaddress,1); | |
if (Wire.available()) rdata = Wire.read(); | |
return rdata; | |
} | |
// maybe let's not read more than 30 or 32 bytes at a time! | |
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) { | |
Wire.beginTransmission(deviceaddress); | |
Wire.write((int)(eeaddress >> 8)); // MSB | |
Wire.write((int)(eeaddress & 0xFF)); // LSB | |
Wire.endTransmission(); | |
Wire.requestFrom(deviceaddress,length); | |
int c = 0; | |
for ( c = 0; c < length; c++ ) | |
if (Wire.available()) buffer[c] = Wire.read(); | |
} | |
void setup() { | |
pinMode(13, OUTPUT); // DUE's Pin 13 LED is stuck HIGH | |
digitalWrite(13, LOW); // Turn off Pin 13 LED | |
char message[] = "Kuya Marc's Arduino DUE Life"; // data to write | |
Wire.begin(); // initialise the connection | |
Serial.begin(9600); | |
i2c_eeprom_write_page(0x50, 0, (byte *)message, sizeof(message)); // write to EEPROM | |
delay(10); // add a small delay | |
Serial.println("Memory written"); | |
} | |
void loop() { | |
int addr=0; // first address | |
byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory | |
while (b!=0) { | |
Serial.print((char)b); // print content to serial port | |
addr++; // increase address | |
b = i2c_eeprom_read_byte(0x50, addr); // access an address from the memory | |
} | |
Serial.println(" "); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment