单片机spi驱动SD卡
ctorBuffer.dat);
//获取总扇区数
vinf->sector_count=sectorBuffer.dat[6]&0x03;
vinf->sector_count<=8;
vinf->sector_count+=sectorBuffer.dat[7];
vinf->sector_count<=2;
vinf->sector_count+=(sectorBuffer.dat[8]&0xc0)>>6;
//获取multiplier
vinf->sector_multiply=sectorBuffer.dat[9]&0x03;
vinf->sector_multiply<=1;
vinf->sector_multiply+=(sectorBuffer.dat[10]&0x80)>>7;
//获取SD卡的容量
vinf->size_MB=vinf->sector_count>>(9-vinf->sector_multiply);
//getthenameofthecard
Read_CID_SD(sectorBuffer.dat);
vinf->name[0]=sectorBuffer.dat[3];
vinf->name[1]=sectorBuffer.dat[4];
vinf->name[2]=sectorBuffer.dat[5];
vinf->name[3]=sectorBuffer.dat[6];
vinf->name[4]=sectorBuffer.dat[7];
vinf->name[5]=0x00;//endflag
}
以上程序将信息装载到一个结构体中,这个结构体的定义如下:
typedefstructSD_VOLUME_INFO
{//SD/SDCardinfo
unsignedintsize_MB;
unsignedcharsector_multiply;
unsignedintsector_count;
unsignedcharname[6];
}VOLUME_INFO_TYPE;
5)扇区读
扇区读是对SD卡驱动的目的之一。SD卡的每一个扇区中有512个字节,一次扇区读操作将把某一个扇区内的512个字节全部读出。过程很简单,先写入命令,在得到相应的回应后,开始数据读取。
扇区读的时序:
扇区读的程序例程:
unsignedcharSD_Read_Sector(unsignedlongsector,unsignedchar*buffer)
{
unsignedcharretry;
//命令16
unsignedcharCMD[]={0x51,0x00,0x00,0x00,0x00,0xFF};
unsignedchartemp;
//地址变换由逻辑块地址转为字节地址
sector=sector<9;//sector=sector*512
CMD[1]=((sector&0xFF000000)>>24);
CMD[2]=((sector&0x00FF0000)>>16);
CMD[3]=((sector&0x0000FF00)>>8);
//将命令16写入SD卡
retry=0;
do
{//为了保证写入命令一共写100次
temp=Write_Command_MMC(CMD);
retry++;
if(retry==100)
{
return(READ_BLOCK_ERROR);//blockwriteError!
}
}
while(temp!=0);
//ReadStartByteformMMC/SD-Card(FEh/StartByte)
//Nowdataisready,youcanreaditout.
while(Read_Byte_MMC()!=0xfe);
readPos=0;
SD_get_data(512,buffer);//512字节被读出到buffer中
return0;
}
其中SD_get_data函数如下:
//----------------------------------------------------------------------------
获取数据到buffer中
//----------------------------------------------------------------------------
voidSD_get_data(unsignedintBytes,unsignedchar*buffer)
{
unsignedintj;
for(j=0;j *buffer++=Read_Byte_SD(); } 6)扇区写 扇区写是SD卡驱动的另一目的。每次扇区写操作将向SD卡的某个扇区中写入512个字节。过程与扇区读相似,只是数据的方向相反与写入命令不同而已。 扇区写的时序: 扇区写的程序例程: //-------------------------------------------------------------------------------------------- 写512个字节到SD卡的某一个扇区中去返回0说明写入成功 //-------------------------------------------------------------------------------------------- unsignedcharSD_write_sector(unsignedlongaddr,unsignedchar*Buffer) { unsignedchartmp,retry; unsignedinti; //,命令24 unsignedcharCMD[]={0x58,0x00,0x00,0x00,0x00,0xFF}; addr=addr<9;//addr=addr*512 CMD[1]=((addr&0xFF000000)>>24); CMD[2]=((addr&0x00FF0000)>>16); CMD[3]=((addr&0x0000FF00)>>8); //写命令24到SD卡中去 retry=0; do {//为了可靠写入,写100次 tmp=Write_Command_SD(CMD); retry++; if(retry==100) { return(tmp);//sendcommamdError! } } while(tmp!=0); //在写之前先产生100个时钟信号 for(i=0;i<100;i++) { Read_Byte_SD(); } //写入开始字节 Write_Byte_MMC(0xFE); //现在可以写入512个字节 for(i=0;i<512;i++) { Write_Byte_MMC(*Buffer++); } //CRC-Byte Write_Byte_MMC(0xFF);//DummyCRC Write_Byte_MMC(0xFF);//CRCCode tmp=Read_Byte_MMC();//readresponse if((tmp&0x1F)!=0x05)//写入的512个字节是未被接受 { SPI_CS=1; return(WRITE_BLOCK_ERROR);//Error! } //等到SD卡不忙为止 //因为数据被接受后,SD卡在向储存阵列中编程数据 while(Read_Byte_MMC()!=0xff){}; //禁止SD卡 SPI_CS=1; return(0);//写入成功 } 此上内容在笔者的实验中都已调试通过。单片机采用STC89LE单片机(SD卡的初始化电压为2.0V~3.6V,操作电压为3.1V~3.5V,因此不能用5V单片机,或进行分压处理),工作于22.1184M的时钟下,由于所采用的单片机中没硬件SPI,采用软件模拟SPI,因此读写速率都较慢。如果要半SD卡应用于音频
单片机spiSD 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)