Arduino库教程-EEPROM-EEPROM Get
EEPROM Get(EEPROM获取)
在Arduino和genuino板上的微控制器有512字节的EEPROM存储器:当开发板关闭时(就像一个小型硬盘驱动器)开始记忆(即是保存这些数值)。
这个例子的目的是展示放置和获取的方法怎样提供了一个和写读不同的行为。从EEPROM处获得不同的变量,并检索这些字节的数目(这些数目和变量的字节长度有关)。
硬件要求
- Arduino 或者 Genuino 开发板
电路
这个例子的电路没有额外的连接
图由 Fritzing 软件绘制
原理图
图由 Fritzing 软件绘制
样例代码
/***
eeprom_get example.
This shows how to use the EEPROM.get() method.
To pre-set the EEPROM data, run the example sketch eeprom_put.
This sketch will run without it, however, the values shown
will be shown from what ever is already on the EEPROM.
This may cause the serial object to print out a large string
of garbage if there is no null character inside one of the strings
loaded.
Written by Christopher Andrews 2015
Released under MIT licence.
***/
#include <EEPROM.h>
void setup() {
float f = 0.00f; //Variable to store data read from EEPROM.
int eeAddress = 0; //EEPROM address to start reading from
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Read float from EEPROM: ");
//Get the float data from the EEPROM at position 'eeAddress'
EEPROM.get(eeAddress, f);
Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
/***
As get also returns a reference to 'f', you can use it inline.
E.g: Serial.print( EEPROM.get( eeAddress, f ) );
***/
/***
Get can be used with custom structures too.
I have separated this into an extra function.
***/
secondTest(); //Run the next test.
}
struct MyObject {
float field1;
byte field2;
char name[10];
};
void secondTest() {
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
MyObject customVar; //Variable to store custom object read from EEPROM.
EEPROM.get(eeAddress, customVar);
Serial.println("Read custom object from EEPROM: ");
Serial.println(customVar.field1);
Serial.println(customVar.field2);
Serial.println(customVar.name);
}
void loop() {
/* Empty loop */
}
[Get Code]
更多
- EEPROM.get()
- 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寿命。
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号