25LC256 issue

Hi, I'm trying to use a SPI EEPROM 25lc256 with Arduino, I'm using the SPI library but i don't know how to use it, so i wrote the code but it doesn't work :cold_sweat: it returns me random data...

Here is my code... i think it's a mess :S

#include <SPI.h>
int variable = 4;
int resultado, registro=0;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  SPI.setBitOrder(MSBFIRST);
  
  SPI.transfer(6);
  SPI.transfer(0x10);
  delay(5);
  
  for(int j=0; j<9; j++)
  {
      SPI.transfer(6);
      SPI.transfer(0x20);
      SPI.transfer(registro);
      SPI.transfer(variable);
      delay(5);
      SPI.transfer(6);
      SPI.transfer(0x30);
      SPI.transfer(registro);
      resultado = SPI.transfer(0x00);
      SPI.transfer(4);
      Serial.print("direccion :");
      Serial.println(registro);
      Serial.println(resultado);
      registro++;
      variable++;
  }

}

void loop() {}

thanks for your help

You have to pull the Chip Select / Slave Select line LOW to talk to an SPI device and set it HIGH again when the operation is complete. I don't see that happening in your code.

You seem to be using the command "6" often. That's the Write Enable command but is not the Write command.

You should probably study the datasheet more:

yes, actually I saw an example with PIC, and they uses write enable several times, I viewed the datasheet before you gave me the link... and I make it work with pic and picaxe, but I don't know how to use it in Arduino, and the SPI library doesn't have enough explaination. :roll_eyes:

How is the chip wired to the Arduino? What pins are connected where?

digital pin 10 Arduino - pin 1 25lc256
digital pin 11 - pin 5
digital pin 12 - pin 2
digital pin 13 - pin 6

pin 7, 3 and 8 of 25lc256 to 5v
pin 4 to gnd

That pinout looks correct and means that Pin 10 is being used as the Slave Select (CS) pin. You need to add pinMode(10, OUTPUT); and digitalWrite(10, HIGH); to setup() and then before each command you add digitalWrite(10, LOW); to enable the SPI device. Add digitalWrite(10, HIGH); after al the data for the command has been transferred.

That will get you closer and will make it clear what commands you are sending. It will probably still not work until you issue the right commands. It would probably help if you could put into comments what you expect each part of the code to do.

Oh! I see ... I think .. SPI library that it automatically :roll_eyes:

I'll try that...

I have to change some code to make it work, but now is working :slight_smile:

Thanks! :stuck_out_tongue_closed_eyes:

Now I'll try to make a writing cycle, and a reading cycle.

#include <SPI.h>
int variable = 2; //data that you want to record
int resultado, registro=0; // variable to record from EEPROM, position of the registry

void setup() {
  pinMode(10,OUTPUT);
  digitalWrite(10, HIGH);
  Serial.begin(9600);
  SPI.begin();  
  SPI.setDataMode(SPI_MODE3);  //setup SPI protocol
  SPI.setClockDivider(SPI_CLOCK_DIV2); // 10 Mhz max for 25LC256
  SPI.setBitOrder(MSBFIRST);
  
  digitalWrite(10, LOW);
  SPI.transfer(6);  //write enable 6
  digitalWrite(10, HIGH);
  
  digitalWrite(10, LOW);
  SPI.transfer(1); //remove block protection 1,0 
  SPI.transfer(0);
  digitalWrite(10, HIGH);
  delay(5);
  
  for(int j=0; j<9; j++)
  {
      digitalWrite(10, LOW);
      SPI.transfer(6); //write enable 6
      digitalWrite(10, HIGH);
      
      digitalWrite(10, LOW);
      SPI.transfer(2); // write 2,0
      SPI.transfer(0);
      SPI.transfer(registro); // registry
      SPI.transfer(variable); // data
      digitalWrite(10, HIGH);
      delay(5);
      
      digitalWrite(10, LOW);
      SPI.transfer(6); // write enable 6
      digitalWrite(10, HIGH);
      
      digitalWrite(10, LOW);
      SPI.transfer(3); // read 3,0
      SPI.transfer(0);
      SPI.transfer(registro); // registry
      resultado = SPI.transfer(0x00);
      digitalWrite(10, HIGH);
       
      Serial.print("direccion");
      Serial.print(registro);
      Serial.print(" : ");
      Serial.println(resultado);
      registro++;
      variable++;
  }
  digitalWrite(10, LOW);
  SPI.transfer(4); // write disabled 4
  digitalWrite(10, HIGH);
}

void loop() {}

This is the writting cycle:

Note: I added a led to digital pin 2 to show when the cycle is finished.

#include <SPI.h>
int variable = 56; //data to record
int resultado, registro=0;

void setup() {
  pinMode(10,OUTPUT);
  pinMode(2,OUTPUT);
  digitalWrite(10, HIGH);
  digitalWrite(2, HIGH);
  SPI.begin();  
  SPI.setDataMode(SPI_MODE3);  // set SPI protocol
  SPI.setClockDivider(SPI_CLOCK_DIV2); // 10 Mhz max for 25LC256
  SPI.setBitOrder(MSBFIRST);
  
  digitalWrite(10, LOW);
  SPI.transfer(6);  // write enable 6
  digitalWrite(10, HIGH);
  
  digitalWrite(10, LOW);
  SPI.transfer(1); // remove block protection 1,0 
  SPI.transfer(0);
  digitalWrite(10, HIGH);
  delay(5);
  
  for(int j=0; j<9; j++)
  {
      digitalWrite(10, LOW);
      SPI.transfer(6); // write enabled 6
      digitalWrite(10, HIGH);
      
      digitalWrite(10, LOW);
      SPI.transfer(2); // write 2,0
      SPI.transfer(0);
      SPI.transfer(registro); // registry
      SPI.transfer(variable); // data to record
      digitalWrite(10, HIGH);
      delay(5);
      
      digitalWrite(10, LOW);
      SPI.transfer(6); // write enabled 6
      digitalWrite(10, HIGH);

      registro++;  // +1
      variable++;  // +1
  }
  digitalWrite(10, LOW);
  SPI.transfer(4); // write disabled 4
  digitalWrite(10, HIGH);
}

void loop() {
digitalWrite(2, LOW);
}

This is the reading cycle:

Note: I added a led to digital pin 2 to show when the cycle is finished.

#include <SPI.h>
int resultado, registro=0;

void setup() {
  pinMode(10,OUTPUT);
  pinMode(2,OUTPUT);
  digitalWrite(10, HIGH);
  digitalWrite(2, HIGH);
  Serial.begin(9600);
  SPI.begin();  
  SPI.setDataMode(SPI_MODE3);  //setup SPI protocol
  SPI.setClockDivider(SPI_CLOCK_DIV2); // 10 Mhz max for 25LC256
  SPI.setBitOrder(MSBFIRST);
  
  for(int j=0; j<9; j++)
  {
      digitalWrite(10, LOW);
      SPI.transfer(3); // read 3,0
      SPI.transfer(0);
      SPI.transfer(registro); // registry
      resultado = SPI.transfer(0x00);
      digitalWrite(10, HIGH);
       
      Serial.print("direccion"); //print on screen
      Serial.print(registro);
      Serial.print(" : ");
      Serial.println(resultado);
      registro++;
  }
}

void loop() {
digitalWrite(2, LOW);
}