新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 單片機(jī)對SD卡讀寫系列(一)

單片機(jī)對SD卡讀寫系列(一)

作者: 時間:2016-11-23 來源:網(wǎng)絡(luò) 收藏
SD卡,是一種基于半導(dǎo)體快閃記憶器的新一代記憶設(shè)備,SD于1999年8月研制成功,其重量只有2克。但卻擁有高記憶容量、快速數(shù)據(jù)傳輸率、極大的移動靈活性以及很好的安全性。SD卡也很容易重新格式化,有廣泛的應(yīng)用領(lǐng)域,如音樂、電影、新聞等多媒體文件都可以方便地保存,數(shù)碼相機(jī)也開始支持SD卡。SD卡容量最高能達(dá)到4GB。

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

  SD卡(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= P3^5;
sbit SD_DO= P3^6;
sbit SD_CS= P3^4;
//===========================================================
//定義按鍵端口
sbit KEY = P2^7;
//===========================================================
//定義512字節(jié)緩沖區(qū),注意需要使用 xdata關(guān)鍵字
unsigned char xdata DATA[512];

void delayms(unsigned int count)
{
int i,j;
for(i=0;i
{
for(j=0;j<260;j++);
}
}
//===========================================================
//寫一字節(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

//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位波特可變
//SCON=0X52;//8位波特可變 TI開中斷
}
//============================================================
//主程序
main()
{
unsigned int i;
unsigned long AddTemp=331264;//SD卡地址第一個數(shù)據(jù)物理地址初始值,可以用winhex查看,這里是641物理扇區(qū),512x641=328192,根據(jù)實際SD卡內(nèi)容更改
delayms(5);
SdInit();//SD卡初始化
while(1)
{
SdWriteBlock(AddTemp,512);
delayms(1000);
AddTemp=331264;
SdReadBlock(DATA,AddTemp,512);//每次讀出512字節(jié)放到緩沖區(qū)
initbaud();
for(i=0;i<512;i++)
{
SBUF=DATA[i];
while(!TI);
TI=0;
delayms(1);
}
while(KEY);//等待按鍵按下繼續(xù)執(zhí)行

}
}
可以在串口中看到SD卡中被寫入的數(shù)據(jù)



關(guān)鍵詞: 單片機(jī)SD卡讀寫系

評論


技術(shù)專區(qū)

關(guān)閉