Hello,
I'm working on a project now and I want to transmit data from EEPROM to SD card. Both EEPROM and SD card use SPI. And I use SPIEEP library to write and read data to EEPROM. However, I have some problems switching SPI between two devices. The code is below:
#include <SPIEEP.h>
#include <SPI.h>
#include <SD.h>
//SdFat sd;
File myFile;
SPIEEP eep(16, 128, 65536); // Microchip 25LC512
#define EEPROM_CSPIN 6 // Chip Select attached to pin 10
#define SDcard_CSPIN 5
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//digitalWrite(EEPROM_CSPIN,LOW);
byte buf[500];
int i;
byte buf1[500];
for (i=0; i < 500; i++) {
buf[i] = 'A' + (i%26);
}
pinMode (SDcard_CSPIN,OUTPUT);
digitalWrite(SDcard_CSPIN,HIGH);
eep.begin_spi(EEPROM_CSPIN);// start eeprom. sets EEPROM_CSPIN as output and high
if (!eep.writen(0x2F00, buf, 500)) {
Serial.println("Error writing 500 bytes to address=0x2F00");
}
delay(500);
if (!eep.readn(0x2F00, buf1, 500)) {
Serial.println("Error reading 500 bytes from address=0x2F00");
}
delay(500);
Serial.println("debug1");
Serial.println((char *)buf1);
//Terminate eep. To restart run eep.begin_spi(EEPROM_CSPIN) again
pinMode (EEPROM_CSPIN,OUTPUT);
digitalWrite(EEPROM_CSPIN,HIGH); // it looks to me as if the eeprom needs to be ended before having anything to do with the SD card
delay(100);
if (!SD.begin(SDcard_CSPIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("debug2");
Serial.println("initialization done.");
}
void loop() {
while(1);
}
The only thing I got in serial monitor is "debug1". I wrote and read to arbitrary address of EEPROM and it does work. I'm confused about what's the problem. Also, I tried SPI.end(); and eep.end(); after EEPROM data reading, but still not work.
Thank you for any advice.
Regards,
Cody