{"id":12019,"date":"2023-11-20T01:30:49","date_gmt":"2023-11-19T17:30:49","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=12019"},"modified":"2024-05-05T12:31:31","modified_gmt":"2024-05-05T04:31:31","slug":"esp32-eeprom-%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=12019","title":{"rendered":"ESP32 EEPROM \u5f00\u53d1"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#1_%E5%89%8D%E8%A8%80\" >1 \u524d\u8a00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#2_%E7%8E%AF%E5%A2%83\" >2 \u73af\u5883<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#21_%E7%A1%AC%E4%BB%B6\" >2.1 \u786c\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#22_%E8%BD%AF%E4%BB%B6\" >2.2 \u8f6f\u4ef6<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#3_%E6%8E%A2%E7%B4%A2\" >3 \u63a2\u7d22<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#31_%E8%AF%BB%E5%86%99%E9%9A%8F%E6%9C%BA%E6%95%B0\" >3.1 \u8bfb\u5199\u968f\u673a\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#32_%E8%AF%BB%E5%86%99%E5%90%84%E7%A7%8D%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\" >3.2 \u8bfb\u5199\u5404\u79cd\u6570\u636e\u7c7b\u578b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.iyatt.com\/?p=12019\/#33_EEPROM_%E5%AF%B9%E8%B1%A1\" >3.3 EEPROM \u5bf9\u8c61<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"1_%E5%89%8D%E8%A8%80\"><\/span>1 \u524d\u8a00<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u5728\u6709\u4e9b\u5f00\u53d1\u60c5\u666f\u4e0b\u9700\u8981\u4f7f\u7528 EEPROM\uff0c\u6bd4\u5982\u7528\u4e8e\u4fdd\u5b58 WiFi \u7684 SSID \u548c \u5bc6\u7801\uff0c\u4e14\u8981\u6c42\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u8bbe\u7f6e\u4fee\u6539\u3002<br \/>\nESP32 \u5e76\u6ca1\u6709 EEPROM \u786c\u4ef6\u4e0a\u7684\u652f\u6301\uff0c\u4f46\u662f\u63d0\u4f9b\u4e86 API\uff0c\u53ef\u4ee5\u5728 FLASH \u4e0a\u6a21\u62df EEPROM\uff0c\u9ed8\u8ba4\u5927\u5c0f\u4e3a 4096 byte\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"2_%E7%8E%AF%E5%A2%83\"><\/span>2 \u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"21_%E7%A1%AC%E4%BB%B6\"><\/span>2.1 \u786c\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>ESP32-WROOM-32<\/p>\n<h2><span class=\"ez-toc-section\" id=\"22_%E8%BD%AF%E4%BB%B6\"><\/span>2.2 \u8f6f\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Arduino IDE 2.2.1<br \/>\nesp32 2.0.14\uff08\u5f00\u53d1\u677f\uff09<\/p>\n<h1><span class=\"ez-toc-section\" id=\"3_%E6%8E%A2%E7%B4%A2\"><\/span>3 \u63a2\u7d22<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"31_%E8%AF%BB%E5%86%99%E9%9A%8F%E6%9C%BA%E6%95%B0\"><\/span>3.1 \u8bfb\u5199\u968f\u673a\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u901a\u7535\u9996\u5148\u8bfb\u53d6\u4e00\u904d EEPROM\uff0c\u5e76\u663e\u793a\u51fa\u6765\uff0c\u7136\u540e\u518d\u53d6\u968f\u673a\u6570\u5199\u5165 EEPROM\uff0c\u5199\u5b8c\u6700\u540e\u518d\u8bfb\u53d6\u6b64\u6b21\u5199\u5165\u7684\u6570\u636e\u3002\u53ef\u4ee5\u65ad\u7535\u518d\u901a\u7535\uff0c\u9a8c\u8bc1\u5f00\u59cb\u8bfb\u53d6\u5230\u7684\u6570\u636e\u548c\u4e0a\u6b21\u5199\u5165\u7684\u6570\u636e\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;EEPROM.h&gt;\n\nvoid setup()\n{\n    int addr = 0;\n    const int eeprom_size = 64; \/\/ \u521d\u59cb\u5316\u7684 EEPROM \u5927\u5c0f\uff0c\u5fc5\u987b\u5927\u4e8e\u5b9e\u9645\u64cd\u4f5c\u7684\u5927\u5c0f\uff0c\u4e00\u822c\u53ef\u4ee5\u76f4\u63a5\u7533\u8bf7 4096\n\n    Serial.begin(115200);\n    Serial.println(&quot;\u542f\u52a8......&quot;);\n    delay(5000);\n\n    if (!EEPROM.begin(eeprom_size))\n    {\n        Serial.println(&quot;\u521d\u59cb\u5316 EEPROM \u5931\u8d25\uff01&quot;);\n        while (1)\n        {\n            delay(1000);\n        }\n    }\n\n    Serial.println(&quot;\u4ece EEPROM \u4e2d\u8bfb\u53d6\u6570\u636e\uff1a&quot;);\n    for (int i = 0; i &lt; eeprom_size; ++i)\n    {\n        Serial.print(byte(EEPROM.read(i)));\n        Serial.print(&quot; &quot;);\n    }\n    Serial.println();\n\n    Serial.println(&quot;\u6b63\u5728\u5411 EEPROM \u4e2d\u5199\u5165\u968f\u673a\u6570\u636e......&quot;);\n    for (int i = 0; i &lt; eeprom_size; ++i)\n    {\n        int val = byte(random(10020));\n        EEPROM.write(addr++, val);\n        Serial.print(val);\n        Serial.print(&quot; &quot;);\n    }\n    EEPROM.commit(); \/\/ \u5199\u5165\u751f\u6548\n\n    Serial.println();\n    addr = 0;\n\n    Serial.println(&quot;\u8bfb\u53d6\u6b64\u6b21\u5199\u5165\u7684\u6570\u636e\uff1a&quot;);\n    for (int i  = 0; i &lt; eeprom_size; ++i)\n    {\n        Serial.print(byte(EEPROM.read(i)));\n        Serial.print(&quot; &quot;);\n    }\n    Serial.println(&quot;----------------------------------&quot;);\n\n    EEPROM.end(); \/\/ \u76f8\u5f53\u4e8e commit \u540e\uff0c\u518d\u52a0\u91ca\u653e EEPROM \u5bf9\u8c61\n}\n\nvoid loop()\n{\n    delay(1000);\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/11\/image-1700414964848.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1108px; --smush-placeholder-aspect-ratio: 1108\/245;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"32_%E8%AF%BB%E5%86%99%E5%90%84%E7%A7%8D%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\"><\/span>3.2 \u8bfb\u5199\u5404\u79cd\u6570\u636e\u7c7b\u578b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-cpp\">#include &lt;EEPROM.h&gt;\n\nvoid setup()\n{\n    const int eeprom_size = 1000;\n\n    Serial.begin(115200);\n    if (!EEPROM.begin(eeprom_size))\n    {\n        Serial.println(&quot;\u521d\u59cb\u5316 EEPROM \u5931\u8d25\uff01&quot;);\n        while (1)\n        {\n            delay(1000);\n        }\n    }\n\n    int address = 0;\n\n    EEPROM.writeByte(address, -128);\n    address += sizeof(byte);\n\n    EEPROM.writeChar(address, &#039;A&#039;);\n    address += sizeof(char);\n\n    EEPROM.writeUChar(address, 255);\n    address += sizeof(unsigned char);\n\n    EEPROM.writeShort(address, -32768);\n    address += sizeof(short);\n\n    EEPROM.writeUShort(address, 65535);\n    address += sizeof(unsigned short);\n\n    EEPROM.writeInt(address, -2147483648);\n    address += sizeof(int);\n\n    EEPROM.writeUInt(address, 4294967295);\n    address += sizeof(unsigned int);\n\n    EEPROM.writeLong(address, -2147483648);\n    address += sizeof(long);\n\n    EEPROM.writeULong(address, 4294967295);\n    address += sizeof(unsigned long);\n\n    int64_t value = -1223372036854775808LL;\n    EEPROM.writeLong64(address, value);\n    address += sizeof(int64_t);\n\n    uint64_t  Value = 18446744073709551615ULL;\n    EEPROM.writeULong64(address, Value);\n    address += sizeof(uint64_t);\n\n    EEPROM.writeFloat(address, 1234.1234);\n    address += sizeof(float);\n\n    EEPROM.writeDouble(address, 123456789.123456789);\n    address += sizeof(double);\n\n    EEPROM.writeBool(address, true);\n    address += sizeof(bool);\n\n    String sentence = &quot;\u6211\u7231 ESP32\u3002&quot;;\n    EEPROM.writeString(address, sentence);\n    address += sentence.length() + 1;\n\n    char gratitude[] = &quot;\u611f\u8c22\u4e50\u946b\uff01&quot;;\n    EEPROM.writeString(address, gratitude);\n    address += 21;\n\n    EEPROM.commit();\n    address = 0;\n\n    Serial.println(EEPROM.readByte(address));\n    address += sizeof(byte);\n\n    Serial.println((char)EEPROM.readChar(address));\n    address += sizeof(char);\n\n    Serial.println(EEPROM.readUChar(address));\n    address += sizeof(unsigned char);\n\n    Serial.println(EEPROM.readShort(address));\n    address += sizeof(short);\n\n    Serial.println(EEPROM.readUShort(address));\n    address += sizeof(unsigned short);\n\n    Serial.println(EEPROM.readInt(address));\n    address += sizeof(int);\n\n    Serial.println(EEPROM.readUInt(address));\n    address += sizeof(unsigned int);\n\n    Serial.println(EEPROM.readLong(address));\n    address += sizeof(long);\n\n    Serial.println(EEPROM.readULong(address));\n    address += sizeof(unsigned long);\n\n    value = 0;\n    value = EEPROM.readLong64(value);\n    Serial.printf(&quot;0x%08X&quot;, (uint32_t)(value &gt;&gt; 32));\n    Serial.printf(&quot;%08X\\n&quot;, (uint32_t)value);\n    address += sizeof(int64_t);\n\n    Value = 0;\n    Value = EEPROM.readULong64(Value);\n    Serial.printf(&quot;0x%08X&quot;, (uint32_t)(Value &gt;&gt; 32));\n    Serial.printf(&quot;%08X\\n&quot;, (uint32_t)Value);\n    address += sizeof(uint64_t);\n\n    Serial.println(EEPROM.readFloat(address), 4);\n    address += sizeof(float);\n\n    Serial.println(EEPROM.readDouble(address), 8);\n    address += sizeof(double);\n\n    Serial.println(EEPROM.readBool(address));\n    address += sizeof(bool);\n\n    Serial.println(EEPROM.readString(address));\n    address += sentence.length() + 1;\n\n    Serial.println(EEPROM.readString(address));\n\n    EEPROM.end();\n}\n\nvoid loop()\n{\n    delay(1000);\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/11\/image-1700419494319.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 257px; --smush-placeholder-aspect-ratio: 257\/293;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"33_EEPROM_%E5%AF%B9%E8%B1%A1\"><\/span>3.3 EEPROM \u5bf9\u8c61<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-cpp\">#include &lt;EEPROM.h&gt;\n\nEEPROMClass  ec_names(&quot;eeprom0&quot;); \/\/ \u5b9e\u4f8b\u5bf9\u8c61 ec_names\uff0c\u5e76\u547d\u540d\u4e3a eeprom0\nEEPROMClass  ec_height(&quot;eeprom1&quot;);\nEEPROMClass  ec_age(&quot;eeprom2&quot;);\n\nvoid setup()\n{\n    Serial.begin(115200);\n\n    if (!ec_names.begin(0x500)) \/\/ \u521d\u59cb\u5316\u5e76\u8bbe\u7f6e\u8d77\u59cb\u5730\u5740\u4e3a 0x500\n    {\n        Serial.println(&quot;\u521d\u59cb\u5316 names \u5931\u8d25\uff01&quot;);\n        while (1)\n        {\n            delay(1000);\n        }\n    }\n\n    if (!ec_height.begin(0x200))\n    {\n        Serial.println(&quot;\u521d\u59cb\u5316 height \u5931\u8d25\uff01&quot;);\n        while (1)\n        {\n            delay(1000);\n        }\n    }\n\n    if (!ec_age.begin(0x100))\n    {\n        Serial.println(&quot;\u521d\u59cb\u5316 age \u5931\u8d25\uff01&quot;);\n        while (1)\n        {\n            delay(1000);\n        }\n    }\n\n    const char * name = &quot;\u9648\u5f3a&quot;;\n    char rname[32];\n    double height = 1.78;\n    uint32_t age = 69;\n\n    \/\/ \u5206\u522b\u5199\u5165\u5230\u4e09\u4e2a EEPROM \u5bf9\u8c61\u7684 0 \u5730\u5740\u4f4d\u4e0a\n    ec_names.writeString(0, name);\n    ec_height.put(0, height); \/\/ put \u652f\u6301\u6cdb\u578b\n    ec_age.put(0, age);\n    Serial.print(&quot;name: &quot;);\n    Serial.println(name);\n    Serial.print(&quot;height: &quot;);\n    Serial.println(height);\n    Serial.print(&quot;age: &quot;);\n    Serial.println(age);\n    Serial.println(&quot;------------------------------------\\n&quot;);\n\n    \/\/ \u6e05\u7a7a\u53d8\u91cf\n    rname[0] = &#039;\\0&#039;;\n    height = 0;\n    age = 0;\n    Serial.print(&quot;name: &quot;);\n    Serial.println(rname);\n    Serial.print(&quot;height: &quot;);\n    Serial.println(height);\n    Serial.print(&quot;age: &quot;);\n    Serial.println(age);\n    Serial.println(&quot;------------------------------------\\n&quot;);\n\n    \/\/ \u4ece\u4e09\u4e2a EEPROM \u5bf9\u8c61\u5404\u81ea\u7684\u96f6\u4f4d\u8bfb\u53d6\n    ec_names.get(0, rname); \/\/ get \u652f\u6301\u6cdb\u578b\n    ec_height.get(0, height);\n    ec_age.get(0, age);\n    Serial.print(&quot;name: &quot;);\n    Serial.println(rname);\n    Serial.print(&quot;height: &quot;);\n    Serial.println(height);\n    Serial.print(&quot;age: &quot;);\n    Serial.println(age);\n\n    ec_names.end();\n    ec_height.end();\n    ec_age.end();\n    Serial.println(&quot;\u5b8c\u6210\uff01&quot;);\n}\n\nvoid loop()\n{\n    delay(0xFFFFFFFF);\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/11\/image-1700421209143.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 390px; --smush-placeholder-aspect-ratio: 390\/262;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 \u524d\u8a00 \u5728\u6709\u4e9b\u5f00\u53d1\u60c5\u666f\u4e0b\u9700\u8981\u4f7f\u7528 EEPROM\uff0c\u6bd4\u5982\u7528\u4e8e\u4fdd\u5b58 WiFi \u7684 SSID \u548c \u5bc6\u7801\uff0c\u4e14\u8981\u6c42\u53ef\u4ee5\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,604],"tags":[],"class_list":["post-12019","post","type-post","status-publish","format-standard","hentry","category-all","category-esp32"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/12019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12019"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/12019\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}