arduino开发ESP8266学习笔记七--------EEPROM的使用

使用arduino开发ESP8266,就可以 更加方便的使用EEPROM,使用时调用EEPROM库。

/*
EEPROM.cpp - esp8266 EEPROM emulation

Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef EEPROM_h
#define EEPROM_h

#include <stddef.h>
#include <stdint.h>
#include <string.h>

class EEPROMClass

{
  public:
  EEPROMClass(uint32_t sector);
  EEPROMClass(void);

  void begin(size_t size);
  uint8_t read(int const address);
  void write(int const address, uint8_t const val);
  bool commit();
  void end();

  uint8_t * getDataPtr();
  uint8_t const * getConstDataPtr() const;

  template<typename T>
  T &get(int const address, T &t) {
  if (address < 0 || address + sizeof(T) > _size)
  return t;

  memcpy((uint8_t*) &t, _data + address, sizeof(T));
  return t;
}

template<typename T>
const T &put(int const address, const T &t)

{
  if (address < 0 || address + sizeof(T) > _size)
  return t;
  if (memcmp(_data + address, (const uint8_t*)&t, sizeof(T)) != 0) {
  _dirty = true;
  memcpy(_data + address, (const uint8_t*)&t, sizeof(T));
}

return t;
}

size_t length() {return _size;}

uint8_t& operator[](int const address) {return getDataPtr()[address];}
uint8_t const & operator[](int const address) const {return getConstDataPtr()[address];}

protected:
uint32_t _sector;
uint8_t* _data;
size_t _size;
bool _dirty;
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
extern EEPROMClass EEPROM;
#endif

#endif

 EEPROM保存的数据可以在断电后任然存在,可以保存一些设置类的信息在里面,一个EEEPROM空间保存一个字节的数据,只能存储0-255的一个数,使用时要注意EEPROM有写次数上限,不要将EEPROM写操作直接写在loop函数里面。

第一部分:EEPROM保存布尔,字节型数据

图1

第二部分:保存int类型数据

保存int类型数据时,记得要将整型分成两部分存在另一个byte数组中,下图为错误延时,具体错误已找出,见程序注释

图2

 图3是更改后正确的截屏

 

 **

图3

 

 第三部分:保存sting型数据

  和其他不一样的地方是,保存string 数据是,不仅要保存需要保存的值,而且还要保存字符串的长度。

图4

程序代码如下:

#include <EEPROM.h>

/*EEPROM的一个空间里只够存储一个字节的数据,所以在存储int类型数据时,要格外注意*/

bool is=1;//布尔型数据,范围0-1
byte byte_1=220;//字节型数据,范围在0-255内
char char_1='a';//char类型数据,范围在0-255内
int a=1234;
union int_value//联合体,在一个地址上存储,连个数据相互关联
{
  int a;
  byte b[2];
};//注意,这个分号,我写丢了一次???????
int_value e_int;//声明

void setup()
{

  Serial.begin(9600);//初始化串口波特率为9600
  Serial.println(" ");//
  EEPROM.begin(1024);//开辟1024个EEPROM空间,就是1024字节的空间

  Serial.print("存储的字符串类型值为:");
  Serial.println(str); //打印Hallo World



  /*byte型数据写EEPROM*/



  EEPROM.write(0,byte_1);//将byte_1存储在第零个EEPROM空间内
  EEPROM.commit();//保存EEPROM数据,和uno,nano不同,ESP8266保存数据需要使用该指令。
  byte byte_2=EEPROM.read(0);//读取第零个EEPROM空间的数据并存储在byte_2中
  Serial.print("保存的值为:");
  Serial.println(byte_2);//通过串口打印byte_2的值,也就是存储在第零个EEPROM空间的数据


  /*字符型型数据写EEPROM*/


  EEPROM.write(0,char_1);
  EEPROM.commit();//保存EEPROM数据,和uno,nano不同,ESP8266保存数据需要使用该指令。
  char char_2=EEPROM.read(0);
  Serial.print("存储的字符型数据是:");
  Serial.println(char_2);//通过串口打印char_2的值,也就是存储在第零个EEPROM空间的数据

 

  /*整数型数据写EEPROM*/
  //一个字节保存不了一个整型数据,所以拆分为两个字节,存放在数组中
  e_int.a=a;
  EEPROM.write(0,e_int.b[0]);
  EEPROM.write(1,e_int.b[1]);
  byte b1=EEPROM.read(0);
  byte b2=EEPROM.read(1);
  e_int.b[0]=b1;
  e_int.b[1]=b2;//这里写错了一次,出现错误,应该是b[1],写成b[2],出现截屏的错误,
  Serial.print("保存的整型数据是:");
  Serial.println(e_int.a);
  EEPROM.commit();//保存EEPROM数据,和uno,nano不同,ESP8266保存数据需要使用该指令。
  //EEPROM是有读写次数限制的,所以要注意不要将写操作放在loop函数里面


}

void loop()

{

}

原文地址:https://www.cnblogs.com/--Destroyer--/p/13284502.html