您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 用51单片机读写SD卡
2012.1151SDSD,。MP3、SD。SD,、、、。,,。SD。SD,,SD、。1SDSDSD,、。1.1SDSD1,1。SD,SD,SPI。,。SDSD,SPI。SPI,SPI,,,SD,SPI。1.2SPISPI,,SDS。SDCMDACMD,,。,CMD17,。SPI,26。“01”,64(,),7CRC1“1”。SD。SPI,3:R1、R2R3,1、23。R1,3。,1。R2R31R1,SD。1SD(SD)(SPI)1DAT3/CS3/(SS)2CMD/DI(MOSI)3VSS14VDD5CLK(SCK)6VSS27DAT0/DO0(MISO)8DAT1/IRQ19DAT2/NC21SD7,06543CRC3R1:SD,SPI51SD。:51;SD;2-5601CRC12PROGRAMLANGUAGE19210251STC89C52SD,SPI,4P14,P15,P16,P17。515V,SD3.3V,。SD7DO,3.3V51,。2。3SPI,SD,SD、SD。3.1SPI//=======================================//SD,SPIvoidSD_spi_write(unsignedcharn){unsignedchari;for(i=0;i8;i++){SD_CLK=0;if(is_init)delay(DELAY_TIME);SD_DI=(n&0x80)7;SD_CLK=1;if(is_init)delay(DELAY_TIME);n=1;}}//======================================//SD,SPIunsignedcharSD_spi_read(){unsignedcharn,i;for(i=0;i8;i++){n=1;SD_CLK=0;if(is_init)delay(DELAY_TIME);SD_DO=1;if(SD_DO)n|=1;SD_CLK=1;if(is_init)delay(DELAY_TIME);}returnn;}3.2SDSD,。,R1,R1。3.3SD//========================================//SDunsignedcharSdCommand(unsignedcharcommand,unsignedlongargument,unsignedcharCRC){unsignedchartemp,time=0;SD_CS=1;SD_spi_write(0xff);//,,SDSD_CS=0;SD_spi_write(command|0x40);SD_spi_write(((unsignedchar*)&argument)[0]);SD_spi_write(((unsignedchar*)&argument)[1]);SD_spi_write(((unsignedchar*)&argument)[2]);SD_spi_write(((unsignedchar*)&argument)[3]);SD_spi_write(CRC);do{temp=SD_spi_read();//,0xfftime++;}while((temp==0xff)&&(timeTRY_TIME));return(temp);}SD。SPI2DAT2/NCDAT3/CSCMD/DIVSS1VDDCLK/SCKVSS2DAT0/DODAT1/IRQ202012.11400kHz,74。,CMD0,SDSPI,CRC,0x95。SD(0x01),CMD1,,0x00。,512。SPI,。SD3。SD:/************************************************-:SD,CMD0,SDSPI-:SD-:,-:-:,0x00,INIT_CMD0_ERROR(sd.h)***********************************************/externunsignedcharis_init;unsignedcharSD_Reset()//SD,SPI,//CMD0(0){unsignedchari,time,temp;is_init=1;//is_init1SD_CS=1;for(i=0;i0x0f;i++)//,74,//!!!SD_spi_write(0xff);//120SD_CS=0;//time=0;do{temp=SdCommand(0x00,0,0x95);//CMD0//UART_Send_Byte(temp);//time++;if(time==TRY_TIME){SD_CS=1;//return(INIT_CMD0_ERROR);//CMD0}}while(temp!=0x01);SD_CS=1;//SD_spi_write(0xff);//SD8return0;//0,}SD:/**********************************************-:SD,CMD1-:SD-:,-:-:,0x00,INIT_CMD1_ERROR(sd.h)*********************************************/externunsignedcharis_init;unsignedcharSD_Init()//,CMD1(1){unsignedchartime,temp;SD_CS=0;//time=0;do{temp=SdCommand(0x01,0,0xff);//UART_Send_Byte(temp);//time++;if(time==TRY_TIME){SD_CS=1;//return(INIT_CMD1_ERROR);//CMD1}}while(temp!=0);is_init=0;//,is_init0,//SD_CS=1;//SD_spi_write(0xff);//SD8return(0);//0,}3.4SDSPI,。4。,MCF51QE128CMD17,SD。0xfe,SD512,2CRC。:/*************************************************3SD4PROGRAMLANGUAGE21-:addr512buffer-:SD-:,-:addr:buffer:-:,0x00,READ_BLOCK_ERROR(sd.h)-:SD,,SPI,***********************************************/unsignedcharSD_Read_Sector(unsignedlongaddr,unsignedchar*buffer)//SD512,//CMD17(17){unsignedintj;unsignedchartime,temp;SD_CS=0;//time=0;do{//addr=addr*512()temp=SdCommand(17,addr9,0xff);//CMD17time++;if(time==TRY_TIME){return(READ_BLOCK_ERROR);//}}while(temp!=0);//0while(SD_spi_read()!=0xfe);//,0xfe,//512for(j=0;j512;j++)//{buffer[j]=SD_spi_read();}SD_spi_read();SD_spi_read();//CRC,SD_CS=1;//SDSD_spi_read();//SD8return0;}。MCF51QE128CMD24,SD0xfe,5122CRC。,SD1,5。SD,0x05。,,。:/***********************************************-:buffer512SDaddr-:SD-:,-:addr:buffer:-:,0x00,WRITE_BLOCK_ERROR(sd.h)-:SD,,SPI,**********************************************/unsignedcharSD_Write_Sector(unsignedlongaddr,unsignedchar*buffer)//SD512//,CMD24(24){unsignedchartemp,time;unsignedinti;//UART_Send_Byte(is_init);SD_CS=0;//SDtime=0;do{//addr=addr*512()[//SD4G]temp=SdCommand(24,addr9,0xff);//CMD24time++;if(time==TRY_TIME){//UART_Send_Str();SD_CS=1;//return(temp);//}}while(temp!=0);//0SD_spi_write(0xff);SD_spi_write(0xff);SD_spi_write(0xff);//SD_spi_write(0xfe);//0xfe,//512for(i=0;i512;i++)//512SDSD_spi_write(buffer[i]);SD_spi_write(0xff);SD_spi_write(0xff);//CRC,temp=SD_spi_read();//5(28)226//UART_Send_Byte(temp);if((temp&0x1F)!=0x05)//XXX00101//SD{//UART_Send_Str();SD_CS=1;return(WRITE_BLOCK_ERROR);//}while(SD_spi_read()==0x00);//SD(//,SDFLASH,//)//,0x00,,0xffSD_CS=1;//SD_spi_write(0xff);//SD8return(0);//0,},SD621,701,80。,701,,。:for(i=0;i80;i++){SD_Read_Sector(621+i,DATA);SD_Write_Sector(701+i,DATA);}6。(:2012-02-12)}privatestaticIUnityContainerBuildUnityContainer(){varcontainer=newUnityContainer().RegisterTypeIDatabaseFactory,DatabaseFactory(newHierarchicalLifetimeManager()).RegisterTypeIUnitOfWork,UnitOfWork().RegisterTypeIReaderService,ReaderService()…//.RegisterTypeIRepositoryReader,ReaderRepository();container.RegisterControllers();returncontainer;}Global.asax:protectedvoidApplication_Start(){…//Bootstrapper.Initialise();},Web,WinForm,PDA,,。,,JS、JQuery、Ajax、DIV+CSS、AS-Flash、、、、、,。6Web,,,,,,“”“”,,、、、。[1]()EricEvans---[M],,:,2006.[2]()TimMcCarthC#2008[M]UMLChina,:,2010.[3]()MartinFowler[M],,:,2011.[4][EB/OL],2010.[5]EntityFramework4.1[E
本文标题:用51单片机读写SD卡
链接地址:https://www.777doc.com/doc-8542733 .html