Arduino en español
Circuitos con Arduino - Juan Antonio Villalpando
-- Tutorial de iniciación a Arduino --
Volver al índice del tutorial
____________________________
33B.- EEPROM. Leer y escribir.
EEPROM o E²PROM son las siglas de Electrically Erasable Programmable Read-Only Memory (ROM programable y borrable eléctricamente). Es un tipo de memoria ROM que puede ser programada, borrada y reprogramada eléctricamente, a diferencia de la EPROM que ha de borrarse mediante un aparato que emite rayos ultravioleta. Son memorias no volátiles, es decir si le quitamos la alimentación, mantiene su información.
- La EEPROM tiene una vida útil de unas 100.000 veces de grabado/borrado.
- Se trata de grabar informacion en la EEPROM del Arduino para más tarde recuperarla.
_______________________________________________________________________________
- Guarda y lee datos en la EEPROM. Se introducen desde el Monitor Serie. Reiniciar el Arduino.
// https://arduino.stackexchange.com/questions/25945/how-to-read-and-write-eeprom-in-esp8266
- Cuando el programa arranca por primera vez guarda en la EEPROM: kio4.com y marca "iniciado"
- Vamos al Monitor Serie, escribimos un texto, lo Enviamos y se guardará en la EEPROM.
- Reseteamos el Arduino.
- Cuando vuelve a arrancar el Arduino, leemos la EEPROM y obtenemos el texto escrito anteriormente en el Monitor Serie.
- Se pueden guardar textos en trozos de hasta 32 bytes.
Grabar - Leer |
const char* marca = "iniciado"; //MAX 32
const char* FIREBASE_HOST = "kio4.com"; //MAX 32
String marca_leida = "";
String FIREBASE_HOST_en_otra_variable = "";
char rx_byte = 0;
String rx_str = "";
#include <EEPROM.h>
void setup() {
Serial.begin(9600);
marca_leida = EEPROM_ESP8266_LEER(0,32).c_str();
if (marca_leida != "iniciado") {
EEPROM_ESP8266_GRABAR(marca, 0); //Primero de 0 al 32
EEPROM_ESP8266_GRABAR(FIREBASE_HOST, 32); // del 32 al 64, etc
}
// Leer
Serial.println(EEPROM_ESP8266_LEER(0, 32)); // Primero de 0 al 32
Serial.println(EEPROM_ESP8266_LEER(32, 64)); // del 32 al 64, etc
Serial.println (EEPROM_ESP8266_LEER(0,32).c_str());
Serial.println (EEPROM_ESP8266_LEER(32,64).c_str());
FIREBASE_HOST_en_otra_variable = EEPROM_ESP8266_LEER(32,64).c_str();
}
//
void loop() {
if (Serial.available() > 0) { // ¿Hay algún caracter?
rx_byte = Serial.read(); // Toma el caracter
rx_str += rx_byte;
if (rx_byte == '\n') {
Serial.print(rx_str);
EEPROM_ESP8266_GRABAR(rx_str, 32);
rx_str = "";
}
}
}
//
void EEPROM_ESP8266_GRABAR(String buffer, int N) {
EEPROM.begin(512); delay(10);
for (int L = 0; L < 32; ++L) {
EEPROM.write(N + L, buffer[L]);
}
EEPROM.commit();
}
//
String EEPROM_ESP8266_LEER(int min, int max) {
EEPROM.begin(512); delay(10); String buffer;
for (int L = min; L < max; ++L)
// if (isAlphaNumeric(EEPROM.read(L)))
buffer += char(EEPROM.read(L));
return buffer;
}
|
_____________________________________________________________________
- Otros ejemplos.
http://www.instructables.com/id/Save-values-in-your-Arduinos-permanent-memory/
______________
- Escribir.
- Esto escribiría la palabra MARTIN.
#include <EEPROM.h>
void setup() {
//EEPROM.write(ADDRESS,VALUE);
int MyVal=255;
EEPROM.write(0,77);
EEPROM.write(1,65);
EEPROM.write(2,82);
EEPROM.write(3,84);
EEPROM.write(4,73);
EEPROM.write(5,78);
}
________________
- Leer.
#include <EEPROM.h>
int MemoryAddr=0;
int Value=0;
void setup() {
Serial.begin(38400);
}
void loop() {
for (int MemoryAddr=0; MemoryAddr <= 5; MemoryAddr++) {
value = EEPROM.read(MemoryAddr);
char MyCharacter = char(value);
Serial.println(MyCharacter);
}
delay(5000);
}
____________________________________________________
- Vamos a poner número de serie a un Arduino en su EEPROM.
- Código de: http://forum.arduino.cc/index.php?topic=45104.0
// WRITES ARDUINO SERIAL TO EEPROM
//
// do this only once on an Arduino,
// write the Serial of the Arduino in the
// first 6 bytes of the EEPROM
#include <EEPROM.h>
char sID[7] = "CD6484";
void setup()
{
Serial.begin(9600);
for (int i=0; i<6; i++) {
EEPROM.write(i,sID[i]);
}
}
void loop() {
Serial.println(sID);
}
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
// READS ARDUINO SERIAL FROM EEPROM
//
// reads the Serial of the Arduino from the
// first 6 bytes of the EEPROM
#include <EEPROM.h>
char sID[7];
void setup()
{
Serial.begin(9600);
for (int i=0; i<6; i++) {
sID[i] = EEPROM.read(i);
}
}
void loop() {
Serial.println(sID);
}
____________________________________________________
- Chip EEPROM
24LC256.
- Conectamos este chip al Arduino mediante I2C, podremos escribir y leer datos en él.
http://www.hobbytronics.co.uk/arduino-external-eeprom
Grabar - Leer |
#include <EEPROM.h>
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth no activado! Activa la conexion Bluetooth.
#endif
BluetoothSerial SerialBT;
const char* texto = ""; //MAX 32
String texto_read = "";
char rx_byte = 0;
String rx_str = "";
void setup() {
Serial.begin(9600);
SerialBT.begin("ESP32test");
}
void loop() {
if (Serial.available() > 0) { // ¿Hay algún caracter?
rx_byte = Serial.read(); // Toma el caracter
rx_str += rx_byte;
if (rx_byte == '\n') {
EEPROM_ESP32_WRITE(rx_str, 0); // Save in address 0...32
Serial.print("texto Written to EEPROM: ");
Serial.println(rx_str);
rx_str = "";
texto_read = (String) EEPROM_ESP32_READ(0,32); // Read 0...32 address
Serial.print("texto Read from EEPROM: ");
Serial.println(texto_read);
Serial.println(".....................");
Serial.print("Now texto sended by Bluetooth: ");
Serial.println(texto_read);
SerialBT.print(texto_read);
}
}
}
//// Function WRITE EEPROM
void EEPROM_ESP32_WRITE(String buffer, int N) {
EEPROM.begin(512); delay(10);
for (int L = 0; L < 32; ++L) {
EEPROM.write(N + L, buffer[L]);
}
EEPROM.commit();
}
//// Function READ EEPROM
String EEPROM_ESP32_READ(int min, int max) {
EEPROM.begin(512); delay(10); String buffer;
for (int L = min; L < max; ++L)
// if (isAlphaNumeric(EEPROM.read(L)))
buffer += char(EEPROM.read(L));
return buffer;
}
|
________________________________
|