找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3309|回復(fù): 3
收起左側(cè)

菜鳥請(qǐng)高手幫忙看看SD卡模擬SPI通信那出錯(cuò)了

[復(fù)制鏈接]
ID:59815 發(fā)表于 2014-5-8 22:24 | 顯示全部樓層 |閱讀模式
//定義SD卡需要的4根信號(hào)線
sbit SD_CLK = P2^1;
sbit SD_DI  = P2^2;
sbit SD_DO  = P2^0;
sbit SD_CS  = P2^3;
sbit p0  = P0^0;
sbit p1  = P0^1;
sbit p2  = P0^2;
sbit p3  = P0^3;
//===========================================================
unsigned char man=0;
//===========================================================
//定義512字節(jié)緩沖區(qū),注意需要使用 xdata關(guān)鍵字
unsigned char xdata DATA[512];
void Delay(unsigned int i)
{
    while(i--);
}
//寫一字節(jié)到SD卡,模擬SPI總線方式
void SdWrite(unsigned char n)
{
unsigned char i;
for(i=8;i!= 0;i--)
{
SD_DI=(n&0x80);
n<<=1;
SD_CLK=0;
if(man==1)
Delay(4);
SD_CLK=1;
if(man==1)
Delay(4);

}

}
//===========================================================
//從SD卡讀一字節(jié),模擬SPI總線方式
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i!= 0;i--)
{
n<<=1;
SD_CLK=0;
if(SD_DO) n|=1;
if(man==1)
Delay(4);
SD_CLK=1;
if(man==1)
Delay(4);

}
return n;
}
//============================================================
//檢測SD卡的響應(yīng)
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i<=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//================================================================
//發(fā)命令到SD卡
void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
{
SdWrite(command|0x40);
SdWrite(((unsigned char *)&argument)[0]);
SdWrite(((unsigned char *)&argument)[1]);
SdWrite(((unsigned char *)&argument)[2]);
SdWrite(((unsigned char *)&argument)[3]);
SdWrite(CRC);
}
//================================================================
//初始化SD卡
unsigned char SdInit(void)
{
unsigned char time,temp,i;
SD_CS=1;
for(i=0;i<=9;i++)
SdWrite(0xff);
SD_CS=0;
time=0;
temp=10;
man=1;
do
{
SdCommand(0x00,0,0x95);
temp=SdResponse();
time++;
if(time>200)
p3=0;//CMD0沒過
}
while(temp!=0x01);
if(temp==0x01)
p0=0;//CMD0通過
time=0;
SD_CS=1;
SdWrite(0xff);
SD_CS=0;
do
{
SdCommand(0x01,0x00ffc000,0xff);
temp=SdResponse();
if(temp==0)
{p1=0;;//CMD1通過
return 0;}
time++;
}
while(time<200);
if(time>180)
p2=0;//CMD1沒過

else
p2=1;
man=0;
return 0;
}
//================================================================
//往SD卡指定地址寫數(shù)據(jù),一次最多512字節(jié)
unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
{
unsigned int count;
unsigned char dataResp;
//Block size is 512 bytes exactly
//First Lower SS
SD_CS=0;
//Then send write command
SdCommand(0x18,address,0xff);
if(SdResponse()==00)
{
SdWrite(0xff);
SdWrite(0xff);
SdWrite(0xff);
//command was a success - now send data
//start with DATA TOKEN = 0xFE
SdWrite(0xfe);
//now send data
for(count=0;count<len;count++) SdWrite(*Block++);
for(;count<512;count++) SdWrite(0);
//data block sent - now send checksum
SdWrite(0xff); //兩字節(jié)CRC校驗(yàn), 為0XFFFF 表示不考慮CRC
SdWrite(0xff);
//Now read in the DATA RESPONSE token
dataResp=SdRead();
//Following the DATA RESPONSE token
//are a number of BUSY bytes
//a zero byte indicates the MMC is busy
while(SdRead()==0);
dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
SD_CS=1;
SdWrite(0xff);
if(dataResp==0x0b)
{
//printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR\n");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token.\n");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC.\n");
return 0;
}
//=======================================================================
//從SD卡指定地址讀取數(shù)據(jù),一次最多512字節(jié)
unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
//printf("MMC_read_block\n");
SD_CS=0;
//Then send write command
SdCommand(0x11,address,0xff);
if(SdResponse()==00)
{
//command was a success - now send data
//start with DATA TOKEN = 0xFE
while(SdRead()!=0xfe);
for(count=0;count<len;count++) *Block++=SdRead();
for(;count<512;count++) SdRead();
//data block sent - now send checksum
SdRead();
SdRead();
//Now read in the DATA RESPONSE token
SD_CS=1;
SdRead();
return 1;
}
//printf("Command 0x11 (Read) was not received by the MMC.\n");
return 0;
}

回復(fù)

使用道具 舉報(bào)

ID:44262 發(fā)表于 2014-7-17 17:33 來自手機(jī) | 顯示全部樓層
陳旭現(xiàn)在是什么問題是不能編譯還是不能實(shí)現(xiàn) 需要的功能
回復(fù)

使用道具 舉報(bào)

ID:62989 發(fā)表于 2014-7-17 19:22 | 顯示全部樓層
我也搞了好久,按照鎮(zhèn)南那個(gè)搞的,什么都一模一樣就是不行!郁悶的要死
回復(fù)

使用道具 舉報(bào)

ID:64910 發(fā)表于 2014-8-10 07:35 | 顯示全部樓層
一樣的難題
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表