sd卡读写程序SD card read and write program文档格式.docx
- 文档编号:16906016
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:9
- 大小:16.64KB
sd卡读写程序SD card read and write program文档格式.docx
《sd卡读写程序SD card read and write program文档格式.docx》由会员分享,可在线阅读,更多相关《sd卡读写程序SD card read and write program文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
#defineeLATBbits.LATB6
Unsignedchar__attribute__((address(0x900)))lcd[3]={0,0,0};
Void(spi_init);
//declarationsysteminitialfunction
Void(spi_low);
//thatproduceslowbaudrate(usingtheSDcardinitializationfunction)
Void(spi_high);
//thatproducehighbaudratefunction(SDcardinitializationafteruse)
Unsignedchar(sd_reset);
//thattheSDcardinitializationfunction
UnsignedcharSD_SendCommand(unsignedcharCMDunsigned,longARG);
//writeSDcardcommandfunctionstatement
UnsignedcharSPI_WriteByte(unsignedcharVAL);
//writeabytefunctionstatement
UnsignedcharSPI_ReadByte(void);
//thatreceiveabytefunction
UnsignedcharSD_WriteSingleBlock(unsignedlongsector);
//thatsingleBLOCKdatawriteSDcardfunction
UnsignedcharSD_ReadSingleBlock(unsignedlongsector);
//readSDcarddatafunctionsingleBLOCKstatement
Void(lcd_display);
//stateresultsdisplayfunction
Void(delay);
//statedelayfunction(shownby)
//systeminitializationfunction
Voidspi_init()
{
TRISG=0x00d0;
//settheSDIoutput,Coutputportfortheother
TRISB=0X0000;
//setfortheoutputportB
TRISD=0X0000;
//setfortheoutputportD
SPI2CON=0x0278;
//idlebusishigh,fosc/64
SPI2STAT=0x8000;
//theendofsamplingtheoutputdataofinputdata,therisingedgeofdatatransmission
}
WriteaLCDprogram****************************************//*************************
//writeabyteofdatafunction
Afterchangingthelevel//ontheneedtoinsertadelaytime,otherwisetheLCDreact.
Voidwrite(unsignedcharx)
PORTD=x;
//PORTDporttosenddatatobedisplayed
Delay();
Rs=1;
//thebytedata,ratherthancommand
Rw=0;
//theoperationforwriting,
Insteadofreading
E=0;
//lowenablesignal
(delay);
//keeptheenablesignalislowforaperiodoftime
E=1;
//pullhighenablesignalneededbytherisingedgeoftheLCDoperation
//********************LCDdisplaysettingsfunction**************************************
Voidlcd_enable()
Rs=0;
//thebytedataforthecommand,ratherthanthedata
//theoperationforwriting,insteadofreading
//*********************LCDinitializationfunction**************************************
Voidlcd_init()
PORTD=0X1;
//display
Lcd_enable();
PORTD=0X38;
//8bit2row5*7dotmatrix
PORTD=0X0e;
//display,cursor,flashing
PORTD=0X06;
//thetextdoesnotmovethecursortotheright.
PORTD=0X86;
//timedisplay.
//***********************LCDdisplayfunction************************************
Voidlcd_display()
UnsignedcharI,j;
Lcd_init();
For(i=0;
i<
3;
//i++)atotalof3bytesofdata
Write(lcd[i]);
//look-uptablefordataandcalltowriteabyteofdatatotheLCDdisplayfunction
For(j=0;
j<
5;
//j++)foraperiodoftime(mainlyinordertocontrolthedisplayspeed)
{delay();
//****************writeabyte***************************function
UnsignedcharSPI_WriteByte(unsignedcharVAL)
SPI2BUF=Val;
//tobesenttothetransmittingregisterdataloading
While(IFS1bits.SPI2IF!
);
//waitforsent
IFS1bits.SPI2IF=0;
//cleartosendcompleteflag
ReturnSPI2BUF;
//readreceiveregister(eveninvaliddataisempty)
//****************receiveabyte**************************function
UnsignedcharSPI_ReadByte(void)
SPI2BUF=0xff;
//sendregisterloadingdata,receivingdatatostart
//waitforthecompletionofreceiving
//clearreceivecompleteflag
//readthereceiveddata
//*****************sendcommandfunction****************************
UnsignedcharSD_SendCommand(unsignedcharCMD,unsignedlongARG)
Unsignedcharr1;
Unsignedcharretry1=0;
//thenumberofrepeatoperation
Cs=0;
//thechipselectsignal
SPI_WriteByte(CMD0x40|);
//writecommandrespectively.
SPI_WriteByte(arg>
>
24);
//fourthbytedatasegment
16);
//thirdbytedatasegment
8);
//secondbytedatasegment
SPI_WriteByte(ARG);
//firstbytedatasegment
SPI_WriteByte(0x95);
//CRCchecksum
While((R1=SPI_WriteByte(0xff))==0xff)//waitforaresponse
Ifbreak(retry1++>
200);
//exittimeout
Cs=1;
//earlychipselectsignal
ReturnR1;
//returnstatus
//*******************SDinitializationfunction**************************
Unsignedcharsd_reset()
UnsignedcharI,tmp;
Unsignedcharretry;
//repeat
Unsignedcharr1=0;
Retry=0;
Do
100;
i++)SPI_WriteByte(0xff);
R1=SD_SendCommand(0,0);
//idlecommand
Retry++;
If(retry>
20)return1;
}while(R1!
=0x01);
//waitforthecommandtoreturnIDLE
Retry=0;
R1=SD_SendCommand(1,0);
//Activecommand
254)return1;
}while(R1);
R1=SD_SendCommand(59,0);
//CRC
If(R1return1);
//returnisnotcorrect,exitinitialization
R1=SD_SendCommand(16,512);
//setthesectorsize512
If(R1=0return1!
Return0;
//returntonormal
//********************writeasector**************************
UnsignedcharSD_WriteSingleBlock(unsignedlongsector)
Unsignedinti;
Unsignedcharretry=0;
R1=SD_SendCommand(24,sector<
<
9);
//writecommand
10)return1;
}while(R1==0x00);
SPI_WriteByte(0xff);
SPI_WriteByte(0xFE);
//thestartsymbol
512;
//i++)tosend512bytesofdata
If(i<
255)SPI_WriteByte(I);
//send0--255
ElseSPI_WriteByte(512-i);
//send255--0
//16-bitsCRC
R1=SPI_WriteByte(0xff);
//readaresponse
If(retry++>
Timeout/exit
While(!
((r1&
0x0f)==5));
//waitfordatareceiveinformation
(while!
(SPI_WriteByte(0xff)));
//waitfortheinternalSDcardprogramming
//******************SDcardreadingasector************************
UnsignedcharSD_ReadSingleBlock(unsignedlongsector)
UnsignedcharR1,temp;
Unsignedinti,j;
R1=SD_SendCommand(17,sector<
//readcommand
While(SPI_WriteByte(0xff)!
=0xFE)//waitforreceivingastartbyte
100)return1;
i++)//read512data
Temp=SPI_WriteByte(0xff);
Lcd[0]=(temp/100)+48;
Lcd[1]=((temp%100)/10)+48;
Lcd[2]=((temp%100)%10)+48;
(lcd_display);
//readdatasenttothedisplay
500;
j++){delay();
//pseudo16-bitsCRC
//***********************delayprocedures*************************
Void(delay)/delayprocedure
Inti;
//defineintegervariable
For(i=0x100;
i--;
//delay
Themainfunctionof**************************//************************
Intmain(void)
Unsignedcharloop,res;
Loop=1;
While(loop)
(spi_init);
//callsysteminitializationfunction
Res=(sd_reset);
//calltheSDcardinitializationfunction
If(RESbreak);
//SDcardinitializationisnormal,notnormal,notoutoftheloopexecutesthereadandwriteoperations
SD_WriteSingleBlock
(1);
//callandwriteSDcardsingleBLOCKfunction,thesectoris1
If(RES)break;
SD_ReadSingleBlock
(1);
//callreadSDcardsingleBLOCKfunction,thesectornumberis1
Loop=0;
While
(1);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- sd卡读写程序SD card read and write program sd 读写 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)