惠利特 Heltec LoRa
直播中

春天一满屋

4年用户 364经验值
擅长:嵌入式技术
私信 关注

【lora节点开发板+单通道lora网关组合试用试用体验】EEPROM的读与写

EEPROM (Electrically Erasable Programmable read only memory)是指带电可擦可编程只读存储器。是一种掉电后数据不丢失的存储芯片。 EEPROM 可以在电脑上或专用设备上擦除已有信息,重新编程。一般用在即插即用。
直接上代码,代码里也有一些注解
  1. /*
  2.    EEPROM Write

  3.    Stores values read from analog input 0 into the EEPROM.
  4.    These values will stay in the EEPROM when the board is
  5.    turned off and may be retrieved later by another sketch.
  6. */

  7. #include

  8. // the current address in the EEPROM (i.e. which byte
  9. // we're going to write to next)
  10. int addr = 0;
  11. byte value;

  12. void setup() {
  13.   Serial.begin(115200);
  14.   EEPROM.begin(512);
  15. }

  16. void loop() {
  17.   // need to divide by 4 because analog inputs range from
  18.   // 0 to 1023 and each byte of the EEPROM can only hold a
  19.   // value from 0 to 255.
  20.   int val = 20 / 4;

  21.   // write the value to the appropriate byte of the EEPROM.
  22.   // these values will remain there when the board is
  23.   // turned off.
  24.   //向地址0写数据
  25.   EEPROM.write(addr, val);

  26.   // advance to the next address.  there are 512 bytes in
  27.   // the EEPROM, so go back to 0 when we hit 512.
  28.   // save all changes to the flash.
  29.   addr = addr + 1;
  30.   Serial.print(addr);
  31.   Serial.println();
  32.   if (addr == 512) {
  33.     addr = 0;   
  34.     if (EEPROM.commit()) {
  35.       Serial.println("EEPROM successfully committed");
  36.       //从地址0读数据
  37.       value = EEPROM.read(addr);

  38.       Serial.print(addr);
  39.       Serial.print("t");
  40.       Serial.print(value, DEC);
  41.       Serial.println();
  42.     } else {
  43.       Serial.println("ERROR! EEPROM commit failed");
  44.     }
  45.   }

  46.   delay(100);
  47. }
上图看结果:
0001.png

更多回帖

发帖
×
20
完善资料,
赚取积分