极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 26602|回复: 12

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

[复制链接]
发表于 2015-4-15 09:01:13 | 显示全部楼层 |阅读模式
本帖最后由 快乐生活 于 2015-4-15 11:05 编辑

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




其中eepom——put实例如下

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

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

  15. #include <EEPROM.h>

  16. struct MyObject{
  17.   float field1;
  18.   byte field2;
  19.   char name[10];
  20. };

  21. void setup(){

  22.   Serial.begin(9600);
  23.   while (!Serial) {
  24.     ; // wait for serial port to connect. Needed for Leonardo only
  25.   }

  26.   float f = 123.456f;  //Variable to store in EEPROM.
  27.   int eeAddress = 0;   //Location we want the data to be put.
  28.   
  29.   
  30.   //One simple call, with the address first and the object second.
  31.   EEPROM.put( eeAddress, f );
  32.   
  33.   Serial.println("Written float data type!");
  34.   
  35.   /** Put is designed for use with custom structures also. **/
  36.   
  37.   //Data to store.
  38.   MyObject customVar = {
  39.     3.14f,
  40.     65,
  41.     "Working!"
  42.   };

  43.   eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
  44.   
  45.   EEPROM.put( eeAddress, customVar );
  46.   Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" );
  47. }

  48. void loop(){ /* Empty loop */ }
复制代码



eeprom-get 实例

  1. /***
  2.     eeprom_get example.

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

  16. #include <EEPROM.h>

  17. void setup(){
  18.   
  19.   float f = 0.00f;   //Variable to store data read from EEPROM.
  20.   int eeAddress = 0; //EEPROM address to start reading from
  21.   
  22.   Serial.begin( 9600 );
  23.   while (!Serial) {
  24.     ; // wait for serial port to connect. Needed for Leonardo only
  25.   }
  26.   Serial.print( "Read float from EEPROM: " );

  27.   //Get the float data from the EEPROM at position 'eeAddress'
  28.   EEPROM.get( eeAddress, f );
  29.   Serial.println( f, 3 );  //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
  30.   
  31.   /***
  32.     As get also returns a reference to 'f', you can use it inline.
  33.     E.g: Serial.print( EEPROM.get( eeAddress, f ) );
  34.   ***/
  35.   
  36.   /***
  37.     Get can be used with custom structures too.
  38.     I have separated this into an extra function.
  39.   ***/
  40.   
  41.   secondTest(); //Run the next test.
  42. }

  43. struct MyObject{
  44.   float field1;
  45.   byte field2;
  46.   char name[10];
  47. };

  48. void secondTest(){
  49.   int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.

  50.   MyObject customVar; //Variable to store custom object read from EEPROM.
  51.   EEPROM.get( eeAddress, customVar );
  52.   
  53.   Serial.println( "Read custom object from EEPROM: " );
  54.   Serial.println( customVar.field1 );
  55.   Serial.println( customVar.field2 );
  56.   Serial.println( customVar.name );
  57. }

  58. void loop(){ /* Empty loop */ }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-4-15 09:58:19 | 显示全部楼层
谢谢分享学习一下
回复 支持 反对

使用道具 举报

发表于 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
感謝分享.

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

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

使用道具 举报

发表于 2015-4-15 11:15:57 | 显示全部楼层
快乐生活 发表于 2015-4-15 10:54
原来用共同体转换数据,后用循环写入或读出。现在使用标准函数,比较简单了。

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

使用道具 举报

发表于 2015-4-15 11:30:18 | 显示全部楼层
依旧还是不支持中文备注啊~~~~
回复 支持 反对

使用道具 举报

发表于 2015-4-15 13:57:05 | 显示全部楼层
darkorigin 发表于 2015-4-15 11:30
依旧还是不支持中文备注啊~~~~

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

使用道具 举报

发表于 2015-4-15 13:58:29 | 显示全部楼层
darkorigin 发表于 2015-4-15 11:30
依旧还是不支持中文备注啊~~~~

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

使用道具 举报

发表于 2015-4-15 19:56:07 | 显示全部楼层
你好 我发觉 1.6.3 自己检查代码时,怎么不像以前版本一样,会自动黄色框选出错误的地方的?这是为什么,怎样解决
回复 支持 反对

使用道具 举报

发表于 2015-4-16 01:46:22 | 显示全部楼层
darkorigin 发表于 2015-4-15 11:30
依旧还是不支持中文备注啊~~~~

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

使用道具 举报

发表于 2015-4-16 09:19:27 | 显示全部楼层
tsaiwn 发表于 2015-4-16 01:46
应该是输入法的问题
我用繁体中文, 从很久以前版本一直都可在 Arduino IDE 输入中文注释
就是说, 大部分 ...

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

使用道具 举报

 楼主| 发表于 2015-4-16 09:31:00 | 显示全部楼层
tsaiwn 发表于 2015-4-16 01:46
应该是输入法的问题
我用繁体中文, 从很久以前版本一直都可在 Arduino IDE 输入中文注释
就是说, 大部分 ...

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

使用道具 举报

发表于 2015-5-21 09:09:22 | 显示全部楼层
long能用吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 22:32 , Processed in 0.044113 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表