Created
February 7, 2016 00:48
-
-
Save compilerexe/cbb7ec6eaa9f542e927a to your computer and use it in GitHub Desktop.
External I2C EEPROM to Arduino (24LC256)
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> | |
#define disk1 0x50 //Address of 24LC256 eeprom chip | |
void setup(void) | |
{ | |
Serial.begin(9600); | |
Wire.begin(); | |
unsigned int address = 0; | |
writeEEPROM(disk1, address, 123); | |
Serial.print(readEEPROM(disk1, address), DEC); | |
} | |
void loop(){} | |
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) | |
{ | |
Wire.beginTransmission(deviceaddress); | |
Wire.write((int)(eeaddress >> 8)); // MSB | |
Wire.write((int)(eeaddress & 0xFF)); // LSB | |
Wire.write(data); | |
Wire.endTransmission(); | |
delay(5); | |
} | |
byte readEEPROM(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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment