This example shows how to store and retrieve data from an EEPROM on a SPI device. Using EEPROM you can read, erase, and rewrite individual bits of data from the sensor’s memory. This example uses SPI EEPROM 25AA080C device from Microchip Technology with the Arduino® Mega 2560. Before using this example, wire an EEPROM to the SPI pins on your Arduino as described:
|SPI EEPROM Pins||Arduino Pins|
|/CS||Digital pin 10|
|/WP||50k or 100k ohm resistor tied to Vcc|
It is also recommended that you place a 1uF decoupling capacitor between VCC and Gnd.
Create a SPI connection on your Arduino.
Create an Arduino object and include the SPI library.
a = arduino('COM10', 'Mega2560', 'Libraries', 'I2C, SPI, Servo');
Write to, and read back data from an EEPROM, which is a micro controller designed to interface directly with the Serial Peripheral Interface (SPI). For more information about the read and write sequence in an EEPROM, refer to the functional description in the EEPROM Data Sheet.
Create a chip select pin connection.
Cspin = 'D46';
Create a SPI device object.
eeprom = device(a, 'SPIChipSelectPin', Cspin, 'bitorder', 'msbfirst', 'SPIMode', 0)
eeprom = device with properties: Interface: SPI SPIChipSelectPin: D10 SCLPin: D21 MISOPin: D50 MOSIPin: D51 Show all properties, functions
Specify the read, write and write enable commands referring to the EEPROM instruction set .
|Instruction Name||Instruction Format||Description|
Read data from memory array beginning at selected address
Write data to memory array beginning at selected address
Reset the write enable latch (disable write operations)
Set the write enable latch (enable write operations)
Read STATUS Register
Write STATUS Register
readCmd = 0b00000011; writeCmd = 0b00000010; writeEnable = 0b00000110;
Write enable the device.
Set the device address. The address is a 16–bit value, with 8 upper and 8 lower bits. The highest value on the address can be 0x03FF.
address = [0 0];
Write 'Hello' at the 0th address of EEPROM.
dataToWrite = [writeCmd,address double('Hello')]; writeRead(eeprom, dataToWrite)
ans = 0 0 0 0 0 0 0 0
Read back the data from the EEPROM.
dataToWrite2 = [readCmd address zeros(1,5)]; returnedData = writeRead(eeprom, dataToWrite2)
returnedData = 0 0 0 72 101 108 108 111
Display the result.