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.

[SOLVED] PIC18F8722 & Micro SD Card 512MB

Status
Not open for further replies.

moahrs

Junior Member level 2
Joined
Dec 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,459
Hi all,

I am using pic18F8722, c18 compiler and MPLAB 8.88. Using C18 lib (SPI).

I am a problem to use a Micro SD Card 512MB. The code I put below. The problems are 2 (I think they are related).

1) In the second part, when I remove MicroSD from idle (After CMD0), that I send one ACMD41 (The CMD1 not works in any case), the MicroSD leaves from idle, but after near 25 seconds.... its normal ?

2) in the third part, I sendthe CMD16, to change block size to 512B. In this part, the response is not 0x00, is another.

you guys can take a look at my code and see if there's anything I'm doing wrong ?

I take this code from another site. I not using MDD sample from Microchip because I Need to put, in this microcontroller, tcp/ip and fat mmc controler, and the TCP/IP stack + MDD FS not work together, and the sample TCP/IP with MDD not have to PIC18 (only to PIC24 and above).

And I know if I remove lot's of code that I not need, the TCP/IP and FAT mmc controler will work fine.

Its not complicate for me, I just do FAT in another projects (more simple), and works fine. My only problem is comunicate with MicroSD Card.

#include "HardwareProfile.h"
#include "spi.h"
#include "mmc2.h"
#include "UART.h"

// Written by Ed Waugh 2004, www.edwaugh.co.uk

// for the original source, and hundreds more examples of PIC C code, see:
// https://www.microchipc.com/sourcecode/#mmc

/************************** MMC Init **************************************/
/* Initialises the MMC into SPI mode and sets block size, returns 0 on success */
int mmc_init(void)
{
int i, a1, a2;

SD_CS_IO = 1; // ensure SPI memory device
// Chip Select is reset
OpenSPI1(SPI_FOSC_64, MODE_00, SMPMID);

for(i=0;i<10;i++) // initialise the MMC card into SPI mode by sending clks on
{
WriteSPI1(0xFF);
}

putrsUART("Initialization\n\r");

SD_CS_IO = 0; // set SS = 0 (on) tells card to go to spi mode when it receives reset

WriteSPI1(0x40); // send reset command
WriteSPI1(0x00); // all the arguments are 0x00 for the reset command
WriteSPI1(0x00);
WriteSPI1(0x00);
WriteSPI1(0x00);
WriteSPI1(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

SD_CS_IO = 1; // set SS = 1 (off)

putrsUART("Got response from MMC\n\r");

i = 0;

while(i < 255) // must keep sending command if response
{
putrsUART("trying response 0x00 from MMC\n\r");

WriteSPI(0xFF);

SD_CS_IO = 0; // set SS = 0 (on) tells card to go to spi mode when it receives reset

WriteSPI(0x77); // CMD55
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0xFF);

SD_CS_IO = 1; // set SS = 1 (off)
WriteSPI(0xFF);
SD_CS_IO = 0; // set SS = 0 (on) tells card to go to spi mode when it receives reset

WriteSPI(0x69); // CMD41
WriteSPI(0x40); // HCS=1
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0xFF);

if (mmc_response(0x00) == 0)
break;

SD_CS_IO = 1; // set SS = 1 (off)

i++;
}

if(i >= 254)
return 1; // if >= 254 then there was a timeout waiting for 0x00 from the mmc

putrsUART("Got out of idle response from MMC\n\r");

SD_CS_IO = 1; // set SS = 1 (off)

WriteSPI1(0xFF); // extra clocks to allow mmc to finish off what it is doing

SD_CS_IO = 0; // set SS = 0 (on)

WriteSPI1(0x50); // send mmc command one to bring out of idle state
WriteSPI1(0x00);
WriteSPI1(0x00);
WriteSPI1(0x02); // high block length bits - 512 bytes
WriteSPI1(0x00); // low block length bits
WriteSPI1(0xFF); // checksum is no longer required but we always send 0xFF

if((mmc_response(0x00))==1)
return 1;

SD_CS_IO = 1; // set SS = 1 (off)

putrsUART("Got set block length response from MMC\n\r");

CloseSPI1();
OpenSPI1(SPI_FOSC_4, MODE_00, SMPMID);

return 0;
}

/************************** 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(ReadSPI1() != response && --count > 0);

if(count==0)
return 1; // loop was exited due to timeout
else
return 0; // loop was exited before timeout
}



Ty a Lot
Moacir Jr.
 

I'm abandoning this idea.... I cant get help, and I am lost... I'm not complaining, just explaning.

Can close this thread.



Ty a lot.
Moacir Jr.
 

One of my personal suggestion..

Download Microchip Application Libraries and Use its MDD Library..

Few days i had implemented that and it works fine.

Even examples are available for PIC18F8722, and FAT16 and FAT32 Format Both are supported.
I don't much about these but had simulated everything on proteus properly..

Just give a try..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top