Skip to content

Commit 5da9792

Browse files
committed
Fixed EEPROM examples and added readme
1 parent 2657747 commit 5da9792

File tree

9 files changed

+160
-204
lines changed

9 files changed

+160
-204
lines changed

libraries/EEPROM/README.md

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
## **EEPROM Library V2.0** for Arduino
2+
3+
**Written by:** _Christopher Andrews_.
4+
5+
### **What is the EEPROM library.**
6+
7+
Th EEPROM library provides an easy to use interface to interact with the internal non-volatile storage found in AVR based Arduino boards. This library will work on many AVR devices like ATtiny and ATmega chips.
8+
9+
### **How to use it**
10+
The EEPROM library is included in your IDE download. To add its functionality to your sketch you'll need to reference the library header file. You do this by adding an include directive to the top of your sketch.
11+
12+
```Arduino
13+
#include <EEPROM.h>
14+
15+
void setup(){
16+
17+
}
18+
19+
void loop(){
20+
21+
}
22+
23+
```
24+
25+
The library provides a global variable named `EEPROM`, you use this variable to access the library functions. The methods provided in the EEPROM class are listed below.
26+
27+
You can view all the examples [here](examples/).
28+
29+
### **Library functions**
30+
31+
#### **`EEPROM.read( address )`** [[_example_]](examples/eeprom_read/eeprom_read.ino)
32+
33+
This function allows you to read a single byte of data from the eeprom.
34+
Its only parameter is an `int` which should be set to the address you wish to read.
35+
36+
The function returns an `unsigned char` containing the value read.
37+
38+
#### **`EEPROM.write( address, value )`** [[_example_]](examples/eeprom_write/eeprom_write.ino)
39+
40+
The `write()` method allows you to write a single byte of data to the EEPROM.
41+
Two parameters are needed. The first is an `int` containing the address that is to be written, and the second is a the data to be written (`unsigned char`).
42+
43+
This function does not return any value.
44+
45+
#### **`EEPROM.update( address, value )`** [[_example_]](examples/eeprom_update/eeprom_update.ino)
46+
47+
This function is similar to `EEPROM.write()` however this method will only write data if the cell contents pointed to by `address` is different to `value`. This method can help prevent unnecessary wear on the EEPROM cells.
48+
49+
This function does not return any value.
50+
51+
#### **`EEPROM.get( address, object )`** [[_example_]](examples/eeprom_get/eeprom_get.ino)
52+
53+
This function will retrieve any object from the EEPROM.
54+
Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to read.
55+
56+
This function returns a reference to the `object` passed in. It does not need to be used and is only returned for conveience.
57+
58+
#### **`EEPROM.put( address, object )`** [[_example_]](examples/eeprom_put/eeprom_put.ino)
59+
60+
This function will write any object to the EEPROM.
61+
Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to write.
62+
63+
This function uses the _update_ method to write its data, and therefore only rewrites changed cells.
64+
65+
This function returns a reference to the `object` passed in. It does not need to be used and is only returned for conveience.
66+
67+
#### **Subscript operator: `EEPROM[address]`** [[_example_]](examples/eeprom_crc/eeprom_crc.ino)
68+
69+
This operator allows using the identifier `EEPROM` like an array.
70+
EEPROM cells can be read _and_ **_written_** directly using this method.
71+
72+
This operator returns a reference to the EEPROM cell.
73+
74+
```c++
75+
unsigned char val;
76+
77+
//Read first EEPROM cell.
78+
val = EEPROM[ 0 ];
79+
80+
//Write first EEPROM cell.
81+
EEPROM[ 0 ] = val;
82+
83+
//Compare contents
84+
if( val == EEPROM[ 0 ] ){
85+
//Do something...
86+
}
87+
```
88+
89+
#### **`EEPROM.length()`**
90+
91+
This function returns an `unsigned int` containing the number of cells in the EEPROM.
92+
93+
---
94+
95+
### **Advanced features**
96+
97+
This library uses a component based approach to provide its functionality. This means you can also use these components to design a customized approach. Two background classes are available for use: `EERef` & `EEPtr`.
98+
99+
#### **`EERef` class**
100+
101+
This object references an EEPROM cell.
102+
Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
103+
This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
104+
105+
```C++
106+
EERef ref = EEPROM[ 10 ]; //Create a reference to 11th cell.
107+
108+
ref = 4; //write to EEPROM cell.
109+
110+
unsigned char val = ref; //Read referenced cell.
111+
```
112+
113+
#### **`EEPtr` class**
114+
115+
This object is a bidirectional pointer to EEPROM cells represented by `EERef` objects.
116+
Just like a normal pointer type, this type can be dereferenced and repositioned using
117+
increment/decrement operators.
118+
119+
```C++
120+
EEPtr ptr = 10; //Create a pointer to 11th cell.
121+
122+
*ptr = 4; //dereference and write to EEPROM cell.
123+
124+
unsigned char val = *ptr; //dereference and read.
125+
126+
ptr++; //Move to next EEPROM cell.
127+
```
128+
129+
#### **`EEPROM.begin()`**
130+
131+
This function returns an `EEPtr` pointing to the first cell in the EEPROM.
132+
This is useful for STL objects, custom iteration and C++11 style ranged for loops.
133+
134+
#### **`EEPROM.end()`**
135+
136+
This function returns an `EEPtr` pointing at the location after the last EEPROM cell.
137+
Used with `begin()` to provide custom iteration.
138+
139+
**Note:** The `EEPtr` returned is invalid as it is out of range. Infact the hardware causes wrapping of the address (overflow) and `EEPROM.end()` actually references the first EEPROM cell.

libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ unsigned long eeprom_crc( void ){
3838

3939
unsigned long crc = ~0L;
4040

41-
for( int index = 0 ; index < 32 ; ++index ){
41+
for( int index = 0 ; index < EEPROM.length() ; ++index ){
4242
crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4);
4343
crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4);
4444
crc = ~crc;

libraries/EEPROM/examples/eeprom_get/eeprom_get.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
void setup(){
2121

2222
float f = 0.00f; //Variable to store data read from EEPROM.
23-
int eeAddress = 0; //Location of the IP address inside the class.
23+
int eeAddress = 0; //EEPROM address to start reading from
2424

2525
Serial.begin( 9600 );
2626
Serial.print( "Read float from EEPROM: " );

libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino

-16
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,6 @@ void setup() {
5151
idx++;
5252
}while( idx < EEPROM.length() );
5353

54-
/***
55-
Iterate the EEPROM using a C++11 ranged for loop.
56-
57-
This version of the loop is best explained in the example 'eeprom_pointer'
58-
as this kind of iteration uses pointers rather than an index/integer.
59-
60-
!! Note: C++11 is not yet enabled by default in any IDE version.
61-
Unless you manually enable it, this sketch will not compile.
62-
You can comment the loop below to verify the non C++11 content.
63-
***/
64-
65-
for( auto cell : EEPROM ){
66-
67-
//Add one to each cell in the EEPROM
68-
cell += 1;
69-
}
7054

7155
} //End of setup function.
7256

libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino

-74
This file was deleted.

libraries/EEPROM/examples/eeprom_put/eeprom_put.ino

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
#include <EEPROM.h>
1818

19+
struct MyObject{
20+
float field1;
21+
byte field2;
22+
char name[10];
23+
};
24+
1925
void setup(){
2026

2127
Serial.begin(9600);
@@ -31,12 +37,6 @@ void setup(){
3137

3238
/** Put is designed for use with custom structures also. **/
3339

34-
struct MyObject{
35-
float field1;
36-
byte field2;
37-
char name[10];
38-
};
39-
4040
//Data to store.
4141
MyObject customVar = {
4242
3.14f,

libraries/EEPROM/examples/eeprom_read/eeprom_read.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ void loop()
4242
Rather than hard-coding the length, you should use the pre-provided length function.
4343
This will make your code portable to all AVR processors.
4444
***/
45-
addr = addr + 1;
46-
if(addr == EEPROM.length())
47-
addr = 0;
45+
address = address + 1;
46+
if(address == EEPROM.length())
47+
address = 0;
4848

4949
/***
5050
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
5151
EEPROM address is also doable by a bitwise and of the length - 1.
5252
53-
++addr &= EEPROM.length() - 1;
53+
++address &= EEPROM.length() - 1;
5454
***/
5555

5656
delay(500);

libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino

-93
This file was deleted.

0 commit comments

Comments
 (0)