关于EEPROM的操作

EEPROM写入:

/* * EEPROM Clear
 *EEPROM写入
 *关于EEPROM说明,arduino UNO,flash只有1k,即1024个存储地址
 *地址位:0-1023,要写入数据类型:char byte
 * 更新时间:2018/4/20
 * by沉默
 
 */
#include <EEPROM.h>
int addr
char var
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
   EEPROM.write(addr, val);//addr地址val 数值

}

读取:

/*
 * EEPROM 读取
 * 也是按地址读取
 *
 */
 
#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 == 1024)
    address = 0;
     
  delay(100);
}

清零:

/* * 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 < 1024; i++)
    EEPROM.write(i, 0);
     
  // 清零工作完成后,将L灯点亮,提示EEPROM清零完成
  digitalWrite(13, HIGH);
}
 
void loop()
{
}

 

加入讨论

电子邮件地址不会被公开。 必填项已用*标注