Ciao ragazzi, sto creando un piccolo dispositivo, visto che mi serve memorizzare dei dati ho deciso di mettere una EEPROM esterna così da poterla cambiare in caso si guastasse.
Cercavo online dei tutorial su come usarla, dovrei memorizzare un numero fornito da una variabile, che potranno andare da 0 a 2200, quindi se non erro devo usare più byte, giusto?
Cercando ho trovato un tutorial che da uno sketch di esempio, ovviamente nello sketch si chiede ad Arduino di memorizzare un valore prestabilito, unico problema di questo sketch che da come dato di lettura un altro valore rispetto a quello che memorizza.
Potreste per favore spiegarmi come risolvere questo problema che si presenta?
Grazie e buon week end a tutti
#include <Wire.h>
#define eeprom 0x50 //defines the base address of the EEPROM
void setup() {
Wire.begin(); //creates a Wire object
Serial.begin(9600);
unsigned int address = 0; //first address of the EEPROM
Serial.println("We write the zip code 22222, a zip code");
for(address = 0; address<5; address++)
writeEEPROM(eeprom, address, '2'); // Writes 22222 to the EEPROM
for(address = 0; address<5; address++) {
Serial.print(readEEPROM(eeprom, address), HEX);
}
}
void loop() {
/*there's nothing in the loop() function because we don't want the arduino to
repeatedly write the same thing to the EEPROM over and over.
We just want a one-time write, so the loop() function is avoided with EEPROMs.*/
}
//defines the writeEEPROM function
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); //writes the MSB
Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
Wire.write(data);
Wire.endTransmission();
}
//defines the readEEPROM function
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); //writes the MSB
Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available())
rdata = Wire.read();
return rdata;
}
Vi allego lo screenshot del risultato ottenuto:
La EEPROM è un 24LC256 collegata come da schema sotto.