51 MCU read and write SD card program

Publisher:Xingfu8888Latest update time:2015-08-07 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
SD card introduction
SD card is a new generation of memory device based on semiconductor flash memory. SD was successfully developed in August 1999 and weighs only 2 grams. However, it has high memory capacity, fast data transfer rate, great mobility and good security. SD card is also easy to reformat and has a wide range of applications. For example, multimedia files such as music, movies, news can be easily saved. Digital cameras also begin to support SD cards. The capacity of SD card can reach up to 4GB.
SD card combines SanDisk flash memory card control and MLC (Multilevel Cell) technology and Toshiba 0.16u and 0.13u NAND technology in a volume of 24mm×32mm×2.1mm. It is connected to a special drive through a 9-pin interface and does not require additional power to maintain the information stored on it. Moreover, it is an integrated solid medium without any moving parts, so there is no need to worry about damage to mechanical movement.
  SD card (Secure Digital Memory Card) is a memory card based on semiconductor flash memory technology. It was jointly developed by Panasonic, Toshiba and SanDisk in the United States in 1999. In 2000, these companies initiated the establishment of the SD Association (Secure Digital Association, SDA for short), which has a strong lineup and attracted a large number of manufacturers to participate. These include IBM, Microsoft, Motorola, NEC, Samsung, etc. Driven by these leading manufacturers, SD cards have become the most widely used storage card in consumer digital devices. SD cards are multifunctional storage cards with large capacity, high performance, and security. Compared with MMC cards, they have an additional cryptographic authentication function for data copyright protection (SDMI specification), and the reading and writing speed is 4 times faster than that of MMC cards.
Okay, since it is so good, we give here the program for the 51 single-chip microcomputer to read and write 2G SD card:
#include //The program passes debugging
#include
//===============================================================
//Define the 4 signal lines required by the SD card
sbit SD_CLK = P3^7;
sbit SD_DI = P3^5;
sbit SD_DO = P3^6;
sbit SD_CS = P3^4;
//=============================================================
//Define the key port
sbit KEY = P2^7;
//=============================================================
//Define a 512-byte buffer. Note that the xdata keyword needs to be used
unsigned char xdata DATA[512];
void delayms(unsigned int count)
{
int i,j;
for(i=0;i
{
for(j=0;j<260;j++);
}
}
//=================================================================
//Write one byte to the SD card, simulating the SPI bus mode
void SdWrite(unsigned char n)
{
unsigned char i;
for(i=8;i;i--)
{
SD_CLK=0;
SD_DI=(n&0x80);
n<<=1;
SD_CLK=1;
}
SD_DI=1;
}
//=============================================================
//Read one byte from SD card, simulating SPI bus mode
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i;i--)
{
SD_CLK=0;
SD_CLK=1;
n<<=1;
if(SD_DO) n|=1;
}
return n;
}
//==============================================================
//Detect the response of the SD card
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i<=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//========================================================================
//Send command to SD card
void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
{
SdWrite(command|0x40);
SdWrite(((unsigned char *)&argument)[0]);
SdWrite(((unsigned char *)&argument)[1]);
SdWrite(((unsigned char *)&argument)[2 ]);
SdWrite(((unsigned char *)&argument)[3]);
SdWrite(CRC);
}
//======================== ========================================
//Initialize SD card
unsigned char SdInit(void )
{
int delay=0, trials=0;
unsigned char i;
unsigned char response=0x01;
SD_CS=1;
for(i=0;i<=9;i++)
SdWrite(0xff);
SD_CS=0;
//Send Command 0 to put MMC in SPI mode
SdCommand(0x00,0,0x95);

response=SdResponse();
if( response!=0x01)
{
return 0;
}
while(response==0x01)
{
SD_CS=1;
SdWrite(0xff);
SD_CS=0;
SdCommand(0x01,0x00ffc000,0xff);
response=SdResponse();
}
SD_CS=1 ;
SdWrite(0xff);
return 1;
}[page]
//================================================ ================
//Write data to the specified address of the SD card, up to 512 bytes at a time
//unsigned char SdWriteBlock(unsigned char *Block, unsigned long address, int len)
unsigned char SdWriteBlock(unsigned long address,int len)
{
unsigned int count;
unsigned char dataResp;
//Block size is 512 bytes exactly
//First Lower SS
SD_CS=0;
//Then send write command
SdCommand(0x18,address,0xff) ;
if(SdResponse()==00)
{
SdWrite(0xff);
SdWrite(0xff);
SdWrite(0xff);
//command was a success - now send data
//start with DATA TOKEN = 0xFE
SdWrite(0xfe);
//now send data
//for(count=0;count for(count=0;count //for(count=0;count for(;count<512;count++) SdWrite(0);
//data block sent - now send checksum
SdWrite(0xff); //Two-byte CRC check, 0XFFFF means CRC is not considered
SdWrite(0xff);
//Now read in the DATA RESPONSE token
dataResp=SdRead();
//Following the DATA RESPONSE token
//are a number of BUSY bytes
//a zero byte indicates the MMC is busy
while(SdRead()==0);
dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
SD_CS=1;
SdWrite(0xff);
if(dataResp==0x0b)
{
//printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR ");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token. ");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC. ");
return 0;
}
//==== ================================================== =================
//Read data from the specified address of the SD card, up to 512 bytes at a time
unsigned char SdReadBlock(unsigned char *Block, unsigned long address, int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
//printf("MMC_read_block ");
SD_CS=0;
//Then send write command
SdCommand(0x11,address,0xff);
if(SdResponse()==00)
{
//command was a success - now send data
//start with DATA TOKEN = 0xFE
while(SdRead()!=0xfe);
for(count=0;count for(;count<512;count++) SdRead();
//data block sent - now send checksum
SdRead();
SdRead();
//Now read in the DATA RESPONSE token
SD_CS=1;
SdRead();
return 1;
}
//printf("Command 0x11 (Read) was not received by the MMC. ");
return 0;
}

void initbaud(void)
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD;
PCON=0X00;
TR1=1;
SCON=0X50;//8-bit baud variable
//SCON=0X52;//8-bit baud variable TI interrupt
}
//================== ============================================
//Main program
main()
{
unsigned int i;
unsigned long AddTemp=331264; //The initial value of the physical address of the first data of the SD card address. You can use winhex to view it. Here is the 641 physical sector, 512x641=328192. Change
delayms(5) according to the actual SD card content. ;
SdInit(); //SD card initialization
while(1)
{
SdWriteBlock(AddTemp,512);
delayms(1000);
AddTemp=331264;
SdReadBlock(DATA,AddTemp,512);//Read 512 bytes each time and put them into the buffer
initbaud();
for(i=0;i<512;i++)
{
SBUF=DATA[i];
while(!TI );
TI=0;
delayms(1);
}
while(KEY); //Wait for the key to be pressed to continue execution

}
}
You can see the data written to the SD card in the serial port. The program is developed in MCU-51/AVR The board was experimented. For more information, please visit:
http://item.taobao.com/item.htm?spm=0.0.0.50.xZjZ87&id=21441052281 , http://item.taobao.com/item.htm?spm=1103*oQM.3 -5SusJ.h-2Yh1mq&id=14049701171& These are two powerful 51 and AVR development boards, which are good choices for beginners and R&D personnel. In the next section, I will tell you about the relevant reading of the TFT true color screen combined with the SD card. Write!
51 MCU read and write SD card program
51 MCU read and write SD card program

Reference address:51 MCU read and write SD card program

Previous article:Parameter calculation of RC automatic reset circuit of single chip microcomputer
Next article:Detailed explanation of MCS-51 microcontroller special function registers

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号