SD卡简介
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;
}