新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 51單片機讀寫SD卡程序

51單片機讀寫SD卡程序

作者: 時間:2016-11-29 來源:網(wǎng)絡 收藏

//================================================================
//往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;countfor(count=0;count//for(count=0;countfor(;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;countfor(;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ù),程序在MCU-51/AVR開發(fā)板進行了實驗。具體介紹可訪問:
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& 這是兩款功能強大的51、AVR開發(fā)板,對初學者和研發(fā)人員都是不錯的選擇,下一節(jié)給大家講下結(jié)合SD卡的TFT真彩屏的相關(guān)讀寫!




上一頁 1 2 下一頁

關(guān)鍵詞: 51單片機讀寫SD

評論


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

關(guān)閉