Interfacing to External EEPROM

5,850

Circuit Image

An external EEPROM is beneficial for various applications as it provides significantly more storage capacity than the internal memory of the 18F4520 microcontroller. Additionally, EEPROM retains its data even when power is disconnected. This project involves interfacing with a Microchip EEPROM using a random read/write mode. The term "random" indicates that memory locations can be accessed in a non-sequential manner. Although sequential access is possible, a different protocol structure enhances throughput for sequential reads. This project focuses solely on the random write protocol. Pins A0 and A1 serve as hardware addressing bits, corresponding to the I2C addresses 0xA0 for writing and 0xA1 for reading. The first four bits are fixed at 1010 (A), while the subsequent four bits indicate the memory block (0 or 1). A0 and A1 are set to low in the circuit diagram. The write protect pin (WP) has two states; when set to high, normal read/write operations are allowed, and when low, only read operations can be performed.

The provided code demonstrates how to interface with an external Microchip EEPROM using I2C for random read and write operations. The code is structured to initialize the I2C communication, write data to a specified address, and read data back from that address.

The I2C protocol is employed in this project, with specific configurations for the hardware I2C controller. The EEPROM write command is represented by the address 0xA0, where the last bit indicates a write operation (0). Conversely, the read command is represented by the address 0xA1, with the last bit set to 1 for read operations.

The code includes functions for writing and reading from the EEPROM. The `rand_write` function initiates the I2C bus, sends the write command along with the target address (split into high and low bytes), and then sends the data to be written. A delay of 5 milliseconds is implemented to allow the EEPROM to complete the write operation, as specified in the datasheet.

The `rand_read` function similarly starts the I2C bus and sends the write command to set the address. It then restarts the I2C bus to switch to read mode, retrieves the data from the specified address, and concludes the operation by releasing the bus.

The main function initializes a reference output, performs a random write operation, and subsequently reads the data back, outputting it for verification. The design emphasizes the capability of random access within the EEPROM, showcasing its utility in applications requiring flexible data storage solutions.An external EEPROM can be useful for several different reasons. External EEPROMs allow much more data to be stored than is available on the 18F4520. In addition, EEPROM memory saves state when power is removed. In this project, we interfaced to a Microchip EEPROM in random read/write mode. "Random" write mode specifies that the memory locations ac cessed do not come in any sequential form. Although one can use this mode to access data sequentially in the EEPROM, there is a different protocol structure for sequential reads that increases throughput. In this project, we only developed the random write protocol. PIN1 and PIN2, A0 and A1 are hardware addressing bits, they correspond to 0xA0 (writing data) and 0xA1 (reading data).

The first 4 bits are internally set to 1010 (A) and the second four correspond to the block of memory (0 or 1), A0 and A1 (hardware addressing we set to low in this circuit diagram) and read/(NOT write). PIN7, WP is the write protect line which has two states. When connected to logic high, normal read/write operation can be performed. When this pin is set low, only read operations are permitted. /* eeprom_rand_access. c Scott McLeod 03-05-2008 This program shows how to interface to an I2C extrenal Microchip EEPROM. This code is written for random reads and writes to the EEPROM */ #include <18f4520. h> #fuses HS, NOLVP, NOWDT, NOPROTECT #use delay(clock=20000000) #use i2c(MASTER, FAST, SCL=PIN_C3, SDA=PIN_C4, FORCE_HW) // use hardware i2c controller int8 EEPROM_WR = 0xA0; // I2C Address.

1010 0000. last bit = 0 => write command // First Nibble is fixed as 1010 (internal address); // Second Nibble is ABCD, // A = Block Set // B, C = Hardware Addresses (Configured when you Wire the CHIP) // D = (0 = write, 1 = read) int8 EEPROM_RD = 0xA1; // Same as EEPROM_WR except last bit = 1 for READ command int read = 0; int data = 128; int16 loc; int temp = 0; void rand_write(int16 address, int data) { i2c_start(); // Claim I2C BUS i2c_write(EEPROM_WR); // Tell all I2C devices you are talking to EEPROM in WRITE MODE i2c_write(address>>8); // Address High Byte (0x00 - 0x) i2c_write(address); // Address Low Byte (0x00 - 0xAA) i2c_write(data); // Data to write i2c_stop(); // Release BUS delay_ms(5); // Let EEPROM Write Data (5ms from data sheet) } int rand_read(int16 address) { i2c_start(); // Claim I2C BUS i2c_write(EEPROM_WR); // Tell all I2C devices you are talking to EEPROM in WRITE MODE // (you are first writing to the EEPROM to set the address. Later you will read) i2c_write(address>>8); // Address High Byte (0x00 - 0x i2c_write(address); // Address Low Byte (0x00 - 0xAA) i2c_start(); // RESTART I2C BUS (necissary for microchip protocol) i2c_write(EEPROM_RD); // Tell all I2C you are talking to EEPROM in READ MODE.

read = i2c_read(); // Read in the data i2c_read(0); // Read last byte with no ACK i2c_stop(); // release the bus return(read); } void main() { output_d(1); // Output a 1 for reference to start delay_ms(1000); // *Not necissary pause, just for debugging loc = 0x00; // Random Location in memory temp = 15; // Byte to write rand_write(loc, temp); // Random Write: rand_write(address, data) output_d(255); delay_ms(1000); // *Not necissary pause, just for visual purposes temp = 0; // Reset temp temp = rand_read(loc); // Random Read: rand_read(address); output_d(temp); // Output temp while(true) { } } 🔗 External reference