No, non funzione, magari sbaglio altro non lo so....
cmq posto tutto il codice di porva per scrittura e lettura byte da eeprom esterna.
Ho utilizzato anche altre funzioni come la write_page che riceve byte* che con itoa converto int in string è funziona alla grande.
qui invce non riesco a farmi ristampare la variabile, quindi se non me la stampa credo che neanche la salva.
e pure che mi sembra talmente banale...
Sul reference di byte() c'è scritto che byte(x) riceve qualsiasi dato e restituisce byte. Perchè non va???????
#include <Wire.h> //I2C library
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();
}
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[c]);
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;
}
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()
{
Wire.begin(); // initialise the connection
Serial.begin(9600);
int x =3;
i2c_eeprom_write_byte( 0x50, 0, (byte)x );
delay(10);
i2c_eeprom_write_byte( 0x50, 1, '2' );
delay(10);
i2c_eeprom_write_byte( 0x50, 2, '1' );
delay(10);
i2c_eeprom_write_byte( 0x50, 3, '0' );
delay(10);
Serial.println("Memory written");
delay(10);
}
void loop()
{
Serial.println(i2c_eeprom_read_byte(0x50, 0));
Serial.println(" ");
delay(1000);
Serial.println(i2c_eeprom_read_byte(0x50, 1));
Serial.println(" ");
delay(1000);
Serial.println(i2c_eeprom_read_byte(0x50, 2));
Serial.println(" ");
delay(1000);
Serial.println(i2c_eeprom_read_byte(0x50, 3));
Serial.println(" ");
delay(1000);
}