“EEPROM:get()”的版本间的差异
来自YFRobotwiki
(以“:EEPROM {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| <font color="olivedrab" size="+3">'''get()'''</font> ...”为内容创建页面) |
|||
| (未显示1个用户的2个中间版本) | |||
| 第10行: | 第10行: | ||
<font color="orange" size="+1">'''描述'''</font> | <font color="orange" size="+1">'''描述'''</font> | ||
| − | + | 从EEPROM中读取任何类型的数据或者对象。 | |
| − | |||
| − | + | <font color="orange" size="+1">'''语法'''</font> | |
| − | + | EEPROM.get(address, data) | |
| − | |||
| − | <font color="orange" size="+1">''' | + | <font color="orange" size="+1">''' 参数'''</font> |
| − | + | address: 读取的位置,从0开始 (int 整型) | |
| − | <font color="orange" size="+1">''' | + | data: 读取的数据,可以是一个基本类型(例如:float 浮点型)或者是一个自定义的结构体。 |
| + | |||
| + | |||
| + | <font color="orange" size="+1">''' 返回值'''</font> | ||
| + | |||
| + | 数据传递的引用 | ||
| + | |||
| + | |||
| + | <font color="orange" size="+1">'''示例'''</font> | ||
<pre style="color:dimgray"> | <pre style="color:dimgray"> | ||
#include <EEPROM.h> | #include <EEPROM.h> | ||
| − | + | struct MyObject{ | |
| − | + | float field1; | |
| + | byte field2; | ||
| + | char name[10]; | ||
| + | }; | ||
| − | void setup() | + | void setup(){ |
| − | { | + | |
| − | + | ||
| − | + | ||
| − | + | float f = 0.00f; //Variable to store data read from EEPROM. | |
| − | + | int eeAddress = 0; //EEPROM address to start reading from | |
| − | + | ||
| − | Serial. | + | Serial.begin( 9600 ); |
| − | Serial | + | while (!Serial) { |
| − | + | ; // wait for serial port to connect. Needed for Leonardo only | |
| − | Serial. | + | } |
| + | Serial.print( "Read float from EEPROM: " ); | ||
| − | + | //Get the float data from the EEPROM at position 'eeAddress' | |
| + | EEPROM.get( eeAddress, f ); | ||
| + | Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. | ||
| − | + | // get() can be used with custom structures too. | |
| − | + | eeAddress = sizeof(float); //Move address to the next byte after float 'f'. | |
| + | MyObject customVar; //Variable to store custom object read from EEPROM. | ||
| + | EEPROM.get( eeAddress, customVar ); | ||
| − | + | Serial.println( "Read custom object from EEPROM: " ); | |
| + | Serial.println( customVar.field1 ); | ||
| + | Serial.println( customVar.field2 ); | ||
| + | Serial.println( customVar.name ); | ||
} | } | ||
| + | |||
| + | void loop(){ /* Empty loop */ } | ||
</pre> | </pre> | ||
2015年7月14日 (二) 11:30的最后版本
|
get()
描述 从EEPROM中读取任何类型的数据或者对象。
EEPROM.get(address, data)
address: 读取的位置,从0开始 (int 整型) data: 读取的数据,可以是一个基本类型(例如:float 浮点型)或者是一个自定义的结构体。
数据传递的引用
#include <EEPROM.h>
struct MyObject{
float field1;
byte field2;
char name[10];
};
void setup(){
float f = 0.00f; //Variable to store data read from EEPROM.
int eeAddress = 0; //EEPROM address to start reading from
Serial.begin( 9600 );
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print( "Read float from EEPROM: " );
//Get the float data from the EEPROM at position 'eeAddress'
EEPROM.get( eeAddress, f );
Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
// get() can be used with custom structures too.
eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
MyObject customVar; //Variable to store custom object read from EEPROM.
EEPROM.get( eeAddress, customVar );
Serial.println( "Read custom object from EEPROM: " );
Serial.println( customVar.field1 );
Serial.println( customVar.field2 );
Serial.println( customVar.name );
}
void loop(){ /* Empty loop */ }
|
更多建议和问题欢迎反馈至 YFRobot论坛