Hey all.
I've been working hard the past 2 weeks trying to get my Arduino to successfully and consistently write/read from memory using SPI but I am having problems. I used the tutorial code for SPIEEPROM to get me started and have made the proper modifications for the memory chip I am using. I'm using the Arduino Uno and the Atmel AT25DF641 chip (data sheet here: http://www.atmel.com/dyn/resources/prod_documents/doc3680.pdf).
For the connections I used pins 13,12,11,10 from the Arduino to the chip (on a breadboard), and also tied the WP to Vcc.
Here's the code that I have so far:
int DATAOUT = 11;//MOSI
int DATAIN = 12;//MISO
int SPICLOCK = 13;//sck
int SLAVESELECT = 10;//ss
byte eeprom_output_data;
byte clr;
int address=0;
char buffer [128];
void setup()
{
Serial.begin(9600);
initPins(); //initialize data pins
digitalWrite(SLAVESELECT,HIGH); //disable device
DDRB = (1<<DDB5)|(1<<DDB3)|(1<<DDB2); // set DDRB register
delay(1000); // delay
// CPOL = 0 base value of clock is zero (SPI Mode0)
// CPHA = 0 data captured on rising edge(SPI Mode0)
// Arduino Master Mode
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
clr=SPSR;
clr=SPDR;
delay(1000); // delay
fill_buffer(); // fill a char array in code
digitalWrite(SLAVESELECT,HIGH);
digitalWrite(SLAVESELECT,LOW); // start operation HIGH->LOW
spi_transfer((char)6); //write enable
digitalWrite(SLAVESELECT,HIGH); //end operation LOW->HIGH
delay(1000); // wait 1 second
digitalWrite(SLAVESELECT,LOW); //start operation HIGH->LOW
spi_transfer((char)2); // write command
spi_transfer((char)0); //first address byte (MSB)
spi_transfer((char)0); //second address byte (next byte)
spi_transfer((char)0); //third address byte (LSB)
// Loop sending each char in char array to memory chip
for (int I=0;I<128;I++)
{
Serial.print("Sending "); Serial.print(buffer[I], DEC);
Serial.println(" to memory.");
spi_transfer(buffer[I]); //write data byte
delay(3);
}
digitalWrite(SLAVESELECT,HIGH); //end operation LOW->HIGH
delay(3000); // wait 3 seconds
Serial.print('h',BYTE);
Serial.print('i',BYTE);
Serial.print('\n',BYTE);//debug
}
void loop()
{
eeprom_output_data = read_eeprom(address);
Serial.print("Data: ");Serial.print(eeprom_output_data,DEC);Serial.print(" from Address: ");Serial.println(address,DEC);
address++;
if(address == 128)
address=0;
delay(500);
}
void fill_buffer()
{
for (int I=0;I<128;I++)
{
buffer[I]=I;
}
}
char spi_transfer(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((char)11); //transmit read opcode
spi_transfer((char)(EEPROM_address>>8)); // first address byte
spi_transfer((char)(EEPROM_address>>8)); // second address byte
spi_transfer((char)(EEPROM_address)); // third address byte
spi_transfer((char)(B11111111)); // dummy byte
data = spi_transfer((char)(B11111111)); // generate extra clock signals for data
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
delay(10);
return data;
}
void initPins()
{
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
}
My output is:
Data: 0 from Address: 0
Data: 0 from Address: 1
.
.
I get 0's for each address despite having the loop that writes to memory. Does anything in the code stick out as wrong? Any help is appreciated!!