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()

{

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM