“EEPROM:put()”的版本间的差异
来自YFRobotwiki
(以“:EEPROM {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| <font color="olivedrab" size="+3">'''put()'''</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.put(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> | ||
| + | |||
| + | 这个函数使用[[EEPROM:update()|EEPROM.update()]]来执行写数据,所以如果值没有改变不会重复写入的。 | ||
| + | |||
| + | |||
| + | <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(){ | ||
| − | |||
| − | |||
Serial.begin(9600); | Serial.begin(9600); | ||
| − | } | + | while (!Serial) { |
| + | ; // wait for serial port to connect. Needed for Leonardo only | ||
| + | } | ||
| − | + | float f = 123.456f; //Variable to store in EEPROM. | |
| − | + | int eeAddress = 0; //Location we want the data to be put. | |
| − | + | ||
| − | + | //One simple call, with the address first and the object second. | |
| − | + | EEPROM.put( eeAddress, f ); | |
| − | + | ||
| − | + | ||
| − | + | Serial.println("Written float data type!"); | |
| − | + | /// put() is designed for use with custom structures too. | |
| − | + | //Data to store. | |
| + | MyObject customVar = { | ||
| + | 3.14f, | ||
| + | 65, | ||
| + | "Working!" | ||
| + | }; | ||
| − | + | eeAddress += sizeof(float); //Move address to the next byte after float 'f'. | |
| + | |||
| + | EEPROM.put( eeAddress, customVar ); | ||
| + | Serial.println( "Written custom data type!") | ||
| + | Serial.println( "View the example sketch eeprom_get to see how you can retrieve the values!" ); | ||
} | } | ||
| + | |||
| + | void loop(){ /* Empty loop */ } | ||
</pre> | </pre> | ||
2015年7月14日 (二) 17:24的最后版本
|
put()
描述 向EEPROM中写入任何类型数据或对象。
EEPROM.put(address, data)
address: 写入的位置,从0开始 (int 整型) data: 写入的数据,可以是一个基本类型(例如:float 浮点型)或者是一个自定义的结构体。
数据传递的引用
这个函数使用EEPROM.update()来执行写数据,所以如果值没有改变不会重复写入的。
#include <EEPROM.h>
struct MyObject{
float field1;
byte field2;
char name[10];
};
void setup(){
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
float f = 123.456f; //Variable to store in EEPROM.
int eeAddress = 0; //Location we want the data to be put.
//One simple call, with the address first and the object second.
EEPROM.put( eeAddress, f );
Serial.println("Written float data type!");
/// put() is designed for use with custom structures too.
//Data to store.
MyObject customVar = {
3.14f,
65,
"Working!"
};
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.put( eeAddress, customVar );
Serial.println( "Written custom data type!")
Serial.println( "View the example sketch eeprom_get to see how you can retrieve the values!" );
}
void loop(){ /* Empty loop */ }
|
更多建议和问题欢迎反馈至 YFRobot论坛