求助 合宙ESP32C3 EEPROM操作失败-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 577|回复: 5

[未解决] 求助 合宙ESP32C3 EEPROM操作失败

[复制链接]
发表于 2022-7-2 18:54 | 显示全部楼层 |阅读模式
想要使用板载的4M EEPROM,写了一个最简单的统计启动次数的程序,结果输出一直是0
即使使用EEPROM.begin()和EEPROM.end(),也是为0

  1. #include<EEPROM.h>

  2. void setup()
  3. {
  4.   unsigned char start_time = 0;
  5.   
  6.   //串口初始化
  7.   Serial.begin(115200);
  8.   start_time = EEPROM.read(0);
  9.   Serial.printf("start time = 0x%x\r\n", start_time);
  10.   EEPROM.write(0, start_time+1);
  11. }

  12. void loop()
  13. {
  14.   delay(1000);
  15. }
复制代码
发表于 2022-7-2 21:18 | 显示全部楼层

回帖奖励 +1 金币

EEPROM用之前要begin, 写入后要commit
 楼主| 发表于 2022-7-2 21:37 | 显示全部楼层
XlinliY.Zhang 发表于 2022-7-2 21:18
EEPROM用之前要begin, 写入后要commit

加上begin和commit还是不行,加上end也尝试了,好纠结

  1. #include<EEPROM.h>

  2. void setup()
  3. {
  4.   unsigned char start_time = 0;
  5.   EEPROM.begin(10);
  6.   //串口初始化
  7.   Serial.begin(115200);
  8.   start_time = EEPROM.read(0);
  9.   Serial.printf("start time = 0x%x\r\n", start_time);
  10.   EEPROM.write(0, 0x88);
  11.   EEPROM.commit();
  12.   EEPROM.end();
  13. }

  14. void loop()
  15. {
  16.   delay(1000);
  17. }
复制代码
 楼主| 发表于 2022-7-2 21:45 | 显示全部楼层
运行了官方例程都不行呀,感觉板子有问题~
  1. /*
  2.    EEPROM Write

  3.    Stores random values 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 "EEPROM.h"

  8. // the current address in the EEPROM (i.e. which byte
  9. // we're going to write to next)
  10. int addr = 0;
  11. #define EEPROM_SIZE 64
  12. void setup()
  13. {
  14.   Serial.begin(115200);
  15.   Serial.println("start...");
  16.   if (!EEPROM.begin(EEPROM_SIZE))
  17.   {
  18.     Serial.println("failed to initialise EEPROM"); delay(1000000);
  19.   }
  20.   Serial.println(" bytes read from Flash . Values are:");
  21.   for (int i = 0; i < EEPROM_SIZE; i++)
  22.   {
  23.     Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
  24.   }
  25.   Serial.println();
  26.   Serial.println("writing random n. in memory");
  27. }

  28. void loop()
  29. {
  30.   // need to divide by 4 because analog inputs range from
  31.   // 0 to 1023 and each byte of the EEPROM can only hold a
  32.   // value from 0 to 255.
  33.   // int val = analogRead(10) / 4;
  34.   int val = byte(random(10020));
  35.   // write the value to the appropriate byte of the EEPROM.
  36.   // these values will remain there when the board is
  37.   // turned off.
  38.   EEPROM.write(addr, val);
  39.   Serial.print(val); Serial.print(" ");
  40.   // advance to the next address.  there are 512 bytes in
  41.   // the EEPROM, so go back to 0 when we hit 512.
  42.   // save all changes to the flash.
  43.   addr = addr + 1;
  44.   if (addr == EEPROM_SIZE)
  45.   {
  46.     Serial.println();
  47.     addr = 0;
  48.     EEPROM.commit();
  49.     Serial.print(EEPROM_SIZE);
  50.     Serial.println(" bytes written on Flash . Values are:");
  51.     for (int i = 0; i < EEPROM_SIZE; i++)
  52.     {
  53.       Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
  54.     }
  55.     Serial.println(); Serial.println("----------------------------------");
  56.   }

  57.   delay(100);
  58. }
复制代码
发表于 2022-7-9 17:12 | 显示全部楼层
更新到 2.0.4  就正常了,以前的2.0.3 不能存储。已经试验存储已经OK了!

点评

确实可以,十分感谢  发表于 2022-7-23 10:52
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-29 23:47 , Processed in 0.080480 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表