快乐生活 发表于 2015-4-15 09:01:13

arduino-1.6.3 支持float、int、字符串写入EEPROM

本帖最后由 快乐生活 于 2015-4-15 11:05 编辑

arduino从1.6.2开始支持将float、int、字符串写入、读出EEPROM。




其中eepom——put实例如下

/***
    eeprom_put example.
   
    This shows how to use the EEPROM.put() method.
    Also, this sketch will pre-set the EEPROM data for the
    example sketch eeprom_get.
   
    Note, unlike the single byte version EEPROM.write(),
    the put method will use update semantics. As in a byte
    will only be written to the EEPROM if the data is actually
    different.

    Written by Christopher Andrews 2015
    Released under MIT licence.   
***/

#include <EEPROM.h>

struct MyObject{
float field1;
byte field2;
char name;
};

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 also. **/

//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.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" );
}

void loop(){ /* Empty loop */ }


eeprom-get 实例

/***
    eeprom_get example.

    This shows how to use the EEPROM.get() method.
   
    To pre-set the EEPROM data, run the example sketch eeprom_put.
    This sketch will run without it, however, the values shown
    will be shown from what ever is already on the EEPROM.
   
    This may cause the serial object to print out a large string
    of garbage if there is no null character inside one of the strings
    loaded.
   
    Written by Christopher Andrews 2015
    Released under MIT licence.      
***/

#include <EEPROM.h>

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.

/***
    As get also returns a reference to 'f', you can use it inline.
    E.g: Serial.print( EEPROM.get( eeAddress, f ) );
***/

/***
    Get can be used with custom structures too.
    I have separated this into an extra function.
***/

secondTest(); //Run the next test.
}

struct MyObject{
float field1;
byte field2;
char name;
};

void secondTest(){
int 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 */ }

suoma 发表于 2015-4-15 09:58:19

谢谢分享学习一下

Super169 发表于 2015-4-15 10:14:30

本帖最后由 Super169 于 2015-4-15 10:16 编辑

感謝分享.

電腦的儲存, 本來就沒有 data type 的, 全部都是一個一個 byte 去儲存.
只要你把所有變數都看成是 bytes 組合, 所有 data type 也是一樣的.
新加入的 put 及 get 就是幫大家把資料以 (unit_8) 一個個寫進去/讀出來.
同樣的原理, 在其他通訊也適用的.
現在他們提供了, 就方便了大家.

快乐生活 发表于 2015-4-15 10:54:39

Super169 发表于 2015-4-15 10:14 static/image/common/back.gif
感謝分享.

電腦的儲存, 本來就沒有 data type 的, 全部都是一個一個 byte 去儲存.


原来用共同体转换数据,后用循环写入或读出。现在使用标准函数,比较简单了。

Super169 发表于 2015-4-15 11:15:57

快乐生活 发表于 2015-4-15 10:54 static/image/common/back.gif
原来用共同体转换数据,后用循环写入或读出。现在使用标准函数,比较简单了。

方法是沒有變的, 只是現在庫內提供了, 用家就不用自己去處理, 方便了大家.

darkorigin 发表于 2015-4-15 11:30:18

依旧还是不支持中文备注啊~~~~

林定祥 发表于 2015-4-15 13:57:05

darkorigin 发表于 2015-4-15 11:30 static/image/common/back.gif
依旧还是不支持中文备注啊~~~~

能支持的,在“首选项”中设置复杂字体,“编辑器语言”中选简体中文。

林定祥 发表于 2015-4-15 13:58:29

darkorigin 发表于 2015-4-15 11:30 static/image/common/back.gif
依旧还是不支持中文备注啊~~~~

中文备注虽然方便,但是不是一个好习惯,因为今后程序的交流不同地区的语言会混乱。

布列松 发表于 2015-4-15 19:56:07

你好 我发觉 1.6.3 自己检查代码时,怎么不像以前版本一样,会自动黄色框选出错误的地方的?这是为什么,怎样解决

tsaiwn 发表于 2015-4-16 01:46:22

darkorigin 发表于 2015-4-15 11:30 static/image/common/back.gif
依旧还是不支持中文备注啊~~~~

应该是输入法的问题
我用繁体中文, 从很久以前版本一直都可在 Arduino IDE 输入中文注释
就是说, 大部分输入法可输入中文注释
但少部分输入法无法输入中文注释
从 00xx 版本以来一直都是这样

darkorigin 发表于 2015-4-16 09:19:27

tsaiwn 发表于 2015-4-16 01:46 static/image/common/back.gif
应该是输入法的问题
我用繁体中文, 从很久以前版本一直都可在 Arduino IDE 输入中文注释
就是说, 大部分 ...

我记得1.0之前的版本可以用简体中文备注。1.0之后的貌似就都突然不行了。
粘贴进去的依旧可以显示 但是不能编辑

快乐生活 发表于 2015-4-16 09:31:00

tsaiwn 发表于 2015-4-16 01:46 static/image/common/back.gif
应该是输入法的问题
我用繁体中文, 从很久以前版本一直都可在 Arduino IDE 输入中文注释
就是说, 大部分 ...

谢谢提醒,我发现在我的计算机(win8)里面,用搜狗输入法汉字无法输入,但是在别的地方输入后可以黏贴过来。根据你的提示发现系统带的汉字输入没有问题。

lyngao 发表于 2015-5-21 09:09:22

long能用吗?
页: [1]
查看完整版本: arduino-1.6.3 支持float、int、字符串写入EEPROM