新聞中心

SD卡讀寫程序

作者: 時(shí)間:2018-09-11 來源:網(wǎng)絡(luò) 收藏

unsigned char sd_reset()
{
unsigned char i,tmp;
unsigned char retry; //重復(fù)次數(shù)
unsigned char r1=0;
retry=0;
delay();
delay();
do
{
for(i=0;i100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(0,0);//發(fā)idle命令
retry++;
if(retry>20) return 1; //超時(shí)退出
} while(r1 != 0x01); //等待IDLE命令返回

本文引用地址:http://2s4d.com/article/201809/388868.htm

retry = 0;
cs=0;
do
{
for(i=0;i100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(1, 0); //發(fā)Active命令
retry++;
if(retry>254) return 1; //超時(shí)退出
} while(r1);

for(i=0;i100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(59, 0); //關(guān)crc
if (r1) return 1; //返回不正確,退出初始化

for(i=0;i100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(16, 512); //設(shè)扇區(qū)大小512
if(r1!=0) return 1; //返回不正確,退出初始化
return 0; //正常返回
}

//********************寫一個(gè)扇區(qū)**************************
unsigned char SD_WriteSingleBlock(unsigned long sector)
{
unsigned char r1;
unsigned int i;
unsigned char retry=0;
do
{
for(i=0;i100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(24, sector9);//寫命令
retry++;
if(retry>10) return 1; //超時(shí)退出
} 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); //發(fā)開始符

for(i=0; i512; i++) //送512字節(jié)數(shù)據(jù)
{
if(i255) SPI_WriteByte(i); //發(fā)送0--255
else SPI_WriteByte(512-i); //發(fā)送255--0

}

SPI_WriteByte(0x95);
SPI_WriteByte(0x95); //16-bits CRC

r1 = SPI_WriteByte(0xff); //讀響應(yīng)位
if(retry++ >10) return 1; //超時(shí)退出
while(!((r10x0f)==5)); //等待數(shù)據(jù)成功接受返回信息
while(!(SPI_WriteByte(0xff))); //等待SD卡內(nèi)部編程完成

return 0;
}

//******************讀SD卡一個(gè)扇區(qū)************************
unsigned char SD_ReadSingleBlock(unsigned long sector)
{
unsigned char r1,temp;
unsigned int i,j;
unsigned char retry=0;

do
{
r1 = SD_SendCommand(17, sector9);//讀命令
retry++;
if(retry>10) return 1; //超時(shí)退出
} while(r1 != 0x00);
cs=0;
while(SPI_WriteByte(0xff)!= 0xfe) //等待接收到開始字節(jié)
{
if(retry++ >100) return 1; //超時(shí)退出
}
for(i=0; i512; i++) //讀512個(gè)數(shù)據(jù)
{
temp = SPI_WriteByte(0xff); //讀取接收到的數(shù)據(jù)
lcd[0]=(temp/100)+48;
lcd[1]=((temp%100)/10)+48;
lcd[2]=((temp%100)%10)+48;
lcd_display(); //讀取數(shù)據(jù)送顯示
for(j=0;j500;j++) {delay();}
}

SPI_WriteByte(0xff); //偽16-bits crc
SPI_WriteByte(0xff);

cs=1;

return 0;
}

//***********************延時(shí)*************************
void delay() //延時(shí)
{
int i; //定義整形變量
for(i=0x100;i--;); //延時(shí)
}

//************************主函數(shù)**************************
int main(void)
{
unsigned char loop,res;
delay();
delay();
delay();
loop=1;

cs=1;
while(loop)
{
spi_init(); //調(diào)用系統(tǒng)初始化函數(shù)
res= sd_reset(); //調(diào)用SD卡初始化函數(shù)
if(res) break; //SD卡初始化是否正常,不正常,退出循環(huán),不執(zhí)行下面的讀寫操作
SD_WriteSingleBlock(1); //調(diào)用寫SD卡單BLOCK函數(shù),其中扇區(qū)號(hào)為1
if(res) break;
SD_ReadSingleBlock(1); //調(diào)用讀SD卡單BLOCK函數(shù),其中扇區(qū)號(hào)為1
if(res) break;
loop=0;
while(1);
}
while(1);
}


上一頁 1 2 下一頁

關(guān)鍵詞: SD卡讀寫 程序

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