Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing PIC18F2220 with SD card via SPI

Status
Not open for further replies.

alvinthen

Member level 2
Joined
Apr 15, 2010
Messages
51
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,628
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:
sdcardshem.gif


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
 

Hi,

Who is the manufacture of the SD card? What is the capacity, 2GB, 4GB, etc. Is it a straight SD or SDHC?

The reason I ask it that the protocols are slightly different.

I'll look your code over and see if anything jumps out at me. It's been a while since I wrote these routines from scratch.
 
Last edited:

Hi,
I've tried the 512mb micro sd card and 128mb mini sd card from nokia (took out from an old phone), transcend 2gb micro sd, all are used with SD card adapter
Thanks!
 

Can you upload a zipped file of the whole project? What compiler are you using? Have you check supply voltage of the circuit?
 

Hi, I've uploaded the zipped file. The power supply was from the project board. I'm using C18.
There's one thing to mention about, from the schmatic above, the voltage after the red LED is not always 3.3V when connected to the MMC. not sure what caused this.
Thanks!
 

Attachments

  • MMC3.zip
    40.5 KB · Views: 89

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top