极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12886|回复: 2

EEPROM使用中的问题

[复制链接]
发表于 2016-1-26 13:55:39 | 显示全部楼层 |阅读模式
在Arduino的IDE1.6.7版本中使用EEPROM的put和get功能时发现下面的问题

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

使用道具 举报

发表于 2016-1-26 16:53:28 | 显示全部楼层
  1. //EEPROM.write()
  2. //EEPROM.read()
  3. //EEPROM.put()
  4. //EEPROM.get()
  5. //EEPROM.update()//相同则不更改,节约100000的读写次数

  6. #include <EEPROM.h>

  7. struct MyObject{
  8.   float field1;
  9.   byte field2;
  10.   char field3[10];
  11. };

  12. void setup(){

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

  16.   Serial.begin( 9600 );

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

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

  30. eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
  31. EEPROM.put(eeAddress, customVar0);
  32.   
  33.   MyObject customVar1; //Variable to store custom object read from EEPROM.
  34.   EEPROM.get( eeAddress, customVar1 );
  35.   Serial.println( "Read custom object from EEPROM: " );
  36.   Serial.println( customVar1.field1,6 );
  37.   Serial.println( customVar1.field2 );
  38.   Serial.println( customVar1.field3 );
  39. }

  40. void loop(){ /* Empty loop */ }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-26 17:20:48 | 显示全部楼层
谢谢楼上的回复,我这里开始使用的是String的变量进行存储从串口读取的数据,然后写入E2PROM中,发现显示以及重新启动之后出现数据丢失的问题,现在改成使用字符串进行读取串口数据,然后写入E2PROM中正常了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 18:50 , Processed in 0.046174 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表