Le entrate A0 a A2 determinao gli ultimi 3 bit del indirizzo del dispositivo. a secondo delle necessitá possono-devono essere messi a Masso o alla tensione di alimentazione.
Se sono messe a massa l' indirizzo é quelo base.
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.send(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.send((int)(eeaddresspage >> 8)); // MSB
Wire.send((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.send(data
);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( 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;
}
// 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.send((int)(eeaddress >> 8)); // MSB
Wire.send((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.receive();
}
void setup()
{
char somedata[] = "this is data from the eeprom"; // data to write
Wire.begin(); // initialise the connection
Serial.begin(9600);
i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // 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);
}
liberamente espirato da
http://www.arduino.cc/playground/Code/I2CEEPROM
Mi restituisce "Memory written" ma quando va a leggere mi si pianta sul valore 255.
Saluti Totodix
Con il seguente sketch trovato in rete, sono riuscito a scrivere, sulla eeprom, la stringa "1234567890123456" sedici byte.
Se cerco di scrivere oltre i sedici byte mi restituisce il valore 255.
Qualcuno può aiutarmi?
Grazie totodix
#include <Wire.h> // specify use of Wire.h library.
byte sw0 = 12; // digital pin for switch connected to ground.
// address in 24LC08. values from 000 hex to 3FF hex
int position_pointer = 0x3f0;
void setup()
{
pinMode(sw0, INPUT);
digitalWrite(sw0, HIGH); // turn on internal pull up
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // setup serial for output
// send test message "1234567890123456"
Wire.beginTransmission(0x50); // connect to 24LC08 device address
Wire.send(position_pointer); // beginning address within EEPROM
Wire.send("1234567890123456");
Wire.endTransmission();
}
void loop()
{
while (digitalRead(sw0) == 1) {
} // wait here until switch is pushed.
Wire.beginTransmission(0x50); // link to 24LC08
Wire.send(position_pointer); // must act as a position pointer
Wire.endTransmission();
Wire.requestFrom(0x50, 7); // request 7 bytes from slave device 24LC08
// below will loop until 7 bytes are received.
while(Wire.available()) // slave may send less than requested
{
byte c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
}
Serial.print("\n"); // next line
delay(1000); // wait one second.
}