SD卡,是一种依据半导体快闪回忆器的新一代回忆设备,SD于1999年8月研发成功,其分量只要2克。但却具有高回忆容量、快速数据传输率、极大的移动灵活性以及很好的安全性。SD卡也很简单从头格式化,有广泛的运用领域,如音乐、电影、新闻等多媒体文件都能够方便地保存,数码相机也开端支撑SD卡。SD卡容量最高能到达4GB。
SD卡在24mm×32mm×2.1mm的体积内结合了〔SanDisk〕快闪回忆卡操控与MLC(Multilevel Cell)技能和Toshiba(东芝)0.16u及0.13u的NAND技能,经过9针的接口界面与专门的驱动器相连接,不需求额定的电源来坚持其上回忆的信息。并且它是一体化固体介质,没有任何移动部分,所以不必忧虑机械运动的损坏。
SD卡(Secure Digital Memory Card)是一种依据半导体闪存工艺的存储卡,1999年,由日本松下、东芝及美国SanDisk公司一起研发完结。2000年,这几家公司建议成立了SD协会(Secure Digital Association简称SDA),阵容强大,招引了很多厂商参与。其间包含IBM,Microsoft,Motorola,NEC、Samsung等。在这些领导厂商的推进下,SD卡已成为现在消费数码设备中运用最广泛的一种存储卡。SD卡具有大容量、高性能、安全等多种特色的多功用存储卡,它比MMC卡多了一个进行数据著作权维护的暗号认证功用(SDMI标准),读写速度比MMC卡要快4倍。
好了,已然这么好,咱们在这里给出51单片机读写2G SD卡程序:
#include //程序经过调试
#include
//=============================================================
//界说SD卡需求的4根信号线
sbit SD_CLK = P3^7;
sbit SD_DI
sbit SD_DO
sbit SD_CS
//===========================================================
//界说按键端口
sbit KEY = P2^7;
//===========================================================
//界说512字节缓冲区,留意需求运用 xdata关键字
unsigned char xdata DATA[512];
void delayms(unsigned int count)
{
}
//===========================================================
//写一字节到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,0×95);
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
for(count=0;count
//for(count=0;count
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”);
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卡指定地址读取数据,一次最多512字节
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卡中被写入的数据