How to connect EEPROM over SPI bus of microcontroller

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to connect EEPROM over SPI bus of microcontroller

Post by Shane » Fri Jun 24, 2011 8:45 pm

In this tutorial, we will cover the basics of Arduino’s SPI (Serial Peripheral Interface) capabilities, as well as the AT25HP512 chip, which is a serial EEPROM, used to store data. This chip runs at 10MHz at 5V and is organized into 512 pages of 128 bytes each, giving us 65,536 bytes of data storage.

Gobetwino is a Windows-based program that acts as a "generic proxy" for the Arduino. It acts on behalf of the Arduino and does actions the Arduino could not do on its own. In this tutorial, the Log to File command is used so you get a sense of using Gobetwino commands. This command will automatically log data to a .txt file you have created on your PC using a program such as notepad and optionally timestamp it, depending on your preferences.

Steps to set up breadboard:
1.) Connect one lead of a CdS Photocell to +5V power. Connect the other lead to a 10k ohm resistor as well as pin A0 of the Arduino. Connect the other lead of the resistor to ground.
2.) Connect pins 3, 7, and 8 of the EEPROM to +5V power. Connect pin 4 to ground.
3.) Connect pin 1 of the EEPROM to pin D10 of the Arduino. This sets up the Slave Select line.
4.) Connect pin 2 of the EEPROM to pin D12 of the Arduino. This sets up the MISO (Master In Slave Out) line.
5.) Connect pin 5 of the EEPROM to pin D11 of the Arduino. This sets up the MOSI (Master Out Slave In) line.
6.) Connect pin 6 of the EEPROM to pin D13 of the Arduino. This sets up the Serial Clock.
1.jpg
1.jpg (24 KiB) Viewed 3493 times

Code: Select all

 //Define the pins used in the SPI conection
 #define DATAOUT 11//MOSI
 #define DATAIN 12//MISO 
 #define SPICLOCK 13//sck
 #define SLAVESELECT 10//ss

//opcodes
 #define WREN 6
 #define WRDI 4
 #define RDSR 5
 #define WRSR 1
 #define READ 3
 #define WRITE 2

//initialize all variables used in program
 byte eeprom_output_data;
 byte eeprom_input_data=0;
 byte clr;
 int address = 0;
 int CdS_Pin = 0;
 char CdS_Val = 0;
 char serInString[25];
 int serInLen = 25;
 char buffer[5];
 int value;

//SPI transfer function to shift data from the Arduino to the EEPROM via the SPI connection
 char spi_transfer(volatile char data)
 {
 SPDR = data; // Start the transmission
 while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
 {
 };
 return SPDR; // return the received byte
 }

void setup()
 {
 Serial.begin(9600);

 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK,OUTPUT);
 pinMode(SLAVESELECT,OUTPUT);
 digitalWrite(SLAVESELECT,HIGH); //disable device

 // SPCR = 01010000
 //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
 //sample on leading edge of clk,system clock/4 rate (fastest)
 SPCR = (1<<SPE)|(1<<MSTR);
 clr=SPSR;
 clr=SPDR;
 delay(10);

 digitalWrite(SLAVESELECT, LOW);
 spi_transfer(WREN);
 digitalWrite(SLAVESELECT, HIGH);

 delay(10);
 digitalWrite(SLAVESELECT, LOW);
 spi_transfer(WRITE);
 address = 0;
 spi_transfer((char)(address>>8)); //send MSByte address first
 spi_transfer((char)(address)); //send LSByte address
 digitalWrite(SLAVESELECT, HIGH);
 delay(3000);
 }

byte read_eeprom(int EEPROM_CdS_Val)
 {
 //READ EEPROM
 int data;
 digitalWrite(SLAVESELECT,LOW);
 spi_transfer(READ); //transmit read opcode
 spi_transfer((char)(EEPROM_CdS_Val>>8)); //send MSByte address first
 spi_transfer((char)(EEPROM_CdS_Val)); //send LSByte address
 data = spi_transfer(0xFF); //get data byte
 digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
 return data;
 }

void loop()
 {
 CdS_Val = analogRead(CdS_Pin); //read value from CdS cell

 digitalWrite(SLAVESELECT, LOW); //enable device
 spi_transfer(WREN); //write enable
 digitalWrite(SLAVESELECT, HIGH); //disable device

 delay(10);
 digitalWrite(SLAVESELECT, LOW);
 spi_transfer(WRITE); //write instruction
 spi_transfer((char)(CdS_Val)); //send value of CdS cell from Arduino (master) to EEPROM (slave)

 digitalWrite(SLAVESELECT, HIGH); 
 delay(3000);

 eeprom_output_data = (char) read_eeprom(CdS_Val);
 Serial.println(eeprom_output_data,DEC);
 Serial.println('\n',BYTE);
 value = CdS_Val;
 Serial.print("#S|LOGDATA|[");
 Serial.print(itoa((value), buffer, 10));
 Serial.println("]#");
 readSerialString(serInString, 1000);
 address++;
 if(address == 128)
 address = 0;
 delay(300000); //read and store value every 5 minutes
 }

void readSerialString (char *strArray,long timeOut) 
 {
 long startTime=millis();
 int i;

 while (!Serial.available()) {
 if (millis()-startTime >= timeOut) {
 return;
 }
 }
 while (Serial.available() && i < serInLen) {
 strArray[i] = Serial.read();
 i++;
 }
 }
2.JPG
2.JPG (58.04 KiB) Viewed 3493 times
3.jpg
3.jpg (74.84 KiB) Viewed 3493 times
4.jpg
4.jpg (80.9 KiB) Viewed 3493 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”