[求教贴] 请问怎么用Arduino自带的EEPROM存手机号码
是这样的情况,我想把手机号码也就是11位数字存进EEPROM,下次上电后读取里面得号码。有没有人给我举个例。我也看了论坛EEPROM的教程,这个11位数字是存进一个地址还是几个地址呢:): 貌似Arduino里面自带的有例子,使用EEPROM这个类给你一个读的,写的自己找吧
/*
* 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 native USB port 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, when at the end restart at the beginning.
Larger AVR processors have larger EEPROM sizes, E.g:
- Arduno Duemilanove: 512b EEPROM storage.
- Arduino Uno: 1kb EEPROM storage.
- Arduino Mega: 4kb EEPROM storage.
Rather than hard-coding the length, you should use the pre-provided length function.
This will make your code portable to all AVR processors.
***/
address = address + 1;
if (address == EEPROM.length()) {
address = 0;
}
/***
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
EEPROM address is also doable by a bitwise and of the length - 1.
++address &= EEPROM.length() - 1;
***/
delay(500);
} 写的
/*
* 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>
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int addr = 0;
void setup() {
/** Empty 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, when at the end restart at the beginning.
Larger AVR processors have larger EEPROM sizes, E.g:
- Arduno Duemilanove: 512b EEPROM storage.
- Arduino Uno: 1kb EEPROM storage.
- Arduino Mega: 4kb EEPROM storage.
Rather than hard-coding the length, you should use the pre-provided length function.
This will make your code portable to all AVR processors.
***/
addr = addr + 1;
if (addr == EEPROM.length()) {
addr = 0;
}
/***
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
EEPROM address is also doable by a bitwise and of the length - 1.
++addr &= EEPROM.length() - 1;
***/
delay(100);
} zhb1190 发表于 2017-5-8 12:54
写的
/*
谢谢啊,已经在看了:) zhb1190 发表于 2017-5-8 12:54
写的
/*
我还问你一个问题,比如我在EEPROM里面已经写入了号码,下次就在直接读取就可以了吧 这个一点也不难吧````:L kissfly 发表于 2017-5-9 14:07
这个一点也不难吧````
我可以问一下你吗?是不是我只吧号码写一次下次直接读就好了 存储数字有很多方式,最简单粗暴的就是直接换算成ASCII码逐位保存
当然BCD码或者二进制可选
用二进制直接存储需要考虑的就是溢出问题,一个字节8位二进制(0-254),两个字节就是16K,4字节就是0-65535,如此类推,需要9字节,但是实际上,如果不考虑+86这个中国区号以及开头的1(不管是电信、移动、联通,都是1开都的号段)9个字节能搞定了(如果把号段前三位用一个字节取代可以用8位搞定) GGG1101 发表于 2017-5-8 14:12
我还问你一个问题,比如我在EEPROM里面已经写入了号码,下次就在直接读取就可以了吧
对的直接读取就可以了,eeprom的特点就是断点也不会丢失
页:
[1]