【问题标题】:How to avoid using EEPROM instead of Flash memory?如何避免使用 EEPROM 而不是 Flash 存储器?
【发布时间】:2016-12-17 13:03:36
【问题描述】:

A 用 Arduino MEGA 制作了一个温度传感器设备。用户可以使用按钮更改变量int templimit。此变量的值被写入 EEPROM,因此在重新启动后它将保持其最后一个值。我的问题是每次重启都会缩短 EEPROM 的使用寿命。

我的问题:有什么方法可以避免从 EEPROM 读取/更新并将变量存储在其他内存上?

保存到 EEPROM:

void save_to_eeprom(unsigned int address, float config) { // (address, value)

  for (byte i = 0; i < sizeof(config); i++) { // size of config is 4
    EEPROM.write(address + i, reinterpret_cast<byte*>(&config)[i]);
  }

从 EEPROM 读取:

float read_from_eeprom(unsigned int address) { //(address)
  float config;
  for (byte i = 0; i < sizeof(config); i++) { // size of config is 4
    reinterpret_cast<byte*>(&config)[i] = EEPROM.read(address + i);
  }
  return config;
}

这就是templimit在重启后保持其最后值的方式,完整代码为:

#include <EEPROM.h>


// this constant won't change:
const int  buttonPin = 8;    // up
const int  buttonPin1 = 3;    // down
const int  buttonPin2 = 2;    // enter


// Variables will change:
int buttonPushCounter;   // counter for the number of button presses
int buttonState;;  // up
int buttonState1;;  // down
int buttonState2;;  // enter
int templimit = read_from_eeprom(20); // temperature limit for alarm

void setup() {
  Serial.begin(9600);
  Serial.println("Update temperature limit if not wait 10 sec for time out");
  updatevalue();
  Serial.println("Done");
  Serial.println(templimit); // temperature limit for alarm

}


void loop() {
  // do nothing
}

int updatevalue(void) {
  int timenow;
  int timepassed;
  int counter = 10;

  timenow = millis();
  while (buttonState == LOW and buttonState1 == LOW and buttonState2 == LOW) { // this loop is just for 10 sec time out
    buttonState2 = digitalRead(buttonPin2); // enter
    buttonState = digitalRead(buttonPin); //up
    buttonState1 = digitalRead(buttonPin1); // down
    timepassed = (millis() - timenow);
    delay(1000);
    Serial.println(counter--);
    if (timepassed >= 10000)
      return 0;
  }
  while (buttonState2 != HIGH) { // do this until enter is pressed

    buttonState2 = digitalRead(buttonPin2); // enter
    buttonState = digitalRead(buttonPin); //up
    buttonState1 = digitalRead(buttonPin1); // down

    if (buttonState == HIGH) { // up
      buttonPushCounter++;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    if (buttonState1 == HIGH) { // down
      buttonPushCounter-- ;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    delay(700);
  }

  save_to_eeprom(20, buttonPushCounter); // save to address 20 tha value it can be negative

}

我的问题:有什么方法可以避免从 EEPROM 读取更新?

【问题讨论】:

    标签: c++ memory arduino


    【解决方案1】:

    EEPROM 是处理这类事情的正确位置。 (我能想到的唯一选择是闪存,它的写入周期更少,所以情况更糟)

    你说“每次重启我都会减少 EEPROM 的使用寿命”,这并不完全正确。缩短 EEPROM 寿命的是写操作。当用户按下回车键时,代码中的写入操作就会发生。因此,您无需写入 EEPROM 即可根据需要重新启动。

    无论如何,如果您希望设置在意外重启后保持不变,我认为您无能为力。然而,一个小的可能改进是:仅当新值与旧值不同时才执行写入。

    EEPROM 有 100 000 次写入周期,因此您应该能够多次更改温度限制而不会失败。

    【讨论】:

    • 我认为从 EEPROM 读取会缩短使用寿命。如果它适合我​​的 porpose ok。
    • 是的,应该是。您可以无限次读取 EEPROM。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    相关资源
    最近更新 更多