Oct192009

Interfacing SD Card with AT89S52 microcontroller

SD card is preferred due to its small size, High memory capacity, non-volatile memory,  Low power consumption and low cost. So this memory product is used in most of the consumer electronic products.

If you want to make a data acquisition system which should store a vast amount of data then SD card is a very good choice. This article describes the interfacing of the SD card with AT89S52 microcontroller.

Interfacing SD card with AT89S52

SD card works at 3.3v TTL logic while the microcontroller works on a logic level of 5v CMOS level standards. so they cannot connected directly. If we connected the sure the SD card will be damaged.

To Solve this problem we need a voltage level converter circuit in between the SD card and the 89s52 microcontroller. We can use SN74LS4245 dedicated level converter chip, which perform the level conversion.

If you need to build a cheap level converter then we can make it with a simple NPN transistor. The circuit given below can be used to transfer the data to the SD card.

 3.3v-5v-level-converter-circuit

Connect the 5V side to the microcontroller and 3.3v side to the SD card. This circuit can only be used for transferring the data from microcontroller to SD card. And for transferring the data from the SD card to microcontroller you can directly connect the pins since the microcontroller 89S52 can read the data at 3.3v logic.

Communication Mode

There are 2 communication protocols for SD card. They are SD mode and SPI mode. SD mode is a standard SD card read-write mode. But it needs an SD card controller interface. But AT89S52 doesn't have an integrated SD card interface controller. so we go for the next mode which is SPI mode. It just needs an 4 wire for data communication. Also a lot of microcontrollers now in the market have inbuilt SPI interface circuit.

Although the AT89S52 doesn’t have an inbuilt SPI hardware controller we can use a software SPI controller to simulate the SPI bus.

Also AT89s52 has only 256byts of RAM while the sd card read and write blocks need 512byts at a time. So we need an external RAM which should be interfaced with the microcontroller. In this circuit we are using 62256 IC which has an capacity of 32KB.

Circuit Diagram

 

SD-card-interfacing-with at89s52-microcontroller

 

 

 

 

Software Design

During power on the sd card will be in SD bus mode. Send an reset command CMDO to enter into SPI mode. Sending data to the SD card is done by creating a software SPI bus. Use this following code to transmit data to the SD card.

sbit CS=P1^0;
sbit CLK= P1^1;
sbit DATaI=P1^2;
sbit DATaO=P1^3;
#define SD_Disable() CS=1 //Disable CS
#define SD_Enable() CS=0 //Enable CS

unsigned char SPI_TransferByte(unsigned char val)
{
unsigned char BitCounter;
for(BitCounter=8; BiCounter!=0; BitCounter--)
{ CLK=0;
DATaI=0; // write
if(val&0x80) DATaI=1;
val<<=1;
CLK=1;
if(DATaO)val|=1; // read
}
CLK=0;
return val;
}

 

Initializing the SD card

unsigned char SD_Init(void) { unsigned char retry,temp; unsigned char i; for (i=0;i<0x0f;i++) { SPI_TransferByte(0xff); //delay } SD_Enable(); //Enable Chip select

SPI_TransferByte(SD_RESET); //send a reset command SPI_TransferByte(0x00); SPI_TransferByte(0x00); SPI_TransferByte(0x00); SPI_TransferByte(0x00); SPI_TransferByte(0x95); SPI_TransferByte(0xff); SPI_TransferByte(0xff); retry=0; do{ temp=Write_Command_SD(SD_INIT,0); //Send the initialization command retry++; if(retry==100) //retry 100 times {SD_Disable(); //disable chip select

return(INIT_CMD1_ERROR); //If retry fails returns an error number } }while(temp!=0); SD_Disable(); //disable card return(TRUE); //return success }

Reading and Writing data Blocks

After the completion of the initialization of SD card we can carry out its read and write operations.

SPI Bus mode supports single block (CMD24) and multi-block (CMD25) write operation. Multi-block operation is to write down starting from the specified location, until the SD card before receiving a stop command CMD12 to stop.

A single block write operation of data block length of only 512 bytes.

For reading the SD card the CMD17 is used and the SD card will reply with 0XFE followed by the 512byte of data and last 2 bytes are CRC verification code.


Tags: , , , , ,

E-mail | Permalink | Trackback | Post RSSRSS comment feed tweet this 12 Responses

Comments


Jorge

Response by Jorge on 10/20/2009 4:41:21 AM

Very good blog and info!
On friday i research info about this topic, and now i find here.
Thank you and continue well




Sulax

Response by Sulax on 10/20/2009 4:18:43 PM


Wonderful stuff....

While it is very interesting, I would like to know the following.

1. Can we not individually Read or Write SD Card Memory Byte !

2. Say I want to Read or Write a specific memory location, is it
   possible or not ?

3. What memory sizes are available in SD Card ?

4. Where can I find all details for interfacing this SD Card !

Awaiting your feedback.

Rgds
Sulax




Joy

Response by Joy on 10/22/2009 5:45:00 PM

What if i use the ic which have inbuilt spi support ??




shweta

Response by shweta on 10/23/2009 3:32:48 PM

good job...

but have u tested this?
i want use this ckt for my college project




binu

Response by binu on 10/23/2009 5:45:33 PM

You can use the hardware SPI for send/receive data.




shoaib

Response by shoaib on 11/18/2009 2:50:40 AM

Thanks for this project, if possible for manual experiment,the  software programme in HEX to load 89C51 directly is requested




S.D. Dwivedi

Response by S.D. Dwivedi on 12/1/2009 6:15:21 AM

Dear Sir idia is very best for low Ram level MCUpls send full SD Card read and write code with scecified addresh. as 24c512 90H="R"and 91H="P".

SD Dwivedi Banda UP India.




enock

Response by enock on 1/7/2010 4:09:33 AM

hi, is it possible to interface sd card and M68HC08 Microcontroller. if yes, help me with some information for the same please.




hardik

Response by hardik on 1/21/2010 11:02:11 AM

i want complete & exact diagram for interfacing micro sd card with 8051
also i want the operation being done during practicle




Saket Arya

Response by Saket Arya on 1/23/2010 11:35:54 PM

Respected Sir,
          I want to design a 8051 or universel programmin kit for microcontrollers. So please help me out, give me some circuit diagrams and other details.
Thank You.
Regards
Saket Arya




Aji

Response by Aji on 3/10/2010 7:19:08 AM

this project is very usefull
pls displayed the hex file




Pankaj Sharma

Response by Pankaj Sharma on 5/31/2010 5:12:19 AM

Awesome Blog dude... Carry ON....



Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading