Arduino – obsługa EEPROM I2C

Poniżej prosty Sketch do obsługi eeprom i fram po i2c

//
//
#include <Wire.h>   // biblioteka do obsługi i2c 
 
#define device_address 0x50    // adres eeprom
 
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0; // adres komórki do zapisu
 
  // zapisz (adress eeprom, adres komórki, wartość)
  writeEEPROM(device_address, address, 123); 

  // odczytaj z eeporom (adress eeprom, adres komórki)
  Serial.print(readEEPROM(device_address, address), DEC); 
}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8));   // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.send(data);
  Wire.endTransmission();
 
  delay(5);
}
 
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8));   // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.receive();
 
  return rdata;
}