使用EEPROM断电保存数据

简介: 本文介绍了Arduino中EEPROM的使用,EEPROM是一种非易失性存储器,用于在断电后保留数据。Arduino的各种控制器如UNO、duemilanove等内置或可外接EEPROM,容量不同。Arduino库提供了`EEPROM.h`来支持读写操作。示例代码展示了如何写入、读取和清除EEPROM的内容。写入时,通过`EEPROM.write()`函数将模拟输入值存入指定地址;读取时,用`EEPROM.read()`函数获取地址处的值;清除则遍历所有地址并写入0。

EEPROM (Electrically Erasable Programmable Read-Only Memory),电可擦可编程只读存储器--一种掉电后数据不丢失的存储芯片。简而言之就是你想断电后arduino还要保存一些参数,就使用EEPROM吧。在各型号的arduino控制器上的AVR芯片均带有EEPROM,也有外接的EEPROM芯片,常见arduino控制器的EEPROM大小:Arduino UNO、Arduino duemilanove-m328、Zduino m328均使用ATmega328芯片,EEPROM都为1KArduino duemilanove-m168的EEPROM为512bytesArduino 2560的EEPROM为4K下面我们介绍arduino自带的EEPROM使用方法,arduino的库已经为我们准备好了EEPROM类库,我们要使用得先调用EEPROM.h,然后使用write和read方法,即可操作EEPROM。

另:下面的官方例子由于写成较早,所以讲EEPROM的大小都定为了512字节,实际使用中,大家可参照上面所说的EEPROM大小,自行更改。

1.写入

选择 File>Examples>EEPROM>eeprom_write

/*
    * EEPROM Write
    *
    * Stores values read from analog input 0 into the EEPROM.
    * These values will stay in the EEPROM when the board is
    * turned off and may be retrieved later by another sketch.
    */
    #include <EEPROM.h>
    // EEPROM 的当前地址,即你将要写入的地址,这里就是从0开始写
    int addr = 0;
    void setup()
    {
    }
    void loop()
    {
      //模拟值读出后是一个0-1024的值,但每字节的大小为0-255,所以这里将值除以4再存储到val
      int val = analogRead(0) / 4;
      
      // write the value to the appropriate byte of the EEPROM.
      // these values will remain there when the board is
      // turned off.
      EEPROM.write(addr, val);
      
      // advance to the next address.  there are 512 bytes in
      // the EEPROM, so go back to 0 when we hit 512.
      addr = addr + 1;
      if (addr == 512)
        addr = 0;
      
      delay(100);
    }

2.读取

选择 File>Examples>EEPROM>eeprom_read

/*
    * EEPROM Read
    *
    * Reads the value of each byte of the EEPROM and prints it
    * to the computer.
    * This example code is in the public domain.
    */
    #include <EEPROM.h>
    // start reading from the first byte (address 0) of the EEPROM
    int address = 0;
    byte value;
    void setup()
    {
      // initialize serial and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
    }
    void loop()
    {
      // read a byte from the current address of the EEPROM
      value = EEPROM.read(address);
      
      Serial.print(address);
      Serial.print("\t");
      Serial.print(value, DEC);
      Serial.println();
      
      // advance to the next address of the EEPROM
      address = address + 1;
      
      // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
      // on address 512, wrap around to address 0
      if (address == 512)
        address = 0;
       
      delay(500);
    }

3.清除

选择 File>Examples>EEPROM>eeprom_clear清除EEPROM的内容,其实就是把EEPROM中每一个字节写入0,因为只用清一次零,所以整个程序都在setup部分完成。

/* * EEPROM Clear
    *
    * Sets all of the bytes of the EEPROM to 0.
    * This example code is in the public domain.
    */
    #include <EEPROM.h>
    void setup()
    {
      // 让EEPROM的512字节内容全部清零
      for (int i = 0; i < 512; i++)
        EEPROM.write(i, 0);
       
      // 清零工作完成后,将L灯点亮,提示EEPROM清零完成
      digitalWrite(13, HIGH);
    }
    void loop()
    {
    }
相关文章
|
1月前
|
运维 数据挖掘 数据库
服务器数据恢复-服务器raid5硬盘指示灯变红的数据恢复案例
一台服务器上3块磁盘组建了一组raid5磁盘阵列。服务器运行过程中有一块硬盘的指示灯变为红色,raid5磁盘阵列出现故障,服务器上层操作系统的分区无法识别。
服务器数据恢复-服务器raid5硬盘指示灯变红的数据恢复案例
|
2天前
|
IDE 开发工具
【自制操作系统03】读取硬盘中的数据
【自制操作系统03】读取硬盘中的数据
|
1月前
|
存储
如何正确的存储你的 VHS 磁带集?
VHS是Video Home System的缩写,是20世纪末的家庭录像格式,使用模拟磁带。VCR是播放这些磁带的设备,曾广泛普及。存储VHS磁带时,应避免高温、湿度、垂直存放和不当处理,这些因素可能导致损坏。可以通过转换器或专业服务将VHS转换为DVD,以保存内容并防止磁带退化。然而,它们已因DVD和流媒体服务的兴起而逐渐退出历史舞台,成为怀旧的象征。
58 1
如何正确的存储你的 VHS 磁带集?
|
6月前
|
存储 API Windows
11.9 实现磁盘相关操作
如下代码实现了在Windows系统中获取所有磁盘驱动器的信息。具体包括两个函数,一个用于获取驱动器类型,另一个用于获取驱动器空间信息。主函数则调用这两个函数来遍历所有逻辑驱动器并输出相应的信息。在输出驱动器空间信息时,会输出该驱动器的总大小、已用空间以及可用空间。
28 0
|
存储 传感器 Linux
(9)存储和EEPROM管理
(9)存储和EEPROM管理
150 0
|
存储 安全 算法
3.7磁盘存储器
3.7磁盘存储器
76 0
|
监控 内存技术
STC——EEPROM(断电数据保护)
STC——EEPROM(断电数据保护)
177 0
|
安全 数据安全/隐私保护
S140阵列卡看不到磁盘解决
本文基于Dell PowerEdge R340服务器,RAID阵列卡使用S140(这里S表示软件)为例。
813 0