签到天数: 866 天 [LV.10]以坛为家III
|
本帖最后由 AllBlue 于 2015-7-29 10:19 编辑
官方EEPROM库的使用-- EEPROM的读写及擦除
EEPROM库语法参考:http://www.yfrobot.com/wiki/index.php?title=EEPROM%E5%BA%93
函数介绍(IDE版本1.6.4):
- read()
- write()
- update()
- get()
- put()
- EEPROM[]
EEPROM (Electrically Erasable Programmable Read-Only Memory),电可擦可编程只读存储器 -- 一种掉电后数据不丢失的存储芯片。它也是一步步进化过来的,ROM最初rom是不能编程,出厂什么内容就永远什么内容,太不方便。后来出现了prom,可以自己写入一次,要是写错了,只能换一片,自认倒霉。人类文明不断进步,终于出现了可多次擦除写入的EPROM,每次擦除要把芯片拿到紫外线上照一下,想一下你往单片机上下了一个程序之后发现有个地方需要加一句话,为此你要把单片机放紫外灯下照半小时,然后才能再下一次,这么折腾一天也改不了几次。历史的车轮不断前进,伟大的EEPROM出现了,拯救了一大批程序员,终于可以随意的修改rom中的内容了。发展到现在,EEPROM出现了很多的变种!!
很多人将它与FLASH闪存( Flash Memory ,简称:Flash)相比较,其实Flash也是EEPROM的一种,它们都能掉电存储数据,而区别有以下几点:
1、 FLASH按扇区操作,EEPROM则按字节操作
2、 FLASH写入时间长,EEPROM写入时间短
3、 FLASH擦写次数少(10000次),EEPROM次数多(1000000次)
4、 FLASH的电路结构简单,成本低,EEPROM工艺复杂,成本高
arduino的控制板上的单片机自带了EEPROM,当然官方也给出了一个库文件,它可以使用户读写EEPROM中的数据。不同型号的arduino有不同大小的EEPROM,例如:
ATmega8和ATmega168 -- 512字节
ATmega328 -- 1024字节
ATmega1280和ATmega2560 -- 4 KB(4096字节)。
官方库文件中也为我们提供了3个示例,有兴趣可以看看,如图:
这3个例子向我们展示了EEPROM库中的两个语法的用法:
1、EEPROM.write():写一个字节到EEPROM(写一个字节需要3.3ms)
语法
EEPROM.write(address,value)
参数
address:写入字节的地址,从0开始(int)
value:写入的字节,范围0 ~ 255(byte)
2、EEPROM.read():读EEPROM 一个字节
语法
EEPROM.read(address)
参数
address:写入字节的地址,从0开始(int)
下面我们来看下这3个例程(我们使用UNO R3,IDE版本为1.0.1,官方例程是以512字节写的,你也可以根据自己用的控制板型号调节相关参数):
第一、eeprom_read,该例程读出512个字节并通过串口打印出来。- #include <EEPROM.h>
- // start reading from the first byte (address 0) of the EEPROM
- int address = 0;
- byte value;
- void setup()
- {
- // initialize serial and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- }
- void loop()
- {
- // read a byte from the current address of the EEPROM
- value = EEPROM.read(address);
-
- Serial.print(address);
- Serial.print("\t");
- Serial.print(value, DEC);
- Serial.println();
-
- // advance to the next address of the EEPROM
- address = address + 1;
-
- // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
- // on address 512, wrap around to address 0
- if (address == 512)
- address = 0;
-
- delay(500);
- }
复制代码 将例程烧写到控制板中,一个没有使用过EEPROM的板子,显示应该和下图中的数据是一样的,都是255:
第二、eeprom_write,该例程使用 EEPROM.write() 语法向EEPROM写入512个字节,程序中我们可以看到,它是将模拟口读出的数控制到0~255范围内,再将这个数字写到EEPROM中。- #include <EEPROM.h>
- // the current address in the EEPROM (i.e. which byte
- // we're going to write to next)
- int addr = 0;
- void setup()
- {
- }
- void loop()
- {
- // need to divide by 4 because analog inputs range from
- // 0 to 1023 and each byte of the EEPROM can only hold a
- // value from 0 to 255.
- int val = analogRead(0) / 4;
-
- // write the value to the appropriate byte of the EEPROM.
- // these values will remain there when the board is
- // turned off.
- EEPROM.write(addr, val);
-
- // advance to the next address. there are 512 bytes in
- // the EEPROM, so go back to 0 when we hit 512.
- addr = addr + 1;
- if (addr == 512)
- addr = 0;
-
- delay(100);
- }
复制代码 下面我们将例程烧写到刚才的控制板中,A0我们不连接任何元件(也就是说A0读到的值是随机的),这样一来我们读到的数据就将不再是255了。
当程序运行一会后,我们再将eeprom_read 例程烧入控制板,再来看看他的数据:
图中我们可以看到,刚才的数据已经完全被改写了。
第三、EEPROM_clear,这个例程就是使用EEPROM.write()语句向EEPROM中写0。- #include <EEPROM.h>
- void setup()
- {
- // write a 0 to all 512 bytes of the EEPROM
- for (int i = 0; i < 512; i++)
- EEPROM.write(i, 0);
-
- // turn the LED on when we're done
- digitalWrite(13, HIGH);
- }
- void loop()
- {
- }
复制代码 写完后,作为提示,L灯会点亮!这时我们再读EEPROM中的数字,理论上它应该都是0 了~~
这样我们基本已经证明了,我们用例程将EEPROM中的数据更改了一番,如果你还觉得不确定,你可以使用EEPROM.write()向EEPROM写固定的数据,例如:写0到地址0、写1到地址1、写2到地址2......然后再将他们读出,打印到串口~~
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|