Skip to content

Commit 25cab43

Browse files
committed
EEPROM examples
1 parent 900083e commit 25cab43

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* EEPROM Clear
3+
*
4+
* Sets all of the bytes of the EEPROM to 0.
5+
* This example code is in the public domain.
6+
7+
*/
8+
9+
#include <EEPROM.h>
10+
11+
void setup()
12+
{
13+
EEPROM.begin(512);
14+
// write a 0 to all 512 bytes of the EEPROM
15+
for (int i = 0; i < 512; i++)
16+
EEPROM.write(i, 0);
17+
18+
// turn the LED on when we're done
19+
digitalWrite(13, HIGH);
20+
EEPROM.end();
21+
}
22+
23+
void loop()
24+
{
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* EEPROM Read
3+
*
4+
* Reads the value of each byte of the EEPROM and prints it
5+
* to the computer.
6+
* This example code is in the public domain.
7+
*/
8+
9+
#include <EEPROM.h>
10+
11+
// start reading from the first byte (address 0) of the EEPROM
12+
int address = 0;
13+
byte value;
14+
15+
void setup()
16+
{
17+
// initialize serial and wait for port to open:
18+
Serial.begin(9600);
19+
while (!Serial) {
20+
; // wait for serial port to connect. Needed for Leonardo only
21+
}
22+
EEPROM.begin(512);
23+
}
24+
25+
void loop()
26+
{
27+
// read a byte from the current address of the EEPROM
28+
value = EEPROM.read(address);
29+
30+
Serial.print(address);
31+
Serial.print("\t");
32+
Serial.print(value, DEC);
33+
Serial.println();
34+
35+
// advance to the next address of the EEPROM
36+
address = address + 1;
37+
38+
// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
39+
// on address 512, wrap around to address 0
40+
if (address == 512)
41+
address = 0;
42+
43+
delay(500);
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* EEPROM Write
3+
*
4+
* Stores values read from analog input 0 into the EEPROM.
5+
* These values will stay in the EEPROM when the board is
6+
* turned off and may be retrieved later by another sketch.
7+
*/
8+
9+
#include <EEPROM.h>
10+
11+
// the current address in the EEPROM (i.e. which byte
12+
// we're going to write to next)
13+
int addr = 0;
14+
15+
void setup()
16+
{
17+
EEPROM.begin(512);
18+
}
19+
20+
void loop()
21+
{
22+
// need to divide by 4 because analog inputs range from
23+
// 0 to 1023 and each byte of the EEPROM can only hold a
24+
// value from 0 to 255.
25+
int val = analogRead(0) / 4;
26+
27+
// write the value to the appropriate byte of the EEPROM.
28+
// these values will remain there when the board is
29+
// turned off.
30+
EEPROM.write(addr, val);
31+
32+
// advance to the next address. there are 512 bytes in
33+
// the EEPROM, so go back to 0 when we hit 512.
34+
// save all changes to the flash.
35+
addr = addr + 1;
36+
if (addr == 512)
37+
{
38+
addr = 0;
39+
EEPROM.commit();
40+
}
41+
42+
delay(100);
43+
}

0 commit comments

Comments
 (0)