“EEPROM:put()”的版本间的差异

来自YFRobotwiki
跳转至: 导航搜索
(以“:EEPROM {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| <font color="olivedrab" size="+3">'''put()'''</font> ...”为内容创建页面)
 
第10行: 第10行:
 
<font color="orange" size="+1">'''描述'''</font>
 
<font color="orange" size="+1">'''描述'''</font>
  
Reads a byte from the EEPROM. Locations that have never been written to have the value of 255.
+
Write any data type or object to the EEPROM.
  
<font color="orange" size="+1">'''Syntax'''</font>
 
  
EEPROM.read(address)
+
<font color="orange" size="+1">'''语法'''</font>
  
<font color="orange" size="+1">'''Parameters'''</font>
+
EEPROM.put(address, data)
  
address: the location to read from, starting from 0 (int)
 
  
<font color="orange" size="+1">'''Returns'''</font>
+
<font color="orange" size="+1">''' 参数'''</font>
  
the value stored in that location (byte)
+
address: the location to write to, starting from 0 (int)
  
<font color="orange" size="+1">'''Example'''</font>
+
data: the data to write, can be a primitive type (eg. float) or a custom struct
 +
 
 +
 
 +
<font color="orange" size="+1">''' 返回值'''</font>
 +
 
 +
A reference to the data passed in
 +
 
 +
 
 +
<font color="orange" size="+1">'''注意'''</font>
 +
 
 +
This function uses [[EEPROM:update()|EEPROM.update()]] to perform the write, so does not rewrites the value if it didn't change.
 +
 
 +
 
 +
<font color="orange" size="+1">'''示例'''</font>
  
 
<pre style="color:dimgray">
 
<pre style="color:dimgray">
 
#include <EEPROM.h>
 
#include <EEPROM.h>
  
int a = 0;
+
struct MyObject{
int value;
+
  float field1;
 +
  byte field2;
 +
  char name[10];
 +
};
 +
 
 +
void setup(){
  
void setup()
 
{
 
 
   Serial.begin(9600);
 
   Serial.begin(9600);
}
+
  while (!Serial) {
 +
    ; // wait for serial port to connect. Needed for Leonardo only
 +
  }
  
void loop()
+
   float f = 123.456f;  //Variable to store in EEPROM.
{
+
  int eeAddress = 0;   //Location we want the data to be put.
   value = EEPROM.read(a);
+
  
   Serial.print(a);
+
   //One simple call, with the address first and the object second.
  Serial.print("\t");
+
   EEPROM.put( eeAddress, f );
  Serial.print(value);
+
   Serial.println();
+
  
   a = a + 1;
+
   Serial.println("Written float data type!");
  
   if (a == 512)
+
   /// put() is designed for use with custom structures too.
    a = 0;
+
  //Data to store.
 +
  MyObject customVar = {
 +
    3.14f,
 +
    65,
 +
    "Working!"
 +
  };
  
   delay(500);
+
   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日 (二) 10:34的版本

EEPROM

put()


描述

Write any data type or object to the EEPROM.


语法

EEPROM.put(address, data)


参数

address: the location to write to, starting from 0 (int)

data: the data to write, can be a primitive type (eg. float) or a custom struct


返回值

A reference to the data passed in


注意

This function uses EEPROM.update() to perform the write, so does not rewrites the value if it didn't change.


示例

#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 */ }


返回EEPROM库

返回Arduino库菜单

返回首页

更多建议和问题欢迎反馈至 YFRobot论坛