單片機(jī)對SD卡讀寫系列(一)
SD卡在24mm×32mm×2.1mm的體積內(nèi)結(jié)合了〔SanDisk〕快閃記憶卡控制與MLC(Multilevel Cell)技術(shù)和Toshiba(東芝)0.16u及0.13u的NAND技術(shù),通過9針的接口界面與專門的驅(qū)動器相連接,不需要額外的電源來保持其上記憶的信息。而且它是一體化固體介質(zhì),沒有任何移動部分,所以不用擔(dān)心機(jī)械運動的損壞。
本文引用地址:http://m.butianyuan.cn/article/201611/320310.htmSD卡(Secure Digital Memory Card)是一種基于半導(dǎo)體閃存工藝的存儲卡,1999年,由日本松下、東芝及美國SanDisk公司共同研制完成。2000年,這幾家公司發(fā)起成立了SD協(xié)會(Secure Digital Association簡稱SDA),陣容強(qiáng)大,吸引了大量廠商參加。其中包括IBM,Microsoft,Motorola,NEC、Samsung等。在這些領(lǐng)導(dǎo)廠商的推動下,SD卡已成為目前消費數(shù)碼設(shè)備中應(yīng)用最廣泛的一種存儲卡。SD卡具有大容量、高性能、安全等多種特點的多功能存儲卡,它比MMC卡多了一個進(jìn)行數(shù)據(jù)著作權(quán)保護(hù)的暗號認(rèn)證功能(SDMI規(guī)格),讀寫速度比MMC卡要快4倍。
好了,既然這么好,我們在這里給出51單片機(jī)讀寫2G SD卡程序:
#include //程序通過調(diào)試
#include
//=============================================================
//定義SD卡需要的4根信號線
sbit SD_CLK = P3^7;
sbit SD_DI
sbit SD_DO
sbit SD_CS
//===========================================================
//定義按鍵端口
sbit KEY = P2^7;
//===========================================================
//定義512字節(jié)緩沖區(qū),注意需要使用 xdata關(guān)鍵字
unsigned char xdata DATA[512];
void delayms(unsigned int count)
{
}
//===========================================================
//寫一字節(jié)到SD卡,模擬SPI總線方式
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;
}
//===========================================================
//從SD卡讀一字節(jié),模擬SPI總線方式
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;
}
//============================================================
//檢測SD卡的響應(yīng)
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i<=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//================================================================
//發(fā)命令到SD卡
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);
}
//================================================================
//初始化SD卡
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;
}
//================================================================
//往SD卡指定地址寫數(shù)據(jù),一次最多512字節(jié)
//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); //兩字節(jié)CRC校驗, 為0XFFFF 表示不考慮CRC
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;
}
//=======================================================================
//從SD卡指定地址讀取數(shù)據(jù),一次最多512字節(jié)
unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
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;
}
return 0;
}
void initbaud(void)
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD;
PCON=0X00;
TR1=1;
SCON=0X50;//8位波特可變
//SCON=0X52;//8位波特可變 TI開中斷
}
//============================================================
//主程序
main()
{
}
可以在串口中看到SD卡中被寫入的數(shù)據(jù)
評論