很多時候,我們會在運行某個功能的時候要記錄數據,
例如
我要做個人流量計算器,如果長時間運行又或者換電池等情況,會把珍貴的數據丟失。
所以現在我們用Arduino的EEPROM來及時存儲數據,以便以後調用。
注意
EEPROM的刷寫次數是有限的,並且一個地址只能存儲數值0-255,其實實用性真的不太大
Arduino EEPROM 的設計壽命是 100,000 write/erase cycles (10萬次寫入/清除)
(10萬次也不少,但如果經常需要修改數據,可以選擇使用SD卡的方式)
Arduino
Uno SD卡模塊 (一)獲取SDcard的信息
程序
EEPROM官方庫裏有一些例子,按下圖依次打開可以找到。
可以從例程名字大概可以猜到
eeprom_read eeprom讀取
eeprom_write eeprom寫入
eeprom_clear eeprom清除
還有其他,裏面程序有大概說明,如eeprom_crc,打開有註釋,大意是檢查數據修改是否已修改和是否有損壞。
我先來看看寫入的程序
/*
* EEPROM Write
* EEPROM 寫入
* Stores values read from analog input 0 into the EEPROM.
* 讀取模擬輸入IO口0的數據寫入EEPROM
* These values will stay in the EEPROM when the board is
* 這些數據會保留在EEPROM
* 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) **/
/** EEPROM中的當前地址(即我們要寫入下一個字節)**/
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.
需要除以4,因爲模擬輸入範圍從
0到1023,EEPROM的每個字節只能保持
一個值從0到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的相應字節。
這些數據將在板子裏留下
關掉程序。
***/
EEPROM.write(addr, val); //在地址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.
提前到下一個地址,最後再重新由地址0開始繼續寫入數據。
較大的AVR處理器具有較大的EEPROM大小,例如:
- Arduno Duemilanove:512b EEPROM存儲。
- Arduino Uno:1kb EEPROM存儲。
- Arduino Mega:4kb EEPROM存儲。
可以使用固定值長度,但你必須使用對應的長度。
這將使您的代碼可移植到所有AVR處理器。
***/
addr = addr + 1; //地址addr+1
if (addr == EEPROM.length()) {//如果地址addr 等於EEPROM的大小,則
addr = 0; //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.
由於EEPROM大小爲2的冪,EEPROM地址的包裝(防止溢出)也可以通過按位和長度爲1來執行。
++addr &= EEPROM.length() - 1;
***/
delay(100);
}
在看看讀取的程序
/*
* 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> //加載EEOROM庫
// start reading from the first byte (address 0) of the EEPROM
int address = 0; //定義地址address
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 native USB port only
//等待串口連接。 僅適用於本機USB端口
}
}
void loop() {
// read a byte from the current address of the EEPROM
//從EEPROM的當前地址讀取一個字節
value = EEPROM.read(address);
//在串口輸出當前地址的序列,和對應的1字節數據
Serial.print(address);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
/***
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.
***/
//地址+1,讀取到最後的地址時,又把地址重置爲0重新讀取一遍
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(500);
}
以上是寫與讀,比較常用的。清除就不再列舉了,自己可以看看例子。
清楚的大概思路和寫的一樣,遍歷每一個地址,把數值改成0,就是clear了。
程序思路講解
1
#include <EEPROM.h> //加載EEPROM庫
2
int addr = 0; //定義需要讀取或者寫入的地址
之後可以利用以下函數去進行相關處理
read()
write()
update()
get()
put()
EEPROM[]