sd卡读写程序SDcardreadandwriteprogram.docx

上传人:b****5 文档编号:29496241 上传时间:2023-07-24 格式:DOCX 页数:11 大小:23.73KB
下载 相关 举报
sd卡读写程序SDcardreadandwriteprogram.docx_第1页
第1页 / 共11页
sd卡读写程序SDcardreadandwriteprogram.docx_第2页
第2页 / 共11页
sd卡读写程序SDcardreadandwriteprogram.docx_第3页
第3页 / 共11页
sd卡读写程序SDcardreadandwriteprogram.docx_第4页
第4页 / 共11页
sd卡读写程序SDcardreadandwriteprogram.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

sd卡读写程序SDcardreadandwriteprogram.docx

《sd卡读写程序SDcardreadandwriteprogram.docx》由会员分享,可在线阅读,更多相关《sd卡读写程序SDcardreadandwriteprogram.docx(11页珍藏版)》请在冰豆网上搜索。

sd卡读写程序SDcardreadandwriteprogram.docx

sd卡读写程序SDcardreadandwriteprogram

sd卡读写程序(SDcardreadandwriteprogram)

SDcardreadandwriteprogram

Objective:

TostudytheSDcard//operation

Design/software

1,usingSPIcommunication//SDcard

2,gotoSD//inorderto0-255atotalof256data,andthenreadbackLCD1602display

//hardwarerequirements:

S11ON//dialswitch

JumperJ18//allconnected

#include//dsPIC30F6014standardheaderfile

_FOSC(CSW_FSCM_O&FFXT_PLL4);//4doublercrystaloscillator,Failsafeclockclosed

_FWDT(WDT_OFF);//closethewatchdogtimer

_FBORPO(RPBOR_OFF&MCLR_EN);//resetprohibitedMCLRresetenable.

_FGS(CODE_PROT_OFF);//codeprotectionagainst#defineCSPORTGbits.RG9//SDcardselectionpindefinition#defineRSLATBbits.LATB4//definitionLCDcontrolbits(noteherecanonlyregisterwithLATB,youcannotdirectlyusethePORTBregister)

#defineRWLATBbits.LATB5

#defineeLATBbits.LATB6

Unsignedchar__attribute__((address(0x900)))lcd[3]={0,0,0};

Void(spi_init);//declarationsysteminitialfunction

Void(spi_low);//thatproduceslowbaudrate(usingtheSDcardinitializationfunction)

Void(spi_high);//thatproducehighbaudratefunction(SD

cardinitializationafteruse)

Unsignedchar(sd_reset);//thattheSDcardinitializationfunction

UnsignedcharSD_SendComman(udnsignedcharCMDunsigned,long

ARG);//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

//writeabyteofdatafunction

Afterchangingthelevel//ontheneedtoinsertadelaytime,otherwisetheLCDreact.

Voidwrite(unsignedcharx)

{

PORTD=x;//PORTDporttosenddatatobedisplayed

Delay();

Rs=1;//thebytedata,ratherthancommand

Delay();

Rw=0;//theoperationforwriting,

Insteadofreading

Delay();

E=0;//lowenablesignal

(delay);//keeptheenablesignalislowforaperiodoftime

Delay();

}

//********************LCDdisplaysettingsfunction

Afterchangingthelevel//ontheneedtoinsertadelaytime,otherwisetheLCDreact.

Voidlcd_enable()

{

Delay();

Rs=0;//thebytedataforthecommand,ratherthanthedata

Delay();

Rw=0;//theoperationforwriting,insteadofreading

Delay();

E=0;//lowenablesignal

(delay);//keeptheenablesignalislowforaperiodoftime

Delay();

}

//*********************LCDinitializationfunction

Voidlcd_init()

