前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用EEPROM断电保存数据

使用EEPROM断电保存数据

作者头像
二哈侠
发布2024-04-20 10:04:23
1030
发布2024-04-20 10:04:23
举报
文章被收录于专栏:防止网络攻击防止网络攻击

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

代码语言:javascript
复制
    /*
    * 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

代码语言:javascript
复制
    /*
    * 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部分完成。

代码语言:javascript
复制
    /* * 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()
    {
    }
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.写入
  • 2.读取
  • 3.清除
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com