I am trying to set up a micro SD card using a SPI network with an Arduino Mini. I'm currently just trying to get the basics up and running using the code provided by the Arduino playground, found here: http://www.arduino.cc/playground/Code/SDCARD
The only change in my code is the pin location declarations, which for are as follows:
int PIN_CS = 10; //Chip select
int PIN_MOSI = 11; //Master out slave in
int PIN_MISO = 12; //Master in slave out
int PIN_CLOCK = 13; //Clock
I am presently having some problems with the void setup() function. When I try to run the sdc_totalNbrBlocks() function (which gets the number of blocks on the SD memory care), I end up in an infinite loop.
After doing some digging, I found that the code is stuck in the sdc_readRegister() function which reads the SD card register content and stores it in a buffer. The line it is stuck at is:
while (spi_cmd(0xFF) != 0xFE)
Looking more into the code, I found that I am also having problem in the spi_cmd function, which is used to send a SPI command. Basically, waiting for the end of transmission fails and an error value is returned. So I think this part of the code might be the root cause of the issue. This is the while loop where it fails:
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
if (i >= 0xFF)
spi_err = 1;
Serial.print("Error Value: ");
It seems like a lot of people have used and tested this code from Arduino program, so I'm not quite sure how to fix the issue. Could this be a hardware setup or a pin initialization error?
Help is appreciated. Please let me know if you need more information about the code. Thanks,