►Initializing SD/MMC card

A. Setting up the card for SPI Communication

Initializing the card, and setting it up for communication is the very first step. These are the most fundamental steps necessary before the card can be accessed. The CardType will be set as either a ‘0’, which is a MMC card, and cannot be used by the SPI interface, a ‘1’which is an SD card, version 1, or a ‘2’, which is an SD card, version 2

BYTE SD_Init()
{
   WORD CardStatus; // R2 value from status inquiry...
   WORD Count;      // local counter
   
   // Global CardType - b0:MMC, b1:SDv1, b2:SDv2

   /* initial speed is slow... */
   SPI_Init( kHz400 );
   
   /* disable SPI chip select... */
   SPI_DisableCS();

   /* fill send data with all ones - 80 bits long to   */
   /* establish link with SD card this fulfills the    */
   /* 74 clock cycle requirement...  */
   for(Count=0;Count<10;Count++)
      SPI_Byte( 0xFF );

   /* enable the card with the CS pin... */
   SPI_EnableCS();

   /* ************************************************ */
   /* SET SD CARD TO SPI MODE - IDLE STATE...          */
   /* ************************************************ */
   Count = 1000;     // one second...
   CardType = 0;
   
   /* wait for card to enter IDLE state... */
   do 
   {
      Delay(1);
      SD_Command( CMD_GO_IDLE_STATE, 0 );
   } while((SD_GetR1() != IDLE_STATE) && (--Count));  

   /* timeout if we never made it to IDLE state... */
   if( !Count )
      return( SD_TIME_OUT );

   /* ************************************************ */
   /* COMPLETE SD CARD INITIALIZATION                  */
   /* FIGURE OUT WHAT TYPE OF CARD IS INSTALLED...     */
   /* ************************************************ */
   Count = 2000;     // two seconds...
   
   /* Is card SDSC or MMC? */
   SD_Command( CMD_APP_CMD, 0 );
   SD_Command( ACMD_SEND_OP_COND, 0 );
   if( SD_GetR1() <= 1 )
   {
      CardType = 2;
   }
   else
   {
      CardType = 1;
   }
   
   /* wait for initialization to finish... */
   do
   {
      Delay(1);
      if( CardType == 2 )
      {
         SD_Command( CMD_APP_CMD, 0 );
         SD_Command( ACMD_SEND_OP_COND, 0 );
      }
      else
      {
         SD_Command( CMD_SEND_OP_COND, 0 );
      }
   } while(SD_GetR1() && --Count);

   if( !Count )
      return( SD_TIME_OUT );

   /* ************************************************ */
   /* QUERY CARD STATUS...                             */
   /* ************************************************ */
   SD_Command( CMD_SEND_STATUS, 0 );
   CardStatus = SD_GetR2();

   if( CardStatus )
      return( SD_ERROR );

   /* ************************************************ */
   /* SET BLOCK SIZE...                                */
   /* ************************************************ */
   SD_Command( CMD_SET_BLOCKLEN, 512 );
   if( SD_GetR1() )
   {
      CardType = 0;
      return( SD_ERROR );
   }

   /* ************************************************ */
   /* SWITCH TO HIGHEST SPI SPEED...                   */
   /* ************************************************ */
   SPI_Init( MHz10 );

   /* disable the card with the CS pin... */
   SPI_DisableCS();

   /* return OK... */
   return( 0 );
}



Tutorial Index
Interface to Chan’s Library of functions SD Card Initialization
Target development platform Reading and Writing a single sector
Setting up the SPI port during startup.A51 Working with diskio.c
Global type definitions and variables Pulling it all together
Basic SPI function Index Page

Conversation

Sun May 19 2019, 04:43 AM
Elmo Oxendine
http://bit.ly/2PVgtFh
Tue May 14 2019, 12:38 AM
Jayson Contreras
Hello
Xkalebar
Sun Apr 28 2019, 09:06 AM
Please provide me the code for gsm based sms controlled notice board
ExperimenterUK
Sun Apr 14 2019, 10:52 PM
@hassan malik post your problem in the "General help Guidance and Discussion" forum
Sun Apr 14 2019, 05:06 PM
RAJESH MAPARi
please send me code to interface atmega32 with 24c1024 serial eeprom
Sun Apr 14 2019, 01:33 PM
hassan malik
hello sir i installed proteus 7.8 professional on window 7 ultimate and window 10 but there would be same errors onn both windows. the error is bad license key.please any one help me
Sun Apr 14 2019, 01:32 PM
hassan malik
hello sir i installed proteus 7.8 professional on window 7 ultimate and window 10 but there would be same errors onn both windows. the error is bad license key.
ExperimenterUK
Sat Apr 06 2019, 05:59 PM
@deshrar. Please post your question in 8051 forum
Sat Apr 06 2019, 07:43 AM
deshraj
hallo mam.. i have downloded gsm notice board code its running fine on lcd but how so send msg to the module . i mean format of password and msg.. thank you
kevinharsha
Wed Apr 03 2019, 04:38 PM
Can i see the circuit diagram for the music tone library 8051 project?

Downloads

Comments

dhiyamtech
Thu May 09 2019, 10:20 AM
harsha.reddy
Fri May 03 2019, 09:02 AM
soumen1963
Fri May 03 2019, 06:01 AM
Vasily
Mon Apr 29 2019, 04:03 AM
Xkalebar
Sun Apr 28 2019, 08:59 AM
obibikwe
Fri Apr 19 2019, 05:55 PM
gigyani
Thu Apr 18 2019, 01:43 AM
Deekshaaan
Wed Apr 17 2019, 03:02 PM

Online

Guests: 23, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38065
Newest member: dhiyamtech
Forum Activity
Ansh12 Avatar
Posted By Ansh12
Sun Apr 28 2019, 02:15 PM
I made a minor change, it works in Proteus.Try the...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Apr 25 2019, 08:20 PM
I made a minor change, it works in Proteus.Try the...
gigyani Avatar
Posted By gigyani
Thu Apr 25 2019, 01:17 AM
when i add cufirst.hex its not working.after 2-3 s...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Apr 24 2019, 12:29 AM
http://www.8051projects.net/download-d205-electron...
gigyani Avatar
Posted By gigyani
Tue Apr 23 2019, 09:22 PM
How to enter the electronic voting machine into vo...
Ansh12 Avatar
Posted By Ansh12
Mon Apr 22 2019, 09:12 AM
Then flash the led when you get any input on B0.Le...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Apr 19 2019, 05:35 PM
The full error is control_unit.asm(1294): error A4...
gigyani Avatar
Posted By gigyani
Thu Apr 18 2019, 10:09 PM
I'm using keil uvision5. attached is the rar file ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Apr 18 2019, 04:58 PM
Please zip your full code and post it.Which assemb...
gigyani Avatar
Posted By gigyani
Thu Apr 18 2019, 02:01 AM
STATREAD:MOV A, #WTCMD ;LOAD WRITE CO...