Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - I²C EEPROM: what changed

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

Title: AVR Embedded Tutorial - I²C EEPROM

OldNewLine
1+# I²C EEPROM (24LC256)
2+
3+This code is for an Atmega328 / Arduino with a 16MHz clock and an [24LC256](https://www.microchip.com/wwwproducts/en/24LC256) I2C compatible 2-wire Serial EEPROM. Other 24LCxx EEPROMs can also be controlled.
4+
5+The following functions are described in other tutorials:
6+
7+- [[AVR Embedded Tutorial - Delays|Delays]]
8+- [[AVR Embedded Tutorial - UART|UART - serial interface]]
9+- [[AVR Embedded Tutorial - I²C, TWI|Activate I²C bus]] or [[AVR Embedded Tutorial - Software I2C, TWI]]
10+
11+This tutorial shows you how to control an external EEPROM (24LC256) which is connected to the I²C bus.
12+
13+**PullUp resistors are necessary**, the EEPROM does not contain any itself.
14+
15+## Functions for EEPROM (24LC256) control
16+
17+### Constants
18+
19+``` pascal
20+ const
21+ TWI_Write = 0;
22+ TWI_Read = 1;
23+```
24+
25+### Functions
26+
27+Note: For a [[Shortstring|ShortString]], the length byte is stored at position \[0\] in the string.
28+
29+Call parameters:
30+
31+- **addr** device address on the I²C bus
32+- **pos** storage location in the EEPROM
33+- **value** byte to be written
34+
35+#### Functions for writing and reading a byte
36+
37+``` pascal
38+ // Write a character
39+ procedure I2C_EEPROM_Write(addr , pos : Int16; value : Byte);
40+ begin
41+ TWIStart((addr shl 1) or TWI_Write);
42+ TWIWrite(pos shr 8);
43+ TWIWrite(pos and $FF);
44+ TWIWrite(value);
45+ TWIStop;
46+ delay_ms(10);
47+ end;
48+
49+ // Read a character
50+ function I2C_EEPROM_Read(addr, pos : Int16) : byte;
51+ begin
52+ TWIStart((addr shl 1) or TWI_Write);
53+ TWIWrite(pos shr 8);
54+ TWIWrite(pos and $FF);
55+ TWIStop;
56+
57+ TWIStart((addr shl 1) or TWI_Read);
58+ Result := TWIReadNACK;
59+ TWIStop;
60+ end;
61+```
62+
63+#### Functions for writing and reading a string
64+
65+A [[Shortstring|ShortString]], or other buffer, is written / read character by character.
66+
67+``` pascal
68+ // Write a string, including length byte
69+ procedure I2C_EEPROM_WriteString(addr, pos : Int16; s : ShortString);
70+ var
71+ i : byte;
72+ begin
73+ for i := 0 to Length(s) do
74+ begin
75+ I2C_EEPROM_Write(addr, pos + i, byte(s[i]));
76+ end;
77+ end;
78+
79+ // Read a string, including length byte
80+ function I2C_EEPROM_ReadString(addr, pos : Int16) : ShortString;
81+ var
82+ i, l : byte;
83+ begin
84+ l := I2C_EEPROM_Read(addr, pos); // read length of string stored at [0]
85+ SetLength(Result, l);
86+ for i := 1 to l do
87+ begin
88+ Result[i] := Char(I2C_EEPROM_Read(addr, pos + i));
89+ end;
90+ end;
91+```
92+
93+## Example program
94+
95+### Constants and variables
96+
97+``` pascal
98+ const
99+ I2Caddr = $54; // Address of the 24LC256 on the I²C bus
100+ s1 = 'Hello World!';
101+ s2 = 'AVR with Lazarus is so beautiful';
102+
103+ var
104+ s : ShortString;
105+```
106+
107+### Main program
108+
109+Since a [[Shortstring|ShortString]] is 256 bytes long, the second string at address \$100 (256) is saved in the EEPROM.
110+
111+``` pascal
112+ begin
113+ UARTInit; // initialize UART
114+ TWIInit; // initialize I²C
115+ asm Sei end; // enable interrupts
116+
117+ // save the two strings
118+ I2C_EEPROM_WriteString(I2Caddr, 0, s1); // first string
119+ I2C_EEPROM_WriteString(I2Caddr, $100, s2); // second string
120+
121+ // eeload the strings for verification
122+ repeat
123+ s := I2C_EEPROM_ReadString(I2Caddr, 0);
124+ UARTSendString('s1:');
125+ UARTSendString(s);
126+ UARTSendString(#13#10);
127+
128+ s := I2C_EEPROM_ReadString(I2Caddr, $100);
129+ UARTSendString('s2:');
130+ UARTSendString(s);
131+ UARTSendString(#13#10);
132+ until 1 = 2;
133+ end.
134+```
135+
136+If you do not believe that the string is in the EEPROM, you can exclude the two lines with WriteString (... and test again.
137+
138+# See also
139+
140+- Overview page - [[AVR Embedded Tutorial|AVR Embedded Tutorials]]
141+
142+---
143+*From the Free Pascal wiki, page "AVR Embedded Tutorial - I²C EEPROM", as it stood on 2020-03-08 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).*

Back to history