EEPROMライブラリ

EEPROMライブラリの使い方に関する文書です。EEPROMは、ボードの電源を切っても値を保持するメモリです。


AUTHOR: Arduino、LAST REVISION: 2024/01/04 19:18


AVRベースのArduinoやGenuinoのマイクロコントローラはEEPROMを搭載しています。EEPROMは、ボードの電源を切っても値を保持するメモリです(小さいHDDのようなものです)。このライブラリを使うと、値を読み書きすることができます。

さまざまなArduinoやGenuinoボードでサポートされているマイクロコントローラは、さまざまな容量のEEPROMを搭載しています。ATmega328Pは1024バイト、ATmega168とATmega8は512バイト、ATmega1280とATmega2560は4キロバイト(4096バイト)です。Arduino/Genuino101ボードは1024バイトの容量のエミュレートされたEEPROMを搭載しています。

ライブラリを使うには、以下を記述します。

1
#include <EEPROM.h>

スケッチ例

EEPROMライブラリの全てのスケッチ例を見るには、以下のリンクをクリックしてください。

関数

read()

説明

EEPROMから1バイト読み込む。一度も書き込まれたことのない場所の値は255である。

書式

1
EEPROM.read(address)

引数

address値を読み込む場所。0から始まる。

戻り値

その場所に書き込まれている値。(byte)

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <EEPROM.h>

int a = 0;
int value;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  value = EEPROM.read(a);

  Serial.print(a);
  Serial.print("\t");
  Serial.print(value);
  Serial.println();

  a = a + 1;

  if (a == 512)
    a = 0;

  delay(500);
}

write()

説明

EEPROMに1バイト書き込む。

書式

1
void EEPROMClass::write(int address, uint8_t value);

引数

address値を書き込む場所。0から始まる。
value書き込む値。0から255の整数。

戻り値

なし。

注意: EEPROMへの書き込みは完了するまでに3.3ミリ秒かかる。EEPROMのメモリ書き換え可能回数は10万回程度である。このため、書き込み回数に注意する必要がある。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <EEPROM.h>

void setup()
{
  for (int i = 0; i < 255; i++)
    EEPROM.write(i, i);
}

void loop()
{
}

update()

説明

EEPROMに1バイト書き込む。書き込み先のアドレスにすでに書き込まれている値と、書き込む値が異なる場合にだけ、値が書き込まれる。

書式

1
void EEPROMClass::update(int address, uint8_t value);

引数

address値を書き込む場所。0から始まる。(int)
value書き込む値。0から255の整数。(byte)

戻り値

なし。

注意: EEPROMへの書き込みは完了するまでに3.3ミリ秒かかる。EEPROMのメモリ書き換え可能回数は10万回程度である。このため、書き込み回数に注意する必要がある。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <EEPROM.h>

void setup()
{
  for (int i = 0; i < 255; i++) {
    // this performs as EEPROM.write(i, i)
    EEPROM.update(i, i);
  }
  for (int i = 0; i < 255; i++) {
    // write value "12" to cell 3 only the first time
    // will not write the cell the remaining 254 times
    EEPROM.update(3, 12);
  }
}

void loop()
{
}

get()

説明

EEPROMから任意の型のデータもしくはオブジェクトを読み込む。

書式

1
template< typename T > T & EEPROMClass::get( int address, T &data);

引数

address値を読み込む場所。0から始まる。(int)
data読み込むデータ。プリミティブ型(例: float)もしくは作成した構造体。

戻り値

引数として渡したdataへの参照。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <EEPROM.h>

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

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 Leonardo 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.

  // get() can be used with custom structures too.
  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 */ }

put()

説明

EEPROMに任意の型のデータもしくはオブジェクトを書き込む。

書式

1
template< typename T > const T &EEPROMClass::put( int address, const T &data );

引数

address値を書き込む場所。0から始まる。(int)
data書き込むデータ。プリミティブ型(例: float)もしくは作成した構造体。

戻り値

引数として渡したdataへの参照。

注意: この関数はデータの書き込みに、aref/EEPROM.update() を用いる。このため、データに変更がない場合は書き込みは行わない。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
}

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

EEPROM[]

説明

この演算子は、EEPROMという識別子を配列のように利用可能とする。これを用いることでEEPROMセルを直接読み書きできる。

書式

1
EEPROM[address]

引数

address値を書き込む場所。0から始まる。(int)

戻り値

EEPROMセルへの参照。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <EEPROM.h>

void setup(){

  unsigned char val;

  //Read first EEPROM cell.
  val = EEPROM[ 0 ];

  //Write first EEPROM cell.
  EEPROM[ 0 ] = val;

  //Compare contents
  if( val == EEPROM[ 0 ] ){
    //Do something...
  }
}

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

length()

説明

この関数はEEPROMに含まれるセル長をunsigned intで返す。

文法

1
EEPROM.length()

戻り値

EEPROM内のセル数を示すunsigned int。

オリジナルのページ

https://docs.arduino.cc/learn/built-in-libraries/eeprom

最終更新日

January 6, 2024

inserted by FC2 system