Arduino-save data using EEPROM

EEPROM (Electrically Erasable Programmable Read-Only Memory), electrically erasable programmable read-only memory-a memory chip that does not lose data after power failure.
arduino has prepared the EEPROM library "EEPROM.h" for us.
We need to call EEPROM.h first, and then use write and read methods to operate EEPROM.

1. Writing method: EEPROM.write(address, value)

#include <EEPROM.h>
int addr = 0;
void setup()
{
}
void loop()
{
  //每次写入1个字节,注意不要溢出。
  //比如要保存模拟值,模拟值读出后是一个0-1024的值,但每字节的大小为0-255,容纳不了,所以这里可以变通一下,将值除以4再存储到val,就能够容纳了。使用的时候读出来再乘以4还原回来。  
  int val = analogRead(0) / 4;
  EEPROM.write(addr, val);

  addr = addr + 1;
  if (addr == 512)  // 当读写位置到了最大容量时,注意不要溢出。
    addr = 0;
  delay(100);
}

2. Reading method: EEPROM.read(address)

#include <EEPROM.h>
int address = 0;
byte value;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // 读入一个字节
  value = EEPROM.read(address);

  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();

  // 到下一个地址
  address = address + 1;

  // 注意地址不要溢出
  if (address == 512)
    address = 0;

  delay(500);
}

3. Clear data: write 0 to each address

#include <EEPROM.h>
void setup()
{
  // 让EEPROM的512字节内容全部清零
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, 0);
}
void loop()
{
}

4. Use the union to quickly store various types of data to EEPROM

First understand what is [union]: Put several different (different types, but the same length) variables into the same memory segment, and jointly occupy this memory data, so that the memory data of each variable is always consistent.

You may have discovered that using the EEPROM API provided by Arduino, you can only store [byte type] data in the EEPROM, that is, you can only write 1 byte at a time. If you want to store multiple bytes of data types, what do you need to do?

For example: a float type data needs to occupy 4 bytes of storage space. Therefore, we can split a float into 4 bytes, and then write the EEPROM byte by byte to achieve the purpose of saving the float data. But manual calculation is more troublesome.
Here we use unions to quickly split the float data into 4 bytes through other data results.

First define a union structure named data. There are two different types of member variables in the union:

union data
 {
   float a;  // 浮点型,占用4个字节
   byte b[4];  // 字节型数组,占用4个字节。
   // 这两个类型的变量都是占用4个字节,且是相同的4个字节,所以能始终保持数据一致,其中一个变了,另一个也随之改变。
 };

Declare a variable col of data type:

data col;

Now you can access the float type member a in this union through col.a, and you can also access the byte type member b in this union through col.b.
col.a and col.b together occupy a four-byte address.
After assigning a value to col.a, the float can be split into 4 bytes through col.b.

(1) Write through union:

#include <EEPROM.h>
union data
{
  float a;
  byte b[4];
};
data col;
int addr = 0;

void setup()
{
  col.a=987.65;  // 通过 a 赋值
  for(int i=0;i<4;i++)
  EEPROM.write(i, col.b[i]);  // 通过 b 拆分为4个字节 
}

void loop()
{
}

(2) Read through union:

#include <EEPROM.h>
union data
{
  float a;
  byte b[4];
};
data col;
int addr = 0;

void setup()
{
  Serial.begin(9600);   
  for(int i=0;i<4;i++)
  col.b[i]=EEPROM.read(i);
  Serial.println(col.a);
}

void loop()
{
}

Guess you like

Origin blog.csdn.net/sdlgq/article/details/51931900