最新消息:

Arduino库教程-EEPROM-EEPROM Put

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

EEPROM Put

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

  • 这个例子的目的是示范怎样用EEPROM.put()方法向EEPROM写入数据,只在它和先前被写入内容时用EEPROM.update() 写入数据。写入的字节数,和数据类型或要写入变量的自定义结构相关。

硬件要求

  • Arduino 或者 Genuino 开发板

电路

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

原理图

请输入图片描述
图由 Fritzing 软件绘制

样例代码

/***
    eeprom_put example.

    This shows how to use the EEPROM.put() method.
    Also, this sketch will pre-set the EEPROM data for the
    example sketch eeprom_get.

    Note, unlike the single byte version EEPROM.write(),
    the put method will use update semantics. As in a byte
    will only be written to the EEPROM if the data is actually
    different.

    Written by Christopher Andrews 2015
    Released under MIT licence.
***/

#include <EEPROM.h>

struct MyObject {
  float field1;
  byte field2;
  char name[10];
};

void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  float f = 123.456f;  //Variable to store in EEPROM.
  int eeAddress = 0;   //Location we want the data to be put.


  //One simple call, with the address first and the object second.
  EEPROM.put(eeAddress, f);

  Serial.println("Written float data type!");

  /** Put is designed for use with custom structures also. **/

  //Data to store.
  MyObject customVar = {
    3.14f,
    65,
    "Working!"
  };

  eeAddress += sizeof(float); //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, customVar);
  Serial.print("Written custom data type! nnView the example sketch eeprom_get to see how you can retrieve the values!");
}

void loop() {
  /* Empty loop */
}

[Get Code]
更多

  • EEPROM library reference
  • EEPROM Clear: 清理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寿命。

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