Arduino 中 EEprom 寫入讀取清除


轉自:https://www.arduino.cn/thread-1157-1-1.html

EEPROM (Electrically Erasable Programmable Read-Only Memory),電可擦可編程只讀存儲器--一種掉電后數據不丟失的存儲芯片。 簡而言之就是你想斷電后arduino還要保存一些參數,就使用EEPROM吧。 在各型號的arduino控制器上的AVR芯片均帶有EEPROM,也有外接的EEPROM芯片,常見arduino控制器的EEPROM大小: Arduino UNO、Arduino duemilanove-m328、Zduino m328均使用ATmega328芯片,EEPROM都為1K Arduino duemilanove-m168的EEPROM為512bytes Arduino 2560的EEPROM為4K
下面我們介紹arduino自帶的EEPROM使用方法,arduino的庫已經為我們准備好了EEPROM類庫,我們要使用得先調用EEPROM.h,然后使用write和read方法,即可操作EEPROM。 另:下面的官方例子由於寫成較早,所以講EEPROM的大小都定為了512字節,實際使用中,大家可參照上面所說的EEPROM大小,自行更改。
1.寫入 選擇 File>Examples>EEPROM>eeprom_write

kittenblock中小學創客名師推薦的圖形化編程軟件

 
/*
 
* 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>
   
 
// EEPROM 的當前地址,即你將要寫入的地址,這里就是從0開始寫
 
int addr = 0;
   
 
void setup()
 
{
 
}
   
 
void loop()
 
{
 
   //模擬值讀出后是一個0-1024的值,但每字節的大小為0-255,所以這里將值除以4再存儲到val
 
   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.  there are 512 bytes in
 
   // the EEPROM, so go back to 0 when we hit 512.
 
  addr = addr + 1;
 
   if (addr == 512)
 
    addr = 0;
 
  
 
  delay( 100);
 
}

2.讀取 選擇 File>Examples>EEPROM>eeprom_read

kittenblock中小學創客名師推薦的圖形化編程軟件

 
/*
 
* 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>
   
 
// start reading from the first byte (address 0) of the EEPROM
 
int address = 0;
 
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 Leonardo only
 
  }
 
}
   
 
void loop()
 
{
 
   // read a byte from the current address of the EEPROM
 
  value = EEPROM.read(address);
 
  
 
  Serial.print(address);
 
  Serial.print( "\t");
 
  Serial.print(value, DEC);
 
  Serial.println();
 
  
 
   // advance to the next address of the EEPROM
 
  address = address + 1;
 
  
 
   // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
 
   // on address 512, wrap around to address 0
 
   if (address == 512)
 
    address = 0;
 
   
 
  delay( 500);
 
}

3.清除 選擇 File>Examples>EEPROM>eeprom_clear 清除EEPROM的內容,其實就是把EEPROM中每一個字節寫入0,因為只用清一次零,所以整個程序都在setup部分完成。

kittenblock中小學創客名師推薦的圖形化編程軟件

 
/* * EEPROM Clear
 
*
 
* Sets all of the bytes of the EEPROM to 0.
 
* This example code is in the public domain.
   
 
*/
 
#include <EEPROM.h>
   
 
void setup()
 
{
 
   // 讓EEPROM的512字節內容全部清零
 
   for (int i = 0; i < 512; i++)
 
    EEPROM.write(i, 0);
 
   
 
   // 清零工作完成后,將L燈點亮,提示EEPROM清零完成
 
  digitalWrite( 13, HIGH);
 
}
   
 
void loop()
 
{
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM