SD卡簡介
SD卡是Secure Digital Card卡的簡稱,直譯成漢語就是“安全數字卡”,是由日本松下公司、東芝公司和美國SANDISK公司共同開發研制的全新的存儲卡產品。SD存儲卡是一個完全開放的標準(系統),多用于MP3、數碼攝像機、數碼相機、電子圖書、AV器材等等,尤其是被廣泛應用在超薄數碼相機上。SD卡在外形上同MultiMedia Card卡保持一致,大小尺寸比MMC卡略厚,容量也大很多。并且兼容MMC卡接口規范。不由讓人們懷疑SD卡是MMC升級版。
另外,SD卡為9引腳,目的是通過把傳輸方式由串行變成并行,以提高傳輸速度。它的讀寫速度比MMC卡要快一些,同時,安全性也更高。SD卡最大的特點就是通過加密功能,可以保證數據資料的安全保密。它還具備版權保護技術,所采用的版權保護技術是DVD中使用的CPRM技術(可刻錄介質內容保護)。
單片機讀取sd卡數據_51單片機讀寫SD卡程序詳解
#include 《reg52.h》//程序通過調試
#include 《stdio.h》
//=============================================================
//定義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字節緩沖區,注意需要使用 xdata關鍵字
unsigned char xdata DATA[512];
void delayms(unsigned int count)
{
int i,j;
for(i=0;i《count;i++)
{
for(j=0;j《260;j++);
}
}
//===========================================================
//寫一字節到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卡讀一字節,模擬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卡的響應
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i《=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//================================================================
//發命令到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卡指定地址寫數據,一次最多512字節
//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《len;count++) SdWrite(*Block++);
for(count=0;count《len;count++) SdWrite(count);
//for(count=0;count《len;count++) SdWrite(0xFF);
for(;count《512;count++) SdWrite(0);
//data block sent - now send checksum
SdWrite(0xff); //兩字節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\n”);
return 0;
}
if(dataResp==0x05)
return 1;
//printf(“Invalid data Response token.\n”);
return 0;
}
//printf(“Command 0x18 (Write) was not received by the MMC.\n”);
return 0;
}
//=======================================================================
//從SD卡指定地址讀取數據,一次最多512字節
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\n”);
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《len;count++) *Block++=SdRead();
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.\n”);
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卡地址第一個數據物理地址初始值,可以用winhex查看,這里是641物理扇區,512x641=328192,根據實際SD卡內容更改
delayms(5);
SdInit(); //SD卡初始化
while(1)
{
SdWriteBlock(AddTemp,512);
delayms(1000);
AddTemp=331264;
SdReadBlock(DATA,AddTemp,512);//每次讀出512字節放到緩沖區
initbaud();
for(i=0;i《512;i++)
{
SBUF=DATA[i];
while(!TI);
TI=0;
delayms(1);
}
while(KEY); //等待按鍵按下繼續執行
}
}
評論
查看更多