Go Down

Topic: Problem interfacing with an 25LC040 EEPROM (SPI) (Read 717 times) previous topic - next topic

decisi

I'm trying to store and read data from a 25LC040 EEPROM (http://www.c-stamp.com/Documents/Documentation/CS450000d1.pdf).
When I'm trying to read the addresses I'm just getting zeros as output.

I followed along with this tutorial: http://arduino.cc/en/Tutorial/SPIEEPROM.

This is my code:

Code: [Select]
#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

byte eeprom_output_data;
byte eeprom_input_data=0;
byte clr;
int address=0;
//data buffer
char buffer [16];
int a = 2;

void fill_buffer()
{
  for(int i = 0; i < 16; i++)
  {
    buffer[i] = i;
  }
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK, OUTPUT);
  pinMode(SLAVESELECT, OUTPUT);
 
  digitalWrite(SLAVESELECT, HIGH);

  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
  clr=SPSR;
  clr=SPDR;
  delay(10);
 
  fill_buffer();

  digitalWrite(SLAVESELECT, LOW);
  spi_transfer(WREN);
  digitalWrite(SLAVESELECT, HIGH);
  delay(10);
 
  digitalWrite(SLAVESELECT, LOW);
  spi_transfer(WRITE);
  spi_transfer(address);
  for(int i = 0; i < 16; i++)
  {
    spi_transfer(buffer[i]);
  }
  digitalWrite(SLAVESELECT, HIGH);
  delay(3000);
  Serial.write("done writing \n");
  delay(1000);
}

void loop()
{
  if(address < 16)
  {
    Serial.print("reading data \n");
    eeprom_output_data = read_eeprom(address);
    Serial.print(eeprom_output_data,DEC);
    Serial.write('\n');
    address++;
    delay(500);
  }
}

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

byte read_eeprom(int EEPROM_address)
{
  int data;
  digitalWrite(SLAVESELECT, LOW);
  spi_transfer(READ);
  spi_transfer((char)(EEPROM_address));
  data = spi_transfer(0xFF);
  digitalWrite(SLAVESELECT,HIGH);
  return data;
}


Any help would be very much appreciated.

decisi

Nevermind.

I failed to wire the EEPROM correctly... stupid me:).

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy