0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ESP32: eeprom への読み書き

Last updated at Posted at 2023-02-06

こちらのページと同様のことを行いました。
ESP32-WROOM-32DでEEPROMを使う

image.png

プログラム

eeprom.ino
// ---------------------------------------------------------------------
//	eeprom.ino
//
//					Feb/07/2023
// ---------------------------------------------------------------------
#include <EEPROM.h>
#define	PROGRAM	"eeprom.ino"
#define	VERSION	"2023-2-7 AM 07:30"
#define	KEY	2023

int xx = 0;
int yy = 0;
int zz = 0;
int key = 0;

// ---------------------------------------------------------------------
void setup()
{
	/* 初期設定 */
	Serial.begin(115200);
	delay(3000);
	Serial.println(PROGRAM);
	Serial.println(VERSION);
	Serial.println();

	EEPROM.begin(16);	//(4+4+4+4)バイト

	/* EEPROM読み出し */
	EEPROM.get(0, xx);
	EEPROM.get(4, yy);
	EEPROM.get(8, zz);
	EEPROM.get(12, key);

	/* 読み出した内容を出力 */
	Serial.println(xx);
	Serial.println(yy);
	Serial.println(zz);
	Serial.println(key);

	if (key == KEY)
		{
		xx++;
		yy++;
		zz++;
		}
	else
		{
		xx = 10;
		yy = 20;
		zz = 30;
		key = KEY; 
		}

	/* EEPROM書き込み */
	EEPROM.put(0, xx);
	EEPROM.put(4, yy);
	EEPROM.put(8, zz);
	EEPROM.put(12, key);
	EEPROM.commit();	 //EEPROMに書き込み
}

// ---------------------------------------------------------------------
void loop()
{
	Serial.println(PROGRAM);
	Serial.println(VERSION);

	Serial.printf("xx = %d\tyy = %d\tzz = %d\r\n",xx,yy,zz);

	Serial.println();
	delay(3000);
}

// ---------------------------------------------------------------------

実行結果

$ cu -l /dev/ttyUSB0 -s 115200
Connected.
eeprom.ino
2023-2-7 AM 07:30

16
26
36
2023
eeprom.ino
2023-2-7 AM 07:30
xx = 17	yy = 27	zz = 37

eeprom.ino
2023-2-7 AM 07:30
xx = 17	yy = 27	zz = 37
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?