|
|
发表于 2016-1-26 16:53:28
|
显示全部楼层
- //EEPROM.write()
- //EEPROM.read()
- //EEPROM.put()
- //EEPROM.get()
- //EEPROM.update()//相同则不更改,节约100000的读写次数
- #include <EEPROM.h>
- struct MyObject{
- float field1;
- byte field2;
- char field3[10];
- };
- void setup(){
- float f0 = 123.456789f; //float 和 double 相同精度,6-7位有效数字
- float f1 = 0.00f; //Variable to store data read from EEPROM.
- int eeAddress = 0; //EEPROM address to start reading from
- Serial.begin( 9600 );
- //单个数字测试
- EEPROM.update(eeAddress, f0);
- Serial.println("Written float data type!");
- EEPROM.get( eeAddress, f1 );
- Serial.print( "Read float from EEPROM: " );
- Serial.println( f1, 6 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
- //多字节读取测试
- // get() can be used with custom structures too.
- MyObject customVar0 = {
- 3.14159f,
- 65,
- "Working!"
- };
- eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
- EEPROM.put(eeAddress, customVar0);
-
- MyObject customVar1; //Variable to store custom object read from EEPROM.
- EEPROM.get( eeAddress, customVar1 );
- Serial.println( "Read custom object from EEPROM: " );
- Serial.println( customVar1.field1,6 );
- Serial.println( customVar1.field2 );
- Serial.println( customVar1.field3 );
- }
- void loop(){ /* Empty loop */ }
复制代码 |
|