add

Your Ad Here

Monday, 21 February 2011

Interfacing SD Card with 8051 (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.

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.
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.





17 comments:

Anonymous said...

Thank You

Naseer Ahmad said...

I read your article about SD Card. I think the function of SD Card are so comfortable for software design activities.Plastic Card

kalita said...

can you please send me the assembly language version of the program. I am not comfortable with C language.

Unknown said...

can i use this code for at89c51?

Anonymous said...

can I see the interfacing to 62256 circuit ?

Jigs PATEL said...

hello sir......
i want to know about circuit diagram for sd card with at89s52 and also want to know about interfacing of 62256 external memory...
i am waiting for ur reply....
thank you......

Anonymous said...

can this be used fot AT89s8253? what are the header files required to debug in keil?

Unknown said...

Could you post the full code for sd card interface

Unknown said...

Sir
I am interested in interfacing SD Card with 8052 series of microcontroller ( possibly P89V51RD2 with SPI communication facility.
However I m not well versed with C coding and hence request Assembly language codes.
Would be obliged with help from the forum.

Anonymous said...

I strongly disagree about one point.
The SD card data output should (must) not be connected directly to the 80x51xx port pin, unless you are using Port 0.
This family of MCU does have internal pull-ups to VCC (5V) which can potentially damage the SD card.
In static operation, ports pins use weak pull-ups, but in transient mode, strong pull-ups are used to speed up transitions and these pull-ups can damage the SD card.
This restriction does not apply to Port 0 because it uses a different configuration (open drain) and does not have any kind of pull-ups.
A voltage translator (or an NPN transistor or low power N channel MOSFET (e.g. a BS170)) is required for safe operation.

Manu said...
This comment has been removed by the author.
Manu said...

hi guys if u need c code for SD card interfacing wiht p89v51rd2 u can see the video of the project and the download the c code form blog http://embeddedinrajasthan.blogspot.in/?view=classic

Unknown said...
This comment has been removed by the author.
Unknown said...

What is hex value of CMD24 AND CMD25??

Unknown said...

I have tried very hard and finally got success on writing and reading a block. Now the program is tested only on Proteus. Thank you.

Unknown said...

My processor 8952.
Sd card adaptor.
Language assembly only.

Unknown said...

Yes

Post a Comment