anduony 发表于 2016-1-26 13:55:39

EEPROM使用中的问题

在Arduino的IDE1.6.7版本中使用EEPROM的put和get功能时发现下面的问题

数据按照PUT进行存储之后,不断电时数据正常读取和写入,当把设备断电之后,之前保存的数据
读取出来之后是乱码?使用串口显示
不知道这是为什么?
有了解的一起讨论。

275891381 发表于 2016-1-26 16:53:28

//EEPROM.write()
//EEPROM.read()
//EEPROM.put()
//EEPROM.get()
//EEPROM.update()//相同则不更改,节约100000的读写次数

#include <EEPROM.h>

struct MyObject{
float field1;
byte field2;
char field3;
};

void setup(){

float f0 = 123.456789f;//float 和 double 相同精度,6-7位有效数字
float f1 = 0.00f;   //Variable to store data read from EEPROM.
int eeAddress = 0; //EEPROM address to start reading from

Serial.begin( 9600 );

//单个数字测试
EEPROM.update(eeAddress, f0);
Serial.println("Written float data type!");
EEPROM.get( eeAddress, f1 );
Serial.print( "Read float from EEPROM: " );
Serial.println( f1, 6 );//This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.

//多字节读取测试
// get() can be used with custom structures too.
    MyObject customVar0 = {
    3.14159f,
    65,
    "Working!"
};

eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.put(eeAddress, customVar0);

MyObject customVar1; //Variable to store custom object read from EEPROM.
EEPROM.get( eeAddress, customVar1 );
Serial.println( "Read custom object from EEPROM: " );
Serial.println( customVar1.field1,6 );
Serial.println( customVar1.field2 );
Serial.println( customVar1.field3 );
}

void loop(){ /* Empty loop */ }

anduony 发表于 2016-1-26 17:20:48

谢谢楼上的回复,我这里开始使用的是String的变量进行存储从串口读取的数据,然后写入E2PROM中,发现显示以及重新启动之后出现数据丢失的问题,现在改成使用字符串进行读取串口数据,然后写入E2PROM中正常了
页: [1]
查看完整版本: EEPROM使用中的问题