Arduino 中 EEprom 写入读取清除
转自:https://www.arduino.cn/thread-1157-1-1.html
EEPROM (Electrically Erasable Programmable Read-Only Memory),电可擦可编程只读存储器--一种掉电后数据不丢失的存储芯片。 简而言之就是你想断电后arduino还要保存一些参数,就使用EEPROM吧。 在各型号的arduino控制器上的AVR芯片均带有EEPROM,也有外接的EEPROM芯片,常见arduino控制器的EEPROM大小: Arduino UNO、Arduino duemilanove-m328、Zduino m328均使用ATmega328芯片,EEPROM都为1K Arduino duemilanove-m168的EEPROM为512bytes Arduino 2560的EEPROM为4K
下面我们介绍arduino自带的EEPROM使用方法,arduino的库已经为我们准备好了EEPROM类库,我们要使用得先调用EEPROM.h,然后使用write和read方法,即可操作EEPROM。 另:下面的官方例子由于写成较早,所以讲EEPROM的大小都定为了512字节,实际使用中,大家可参照上面所说的EEPROM大小,自行更改。
1.写入 选择 File>Examples>EEPROM>eeprom_write
|
/*
|
|
|
* EEPROM Write
|
|
|
*
|
|
|
* Stores values read from analog input 0 into the EEPROM.
|
|
|
* These values will stay in the EEPROM when the board is
|
|
|
* turned off and may be retrieved later by another sketch.
|
|
|
*/
|
|
|
#include <EEPROM.h>
|
|
|
// EEPROM 的当前地址,即你将要写入的地址,这里就是从0开始写
|
|
|
int addr = 0;
|
|
|
void setup()
|
|
|
{
|
|
|
}
|
|
|
void loop()
|
|
|
{
|
|
|
//模拟值读出后是一个0-1024的值,但每字节的大小为0-255,所以这里将值除以4再存储到val
|
|
|
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);
|
|
|
}
|
2.读取 选择 File>Examples>EEPROM>eeprom_read
|
/*
|
|
|
* EEPROM Read
|
|
|
*
|
|
|
* Reads the value of each byte of the EEPROM and prints it
|
|
|
* to the computer.
|
|
|
* This example code is in the public domain.
|
|
|
*/
|
|
|
#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);
|
|
|
}
|
3.清除 选择 File>Examples>EEPROM>eeprom_clear 清除EEPROM的内容,其实就是把EEPROM中每一个字节写入0,因为只用清一次零,所以整个程序都在setup部分完成。
|
/* * EEPROM Clear
|
|
|
*
|
|
|
* Sets all of the bytes of the EEPROM to 0.
|
|
|
* This example code is in the public domain.
|
|
|
*/
|
|
|
#include <EEPROM.h>
|
|
|
void setup()
|
|
|
{
|
|
|
// 让EEPROM的512字节内容全部清零
|
|
|
for (int i = 0; i < 512; i++)
|
|
|
EEPROM.write(i, 0);
|
|
|
|
|
|
// 清零工作完成后,将L灯点亮,提示EEPROM清零完成
|
|
|
digitalWrite(13, HIGH);
|
|
|
}
|
|
|
void loop()
|
|
|
{
|
|
|
}
|
Arduino 中 EEprom 写入读取清除的更多相关文章
- arduino中SCoop库的简单应用案例
转载:https://www.csdn.net/gather_27/MtTaggzsMDExMS1ibG9n.html arduino中SCoop库的简单应用案例首先这篇文章来在视频https://v ...
- spark中数据的读取与保存
1.文本文件 (1)读取文本文件 JavaRDD<String> input =sc.textFile(dir) (2)保存文本文件 result.saveAsTextFile(dir)) ...
- C#中Cookies的读取
C#中Cookies的读取 链接: 一 .写入Cookie 1. Name 和 Value 属性由程序设定,默认值都是空引用. 2. Domain属性的默认值为当前URL的域名部分,不管发出这个c ...
- java:OutputStream和InputStream 输出输入流,FileOutputStream,FileInputStream写入读取流
1.在java中stream代表一种数据流(源),javaio的底层数据元,---(想像成水龙头)2.任何有能力产生数据流(源)的javaio对象就可以看作是一个InputStream对象既然它能产生 ...
- net快速写入/读取大量数据Postgresql
Postgresql快速写入/读取大量数据 http://www.cnblogs.com/podolski/p/7152144.html 环境及测试 使用.net驱动npgsql连接post数据库.配 ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- .NET 5/.NET Core使用EF Core 5连接MySQL数据库写入/读取数据示例教程
本文首发于<.NET 5/.NET Core使用EF Core 5(Entity Framework Core)连接MySQL数据库写入/读取数据示例教程> 前言 在.NET Core/. ...
- flink---实时项目--day02-----1. 解析参数工具类 2. Flink工具类封装 3. 日志采集架构图 4. 测流输出 5. 将kafka中数据写入HDFS 6 KafkaProducer的使用 7 练习
1. 解析参数工具类(ParameterTool) 该类提供了从不同数据源读取和解析程序参数的简单实用方法,其解析args时,只能支持单只参数. 用来解析main方法传入参数的工具类 public c ...
- 大数据学习day34---spark14------1 redis的事务(pipeline)测试 ,2. 利用redis的pipeline实现数据统计的exactlyonce ,3 SparkStreaming中数据写入Hbase实现ExactlyOnce, 4.Spark StandAlone的执行模式,5 spark on yarn
1 redis的事务(pipeline)测试 Redis本身对数据进行操作,单条命令是原子性的,但事务不保证原子性,且没有回滚.事务中任何命令执行失败,其余的命令仍会被执行,将Redis的多个操作放到 ...
随机推荐
- Android开发之模拟器genymotion安装apk出现错误: Install_failed_invalid_URI
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. install failed invalid uri Android开发之模拟器ge ...
- sdf文件可以通过database net4工具升级版本
用database .net4工具打开数据库后,右键数据库->数据库工具->upgrade to->to 4.0 or to 3.5; 可以用来判断数据库版本及是否要升级.
- Mysql表,列,库的增删查改
下面是我总结的一些基础的sql知识,主要是为了以后更好的查阅和帮助其他初学的人,同时记录自己的成长,还写了一点稍有难度的sql面试题级别的题目,好了废话不多说,见真题... #创建数据库 CREATE ...
- 想在java接口自动化里用上Python的requests?这样做就可以了
相信现在很多的公司自动化测试重点都在接口层,因为接口测试更加接近代码底层,相对于UI自动化,接口自动化有着开发更快.覆盖更全.回报率高等优点. 接口自动化代码实现不难,本质上就是代码模拟发送请求,然后 ...
- hdu6075 2019CCPC网络选拔赛1004 path
题意:给定一个带权有向图,有q组询问,每次询问在有向图的所有路径中,第k小的路径权值 解题思路:因为k最大只有5e4,考虑暴力搜索出前maxk小的路径并用数组记录权值,然后就可以O(1)查询. 具体实 ...
- Zabbix Agent升级
最近对Zabbix Server进行了升级,所以陆陆续续对Zabbix Agent也做了升级,下面是这几天工作的一个小结,鉴于经验有限和认知有限等各方面因素,下文很难面面俱到,如有疏漏或不足之处, ...
- 转载:SQL优化的主旨
如果把查询看作是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间. 如果要优化查询,实际上要优化其子任务, 要么消除其中一些子任务, 要么减少子任务的执行次数, 要么让子任务执行得更快 ...
- pwnable.kr之input
连接到远程服务器:ssh input2@pwnable.kr -p2222 查看题目所给的代码,根据题目的要求我们要给出所有符合条件的输入才能拿到flag,本来想在输入上动点歪脑筋,结果输入有字节数的 ...
- MongoDB基础总结
1.数据可基本操作 1. 创建数据库 use databaseName 选择一个数据库,如果数据库不存在就自动创建一个数据库 只有向数据库中插入数据时,数据库才会被真实创建出来,而当数据库中没有数据 ...
- 使用镜像安装cygwin、gcc并配置CLion IDE -2020.09.12
使用镜像安装cygwin.gcc并配置CLion IDE -2020.09.12 Cygwin 官网:http://www.cygwin.com/ 下载64bit安装器,并打开选择next 尽量不要装 ...
