Sunday, 22 September 2013

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.

1 comment: