|
| 1 | +--- |
| 2 | +title: EEPROM |
| 3 | +icon: |
| 4 | +--- |
| 5 | + |
| 6 | +## 简介 |
| 7 | + |
| 8 | +EEPROM 是一种存储器,其值在电路板断电时保留。在 AirMCU 中,没有内置的 EEPROM,但是可以使用 Flash 模拟 EEPROM。一般来说,我们采用内置 flash 的最后一个 page 扇区(或者是其它可擦写的最小单位)来模拟。 |
| 9 | + |
| 10 | +## API |
| 11 | + |
| 12 | +要使用此库,请在代码顶部包含头文件: |
| 13 | + |
| 14 | +```cpp |
| 15 | +#include <EEPROM.h> |
| 16 | +``` |
| 17 | + |
| 18 | +### read() |
| 19 | + |
| 20 | +从 EEPROM 读取一个字节。 |
| 21 | + |
| 22 | +```cpp |
| 23 | +EEPROM.read(address) |
| 24 | +``` |
| 25 | + |
| 26 | +- `address`:要读取的地址,从 0 开始。 |
| 27 | +- 返回值:读取的字节。 |
| 28 | + |
| 29 | +#### 示例 |
| 30 | + |
| 31 | +```cpp |
| 32 | +#include <EEPROM.h> |
| 33 | + |
| 34 | +int a = 0; |
| 35 | +int value; |
| 36 | + |
| 37 | +void setup() |
| 38 | +{ |
| 39 | + Serial.begin(9600); |
| 40 | +} |
| 41 | + |
| 42 | +void loop() |
| 43 | +{ |
| 44 | + value = EEPROM.read(a); |
| 45 | + |
| 46 | + Serial.print(a); |
| 47 | + Serial.print("\t"); |
| 48 | + Serial.print(value); |
| 49 | + Serial.println(); |
| 50 | + |
| 51 | + a = a + 1; |
| 52 | + |
| 53 | + if (a == 512) |
| 54 | + a = 0; |
| 55 | + |
| 56 | + delay(500); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### write() |
| 61 | + |
| 62 | +将一个字节写入 EEPROM。 |
| 63 | + |
| 64 | +```cpp |
| 65 | +EEPROM.write(address, value) |
| 66 | +``` |
| 67 | + |
| 68 | +- `address`:要写入的地址,从 0 开始。 |
| 69 | +- `value`:要写入的值。 |
| 70 | + |
| 71 | +#### 示例 |
| 72 | + |
| 73 | +```cpp |
| 74 | +#include <EEPROM.h> |
| 75 | + |
| 76 | +void setup() |
| 77 | +{ |
| 78 | + for (int i = 0; i < 255; i++) |
| 79 | + EEPROM.write(i, i); |
| 80 | +} |
| 81 | + |
| 82 | +void loop() |
| 83 | +{ |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### update() |
| 88 | + |
| 89 | +将一个字节写入 EEPROM,但仅在值不同的情况下才写入。 |
| 90 | + |
| 91 | +```cpp |
| 92 | +EEPROM.update(address, value) |
| 93 | +``` |
| 94 | + |
| 95 | +- `address`:要写入的地址,从 0 开始。 |
| 96 | +- `value`:要写入的值。 |
| 97 | + |
| 98 | +#### 示例 |
| 99 | + |
| 100 | +```cpp |
| 101 | +#include <EEPROM.h> |
| 102 | + |
| 103 | +void setup() |
| 104 | +{ |
| 105 | + for (int i = 0; i < 255; i++) { |
| 106 | + // this performs as EEPROM.write(i, i) |
| 107 | + EEPROM.update(i, i); |
| 108 | + } |
| 109 | + for (int i = 0; i < 255; i++) { |
| 110 | + // write value "12" to cell 3 only the first time |
| 111 | + // will not write the cell the remaining 254 times |
| 112 | + EEPROM.update(3, 12); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void loop() |
| 117 | +{ |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### get() |
| 122 | + |
| 123 | +从 EEPROM 读取一个值。 |
| 124 | + |
| 125 | +```cpp |
| 126 | +EEPROM.get(address, value) |
| 127 | +``` |
| 128 | + |
| 129 | +- `address`:要读取的地址,从 0 开始。 |
| 130 | +- `value`要读取的数据,可以是原始类型(例如 float)或自定义结构。 |
| 131 | +- 返回值:对传入数据的引用 |
| 132 | + |
| 133 | +#### 示例 |
| 134 | + |
| 135 | +```cpp |
| 136 | +#include <EEPROM.h> |
| 137 | + |
| 138 | +struct MyObject{ |
| 139 | + float field1; |
| 140 | + byte field2; |
| 141 | + char name[10]; |
| 142 | +}; |
| 143 | + |
| 144 | +void setup(){ |
| 145 | + |
| 146 | + float f = 0.00f; //Variable to store data read from EEPROM. |
| 147 | + int eeAddress = 0; //EEPROM address to start reading from |
| 148 | + |
| 149 | + Serial.begin( 9600 ); |
| 150 | + while (!Serial) { |
| 151 | + ; // wait for serial port to connect. Needed for Leonardo only |
| 152 | + } |
| 153 | + Serial.print( "Read float from EEPROM: " ); |
| 154 | + |
| 155 | + //Get the float data from the EEPROM at position 'eeAddress' |
| 156 | + EEPROM.get( eeAddress, f ); |
| 157 | + Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. |
| 158 | + |
| 159 | + // get() can be used with custom structures too. |
| 160 | + eeAddress = sizeof(float); //Move address to the next byte after float 'f'. |
| 161 | + MyObject customVar; //Variable to store custom object read from EEPROM. |
| 162 | + EEPROM.get( eeAddress, customVar ); |
| 163 | + |
| 164 | + Serial.println( "Read custom object from EEPROM: " ); |
| 165 | + Serial.println( customVar.field1 ); |
| 166 | + Serial.println( customVar.field2 ); |
| 167 | + Serial.println( customVar.name ); |
| 168 | +} |
| 169 | + |
| 170 | +void loop(){ /* Empty loop */ } |
| 171 | +``` |
| 172 | +
|
| 173 | +### put() |
| 174 | +
|
| 175 | +将一个值写入 EEPROM。 |
| 176 | +
|
| 177 | +```cpp |
| 178 | +EEPROM.put(address, value) |
| 179 | +``` |
| 180 | + |
| 181 | +- `address`:要写入的地址,从 0 开始。 |
| 182 | +- `value`要写入的数据,可以是原始类型(例如 float)或自定义结构。 |
| 183 | +- 返回值:对传入数据的引用 |
| 184 | + |
| 185 | +::: note |
| 186 | +注意:此函数使用 EEPROM.update() 执行写入,因此如果值没有更改,则不会重写该值。 |
| 187 | +::: |
| 188 | + |
| 189 | +#### 示例 |
| 190 | + |
| 191 | +```cpp |
| 192 | +#include <EEPROM.h> |
| 193 | + |
| 194 | +struct MyObject { |
| 195 | + float field1; |
| 196 | + byte field2; |
| 197 | + char name[10]; |
| 198 | +}; |
| 199 | + |
| 200 | +void setup() { |
| 201 | + |
| 202 | + Serial.begin(9600); |
| 203 | + while (!Serial) { |
| 204 | + ; // wait for serial port to connect. Needed for native USB port only |
| 205 | + } |
| 206 | + |
| 207 | + float f = 123.456f; //Variable to store in EEPROM. |
| 208 | + int eeAddress = 0; //Location we want the data to be put. |
| 209 | + |
| 210 | + //One simple call, with the address first and the object second. |
| 211 | + EEPROM.put(eeAddress, f); |
| 212 | + |
| 213 | + Serial.println("Written float data type!"); |
| 214 | + |
| 215 | + /** Put is designed for use with custom structures also. **/ |
| 216 | + |
| 217 | + //Data to store. |
| 218 | + MyObject customVar = { |
| 219 | + 3.14f, |
| 220 | + 65, |
| 221 | + "Working!" |
| 222 | + }; |
| 223 | + |
| 224 | + eeAddress += sizeof(float); //Move address to the next byte after float 'f'. |
| 225 | + |
| 226 | + EEPROM.put(eeAddress, customVar); |
| 227 | + Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); |
| 228 | +} |
| 229 | + |
| 230 | +void loop() { /* Empty loop */ } |
| 231 | +``` |
| 232 | +
|
| 233 | +### EEPROM[] |
| 234 | +
|
| 235 | +EEPROM[] 是一个重载了`EEPROM`类的`operator[]`运算符,可以像数组一样使用。 |
| 236 | +
|
| 237 | +该运算符允许像数组一样使用标识符。使用这种方法可以直接读写 EEPROM 单元。 |
| 238 | +
|
| 239 | +```cpp |
| 240 | +EEPROM[address] |
| 241 | +``` |
| 242 | + |
| 243 | +- `address`:要读取的地址,从 0 开始。 |
| 244 | +- 返回值:EEPROM 自身的引用 |
| 245 | + |
| 246 | +#### 示例 |
| 247 | + |
| 248 | +```cpp |
| 249 | +#include <EEPROM.h> |
| 250 | + |
| 251 | +void setup(){ |
| 252 | + |
| 253 | + unsigned char val; |
| 254 | + |
| 255 | + //Read first EEPROM cell. |
| 256 | + val = EEPROM[ 0 ]; |
| 257 | + |
| 258 | + //Write first EEPROM cell. |
| 259 | + EEPROM[ 0 ] = val; |
| 260 | + |
| 261 | + //Compare contents |
| 262 | + if( val == EEPROM[ 0 ] ){ |
| 263 | + //Do something... |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +void loop(){ /* Empty loop */ } |
| 268 | +``` |
| 269 | +
|
| 270 | +### length() |
| 271 | +
|
| 272 | +该函数返回一个无符号整数,其中包含 EEPROM 中的单元数。 |
| 273 | +
|
| 274 | +```cpp |
| 275 | +EEPROM.length() |
| 276 | +``` |
| 277 | + |
| 278 | +- 返回值:EEPROM 中的单元数。类型为`unsigned int`。 |
0 commit comments