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

来自YFRobotwiki
跳转至: 导航搜索
(以“:EEPROM {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| <font color="olivedrab" size="+3">'''get()'''</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.
+
Read any data type or object from 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.get(address, data)
 +
 
 +
 
 +
<font color="orange" size="+1">''' 参数'''</font>
  
 
address: the location to read from, starting from 0 (int)
 
address: the location to read from, starting from 0 (int)
  
<font color="orange" size="+1">'''Returns'''</font>
+
data: the data to read, can be a primitive type (eg. float) or a custom struct
  
the value stored in that location (byte)
 
  
<font color="orange" size="+1">'''Example'''</font>
+
<font color="orange" size="+1">''' 返回值'''</font>
 +
 
 +
A reference to the data passed in
 +
 
 +
 
 +
<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);
+
}
+
  
void loop()
+
   float f = 0.00f;  //Variable to store data read from EEPROM.
{
+
  int eeAddress = 0; //EEPROM address to start reading from
   value = EEPROM.read(a);
+
  
   Serial.print(a);
+
   Serial.begin( 9600 );
   Serial.print("\t");
+
   while (!Serial) {
   Serial.print(value);
+
    ; // wait for serial port to connect. Needed for Leonardo only
   Serial.println();
+
   }
 +
   Serial.print( "Read float from EEPROM: " );
  
   a = a + 1;
+
   //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.
  
   if (a == 512)
+
   // get() can be used with custom structures too.
    a = 0;
+
  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 );
  
   delay(500);
+
   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日 (二) 10:31的版本

EEPROM

get()


描述

Read any data type or object from the EEPROM.


语法

EEPROM.get(address, data)


参数

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

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


返回值

A reference to the data passed in


示例

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


返回EEPROM库

返回Arduino库菜单

返回首页

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