Arduino库教程-EEPROM-EEPROM Update
EEPROM Update(EEPROM更新)
在Arduino和genuino板上的微控制器有512字节的EEPROM存储器:当开发板关闭时(就像一个小型硬盘驱动器)开始记忆(即是保存这些数值)。
这个例子的目的是示范仅在本地它和先前写入的内容不同时,用EEPROM.update()方法向EEPROM写入数据。这个解决方案可以节省执行时间,因为每一个写操作需要3.3毫秒;EEPROM也有每个单位置100.000的写周期的限制,因此避免在任何位置重写相同的值将增加EEPROM的整体寿命。
硬件要求
- Arduino 或者 Genuino 开发板
电路
这个例子的电路没有额外的连接
图由 Fritzing 软件绘制
原理图
图由 Fritzing 软件绘制
样例代码
/***
EEPROM Update method
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.
If a value has not changed in the EEPROM, it is not overwritten
which would reduce the life span of the EEPROM unnecessarily.
Released using MIT licence.
***/
#include <EEPROM.h>
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int address = 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;
/***
Update the particular EEPROM cell.
these values will remain there when the board is
turned off.
***/
EEPROM.update(address, val);
/***
The function EEPROM.update(address, val) is equivalent to the following:
if( EEPROM.read(address) != val ){
EEPROM.write(address, 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.
***/
address = address + 1;
if (address == EEPROM.length()) {
address = 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.
++address &= EEPROM.length() - 1;
***/
delay(100);
}
[Get Code]
更多
- EEPROM.update()
- 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寿命。
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号