最新消息:

Arduino库教程-EEPROM-EEPROM Write

Arduino 少儿编程 1637浏览 0评论
Arduino库教程

EEPROM Write(写入EEPROM)

  • 在Arduino和genuino板上的微控制器有512字节的EEPROM存储器:当开发板关闭时(就像一个小型硬盘驱动器)开始记忆(即是保存这些数值)。

  • 这个例子说明了如何通过EEPROM.write() 函数保存从模拟引脚A0读取的数据到EEPROM里。当开发板关闭时,这些数值将会保存在EEPROM里,并且可以被稍后其他的程序恢复。

硬件要求

  • Arduino 或者 Genuino 开发板

电路

这个例子的电路没有额外的连接
Arduino库教程-EEPROM-EEPROM Write
图由 Fritzing 软件绘制

原理图

Arduino库教程-EEPROM-EEPROM Write
图由 Fritzing 软件绘制

样例代码

/*
 * 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>

/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int addr = 0;

void setup() {
  /** Empty setup. **/
}

void loop() {
  /***
    Need to divide by 4 because analog inputs range from
    0 to 1023 and each byte of the EEPROM can only hold a
    value from 0 to 255.
  ***/

  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, when at the end restart at the beginning.

    Larger AVR processors have larger EEPROM sizes, E.g:
    - Arduno Duemilanove: 512b EEPROM storage.
    - Arduino Uno:        1kb EEPROM storage.
    - Arduino Mega:       4kb EEPROM storage.

    Rather than hard-coding the length, you should use the pre-provided length function.
    This will make your code portable to all AVR processors.
  ***/
  addr = addr + 1;
  if (addr == EEPROM.length()) {
    addr = 0;
  }

  /***
    As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
    EEPROM address is also doable by a bitwise and of the length - 1.

    ++addr &= EEPROM.length() - 1;
  ***/


  delay(100);
}

[Get Code]
更多

  • EEPROM.write()
  • analogRead()
  • if()
  • EEPROM library reference
  • EEPROM Clear: 用0来填满EEPROM里面的数据。
  • EEPROM Read: 读取EEPROM,并且发送它的值到电脑。
  • EEPROM Write: 保存模拟输入引脚的值到EEPROM。
  • EEPROM Crc: 将EEPROM内容里的CRC当作数组分析。
  • EEPROM Get: 从EEPROM获得一个值,并作为float格式串行打印。
  • EEPROM Iteration: 明白怎样到达EEPROM存储本地。
  • EEPROM Put: 用变量来把一些数值放到EEPROM里。
  • EEPROM Update: 保存从A0读取的数值到EEPROM里,仅在不同的时候写入,以延长EEPROM寿命。

您必须 登录 才能发表评论!