alvinthen
Member level 2
Hi,
As the title suggests, I'm trying to interface a PIC18F2220 with a SD card via SPI mode. I've not successfully initialize the SD card. I've sent 80+ clock pulses with the chip disabled and CMD0 with chip enabled. I should wait for a R1 Response with value of 0x01 before continuing to CMD1. However, I'm stucked here for few days trying to figure out why the card does not response with R1 (0x01).
I've followed the schematic below exactly:
And snippets of my codes:
Functions of SPI
My program got timeout while waiting for R1 response, any idea what's wrong with the circuit of the code?
Thank you very much!
Alvin
As the title suggests, I'm trying to interface a PIC18F2220 with a SD card via SPI mode. I've not successfully initialize the SD card. I've sent 80+ clock pulses with the chip disabled and CMD0 with chip enabled. I should wait for a R1 Response with value of 0x01 before continuing to CMD1. However, I'm stucked here for few days trying to figure out why the card does not response with R1 (0x01).
I've followed the schematic below exactly:
And snippets of my codes:
Code:
/************************** MMC Init **************************************/
/* Initialises the MMC into SPI mode and sets block size, returns 0 on success */
int mmc_init()
{
int i;
spi_init();
spi_disable_cs(); // set SS = 1 (off)
for(i=0;i<12;i++) // initialise the MMC card into SPI mode by sending clks on
{
spi_write(0xFF);
}
spi_enable_cs(); // set SS = 0 (on) tells card to go to spi mode when it receives reset
spi_write(0x40); // send reset command
spi_write(0x00); // all the arguments are 0x00 for the reset command
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x95); // precalculated checksum as we are still in MMC mode
if(mmc_response(0x01)==1) return 1; // if = 1 then there was a timeout waiting for 0x01 from the mmc
/************************** MMC get response **************************************/
/**** Repeatedly reads the MMC until we get the response we want or timeout ****/
int mmc_response(unsigned char response)
{
unsigned long count = 0xFFFF; // 16bit repeat, it may be possible to shrink this to 8 bit but there is not much point
while(spi_write(0xFF) != response && --count > 0);
if(count==0) return 1; // loop was exited due to timeout
else return 0; // loop was exited before timeout
}
Functions of SPI
Code:
void spi_init(void)
{
SPI_SDI = 1;
SPI_SDO = 0;
SPI_SCK = 0;
SPI_SS = 0;
LATCbits.LATC2 = 1; //disable chip select
SSPSTATbits.SMP = 0;
SSPSTATbits.CKE = 1;
SSPCON1bits.CKP = 0;
SSPCON1bits.SSPEN = 1;
SSPCON1bits.SSPM0 = 0;
SSPCON1bits.SSPM1 = 1;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM3 = 0;
}
void spi_disable_cs (void)
{
LATCbits.LATC2 = 1;
}
void spi_enable_cs (void)
{
LATCbits.LATC2 = 0;
}
int spi_write(int data)
{
SSPBUF = data;
while(!SSPSTATbits.BF);
return SSPBUF;
}
My program got timeout while waiting for R1 response, any idea what's wrong with the circuit of the code?
Thank you very much!
Alvin