Skip to content

Commit 2657747

Browse files
committed
Updated EEPROM examples.
Removed hard coded lengths, which were incorrect for standard Arduino's now.
1 parent fd4323f commit 2657747

File tree

4 files changed

+94
-31
lines changed

4 files changed

+94
-31
lines changed

libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino

+18-6
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22
* EEPROM Clear
33
*
44
* Sets all of the bytes of the EEPROM to 0.
5+
* Please see eeprom_iteration for a more in depth
6+
* look at how to traverse the EEPROM.
7+
*
58
* This example code is in the public domain.
6-
79
*/
810

911
#include <EEPROM.h>
1012

1113
void setup()
1214
{
13-
// write a 0 to all 512 bytes of the EEPROM
14-
for (int i = 0; i < 512; i++)
15+
16+
/***
17+
Iterate through each byte of the EEPROM storage.
18+
19+
Larger AVR processors have larger EEPROM sizes, E.g:
20+
- Arduno Duemilanove: 512b EEPROM storage.
21+
- Arduino Uno: 1kb EEPROM storage.
22+
- Arduino Mega: 4kb EEPROM storage.
23+
24+
Rather than hard-coding the length, you should use the pre-provided length function.
25+
This will make your code portable to all AVR processors.
26+
***/
27+
28+
for ( int i = 0 ; i < EEPROM.length() ; i++ )
1529
EEPROM.write(i, 0);
1630

1731
// turn the LED on when we're done
1832
digitalWrite(13, HIGH);
1933
}
2034

21-
void loop()
22-
{
23-
}
35+
void loop(){ /** Empty loop. **/ }

libraries/EEPROM/examples/eeprom_read/eeprom_read.ino

+21-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,27 @@ void loop()
3131
Serial.print(value, DEC);
3232
Serial.println();
3333

34-
// advance to the next address of the EEPROM
35-
address = address + 1;
36-
37-
// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
38-
// on address 512, wrap around to address 0
39-
if (address == 512)
40-
address = 0;
34+
/***
35+
Advance to the next address, when at the end restart at the beginning.
36+
37+
Larger AVR processors have larger EEPROM sizes, E.g:
38+
- Arduno Duemilanove: 512b EEPROM storage.
39+
- Arduino Uno: 1kb EEPROM storage.
40+
- Arduino Mega: 4kb EEPROM storage.
41+
42+
Rather than hard-coding the length, you should use the pre-provided length function.
43+
This will make your code portable to all AVR processors.
44+
***/
45+
addr = addr + 1;
46+
if(addr == EEPROM.length())
47+
addr = 0;
48+
49+
/***
50+
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
51+
EEPROM address is also doable by a bitwise and of the length - 1.
52+
53+
++addr &= EEPROM.length() - 1;
54+
***/
4155

4256
delay(500);
4357
}

libraries/EEPROM/examples/eeprom_update/eeprom_update.ino

+20-3
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,32 @@ void loop()
3838
The function EEPROM.update(addr, val) is equivalent to the following:
3939
4040
if( EEPROM.read(addr) != val ){
41-
EEPROM.write(addr, val);
41+
EEPROM.write(addr, val);
4242
}
4343
***/
4444

4545

46-
/** advance to the next address. there are 512 bytes in the EEPROM, so go back to 0 when we hit 512. **/
46+
/***
47+
Advance to the next address, when at the end restart at the beginning.
48+
49+
Larger AVR processors have larger EEPROM sizes, E.g:
50+
- Arduno Duemilanove: 512b EEPROM storage.
51+
- Arduino Uno: 1kb EEPROM storage.
52+
- Arduino Mega: 4kb EEPROM storage.
53+
54+
Rather than hard-coding the length, you should use the pre-provided length function.
55+
This will make your code portable to all AVR processors.
56+
***/
4757
addr = addr + 1;
48-
if (addr == 512)
58+
if(addr == EEPROM.length())
4959
addr = 0;
60+
61+
/***
62+
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
63+
EEPROM address is also doable by a bitwise and of the length - 1.
64+
65+
++addr &= EEPROM.length() - 1;
66+
***/
5067

5168
delay(100);
5269
}

libraries/EEPROM/examples/eeprom_write/eeprom_write.ino

+35-15
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,51 @@
88

99
#include <EEPROM.h>
1010

11-
// the current address in the EEPROM (i.e. which byte
12-
// we're going to write to next)
13-
int addr = 0;
11+
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
12+
int addr = 0;
1413

15-
void setup()
16-
{
17-
}
14+
void setup(){ /** Empty setup. **/}
1815

1916
void loop()
2017
{
21-
// need to divide by 4 because analog inputs range from
22-
// 0 to 1023 and each byte of the EEPROM can only hold a
23-
// value from 0 to 255.
18+
/***
19+
Need to divide by 4 because analog inputs range from
20+
0 to 1023 and each byte of the EEPROM can only hold a
21+
value from 0 to 255.
22+
***/
23+
2424
int val = analogRead(0) / 4;
2525

26-
// write the value to the appropriate byte of the EEPROM.
27-
// these values will remain there when the board is
28-
// turned off.
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+
***/
31+
2932
EEPROM.write(addr, val);
3033

31-
// advance to the next address. there are 512 bytes in
32-
// the EEPROM, so go back to 0 when we hit 512.
34+
/***
35+
Advance to the next address, when at the end restart at the beginning.
36+
37+
Larger AVR processors have larger EEPROM sizes, E.g:
38+
- Arduno Duemilanove: 512b EEPROM storage.
39+
- Arduino Uno: 1kb EEPROM storage.
40+
- Arduino Mega: 4kb EEPROM storage.
41+
42+
Rather than hard-coding the length, you should use the pre-provided length function.
43+
This will make your code portable to all AVR processors.
44+
***/
3345
addr = addr + 1;
34-
if (addr == 512)
46+
if(addr == EEPROM.length())
3547
addr = 0;
48+
49+
/***
50+
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
51+
EEPROM address is also doable by a bitwise and of the length - 1.
52+
53+
++addr &= EEPROM.length() - 1;
54+
***/
55+
3656

3757
delay(100);
3858
}

0 commit comments

Comments
 (0)