Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - EEPROM: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: AVR Embedded Tutorial - EEPROM

OldNewLine
1+# Read and write EEPROM data
2+
3+This code in this tutorial is for an Atmega328/Arduino with 16MHz. For how to control the UART with **UARTInit** and **UARTSendString(...**, see the [[AVR Embedded Tutorial - UART|UART Tutorial]].
4+
5+## Accessing the EEPROM
6+
7+Every single byte in the EEPROM can be accessed via an address. This gives you the opportunity to read and write each character individually.
8+
9+### Writing data
10+
11+> **Note:** The two lines with **EECR** must be separate, otherwise nothing is written!
12+
13+``` pascal
14+procedure EEPROM_write(Address : int16; Data : byte);
15+ begin
16+ // Check if ready to write
17+ while (EECR and (1 shl EEPE)) <> 0 do
18+ begin
19+ ;
20+ end;
21+
22+ EEAR := Address;
23+ EEDR := Data; // Byte to write
24+ EECR := EECR or (1 shl EEMPE); // There must be 2 lines!
25+ EECR := EECR or (1 shl EEPE);
26+ end;
27+```
28+
29+### Reading data
30+
31+``` pascal
32+function EEPROM_read(Address : int16) : byte;
33+ begin
34+ // Check if ready to read
35+ while (EECR and (1 shl EEPE )) <> 0 do
36+ begin
37+ ;
38+ end;
39+
40+ EEAR := Address;
41+ EECR := EECR or (1 shl EERE);
42+ Result := EEDR; // Read byte
43+ end;
44+```
45+
46+## Example
47+
48+Sample code that shows how to read and write data.
49+
50+``` pascal
51+ const
52+ sc = 'Hello World!';
53+
54+ var
55+ i : integer;
56+ s : shortString;
57+
58+ begin
59+ UARTInit ;
60+
61+ // Write string, including length byte
62+ for i := 0 to Length (sc) do
63+ begin
64+ EEPROM_write(i, byte(sc[i]));
65+ end;
66+
67+ // Read string, including length byte
68+ SetLength(s, EEPROM_read(0)); // Read length byte
69+
70+ for i := 1 to Length(s) do
71+ begin
72+ s[i] := char(EEPROM_read(i)); // Read string data bytes
73+ end;
74+
75+ repeat
76+ UARTSendString(s);
77+ UARTSendString(#13#10);
78+ until 1 = 2;
79+ end.
80+```
81+
82+Once the first routine (above) has been executed, you can easily see with the second routine (below) that "Hello World!" was stored in the EEPROM, even if you have disconnected the ATmega from the power in the meantime.
83+
84+``` pascal
85+ var
86+ i : integer;
87+ s : shortString;
88+
89+ begin
90+ UARTInit;
91+
92+ // Read string, including length byte
93+ SetLength(s, EEPROM_read(0)); // Read length byte
94+
95+ for i := 1 to Length(s) do
96+ begin
97+ s[i] := char(EEPROM_read(i));
98+ end;
99+
100+ repeat
101+ UARTSendString(s);
102+ UARTSendString(#13#10);
103+ until 1 = 2;
104+ end.
105+```
106+
107+# See also
108+
109+- [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview
110+
111+---
112+*From the Free Pascal wiki, page "AVR Embedded Tutorial - EEPROM", as it stood on 2020-01-26 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
113+Category: AVR
114+Category: Arduino
115+Category: Embedded
116+Category: Tutorials

Back to history