{

PORTD=0X1;//display

Lcd_enable();

PORTD=0X38;//8bit2row5*7dotmatrix

Lcd_enable();

PORTD=0X0e;//display,cursor,flashing

Lcd_enable();

PORTD=0X06/;/thetextdoesnotmovethecursortotheright.

Lcd_enable();

PORTD=0X86;//timedisplay.

Lcd_enable();

//***********************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)

{

data

SPI2BUF=Val;//tobesenttothetransmittingregisterloading

While(IFS1bits.SPI2IF!

);//waitforsent

IFS1bits.SPI2IF=0;//cleartosendcompleteflag

ReturnSPI2BUF;//readreceiveregister(eveninvaliddataisempty)

}

function

UnsignedcharSPI_ReadByte(void)

{

data

SPI2BUF=0xff;//sendregisterloadingdata,receivingtostart

While(IFS1bits.SPI2IF!

);//waitforthecompletionofreceiving

IFS1bits.SPI2IF=0;//clearreceivecompleteflag

ReturnSPI2BUF;//readthereceiveddata

}

UnsignedcharSD_SendComman(udnsignedcharCMD,unsignedlongARG)

{

Unsignedcharr1;

Unsignedcharretry1=0;//thenumberofrepeatoperation

Cs=0;//thechipselectsignal

SPI_WriteByte(CMD0x40|);//writecommandrespectively.

SPI_WriteByte(arg>>24);//fourthbytedatasegment

SPI_WriteByte(arg>>16);//thirdbytedatasegment

SPI_WriteByte(arg>>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;

Delay();

Delay();

Do

{

For(i=0;i<100;i++)SPI_WriteByte(0xff);

R1=SD_SendCommand(0,0);//idlecommand

Retry++;

If(retry>20)return1;//exittimeout

}while(R1!

=0x01);//waitforthecommandtoreturnIDLE

Retry=0;

Cs=0;

Do

{

For(i=0;i<100;i++)SPI_WriteByte(0xff);

R1=SD_SendCommand(1,0);//Activecommand

Retry++;

If(retry>254)return1;//exittimeout}while(R1);

For(i=0;i<100;i++)SPI_WriteByte(0xff);

R1=SD_SendCommand(59,0);//CRC

If(R1return1);//returnisnotcorrect,exitinitialization

For(i=0;i<100;i++)SPI_WriteByte(0xff);

R1=SD_SendCommand(16,512);//setthesectorsize512

If(R1=0return1!

);//returnisnotcorrect,exitinitialization

Return0;//returntonormal

}

writeasector

UnsignedcharSD_WriteSingleBlock(unsignedlongsector){

Unsignedcharr1;

Unsignedinti;

Unsignedcharretry=0;

Do

{

For(i=0;i<100;i++)SPI_WriteByte(0xff);

R1=SD_SendCommand(24,sector<<9);//writecommand

Retry++;

If(retry>10)return1;//exittimeout

}while(R1==0x00);

Cs=0;

SPI_WriteByte(0xff);

SPI_WriteByte(0xff);

SPI_WriteByte(0xff);

SPI_WriteByte(0xff);

SPI_WriteByte(0xff);

SPI_WriteByte(0xff);

SPI_WriteByte(0xFE);//thestartsymbol

For(i=0;i<512;//i++)tosend512bytesofdata

If(i<255)SPI_WriteByte(I);//send0--255

ElseSPI_WriteByte(512-i);//send255--0

}

SPI_WriteByte(0x95);

SPI_WriteByte(0x95);//16-bitsCRC

R1=SPI_WriteByte(0xff);//readaresponse

If(retry++>10)return1;

Timeout/exit

information

While(!

((r1&0x0f)==5));//waitfordatareceive

(while!

(SPI_WriteByte(0xff)));//waitfortheinternalSDcardprogramming

Return0;

}

//******************SDcardreadingasector

UnsignedcharSD_ReadSingleBlock(unsignedlongsector)

{

UnsignedcharR1,temp;

Unsignedinti,j;

Unsignedcharretry=0;

Do

{

R1=SD_SendCommand(17,sector<<9);//readcommandRetry++;

If(retry>10)return1;//exittimeout

}while(R1==0x00);

Cs=0;

While(SPI_WriteByte(0xff)!

=0xFE)//waitforreceivingastartbyte

{

If(retry++>100)return1;//exittimeout

For(i=0;i<512;i++)//read512data

{

Temp=SPI_WriteByte(0xff);//readthereceiveddata

Lcd[0]=(temp/100)+48;

Lcd[1]=((temp%100)/10)+48;

Lcd[2]=((temp%100)%10)+48;

(lcd_display);//readdatasenttothedisplay

For(j=0;j<500;j++){delay();}

}

SPI_WriteByte(0xff);//pseudo16-bitsCRC

SPI_WriteByte(0xff);

Cs=1;

}

Return0;

delayprocedures

Void(delay)/delayprocedure

{

Inti;//defineintegervariable

For(i=0x100;i--;);//delay

}

Themainfunctionof

Intmain(void)

{

Unsignedcharloop,res;

Delay();

Delay();

Delay();

Loop=1;

Cs=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);//callreadSDcardsingleBLOCK

function,thesectornumberis1

If(RES)break;

Loop=0;

While

(1);

}

While

(1);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 调查报告

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1