基于stc12c5a60s2的mp3,sd卡一直不能初始化问题
时间:10-02
整理:3721RD
点击:
想模仿这个教程做个mp3http://www.geekfans.com/article-4670-1.html
可是sd卡一直都初始化不了,求助一下大家,都是自学的
//***********模拟spi写函数
void SPI_W(unsigned char Data){
unsigned char i;
for(i = 0; i<8; i++){
Data <<= 1;
CLK = 0;
DI = CY;
CLK = 1;
};
DI = 1;
}
//***********spi读函数
unsigned char SPI_R(){
unsigned char Data,i;
DO = 1;//设置DO接口为输入状态
for(i = 0; i<8; i++){
Data <<= 1;
CLK = 0;
CLK = 1;
Data |= DO;
};
return Data;
}
//**************读sd卡回应
unsigned char SD_Response(){
unsigned char i,Response;
for(i = 0; i<10; i++){
Response = SPI_R();
if(Response == 0x00)
break;
if(Response == 0x01)
break;
};
return Response;
}
//***************向SD发命令
void SD_Cmd(unsigned char Cmd, unsigned long Argument, unsigned char CRC){
unsigned char arg[4];
arg[0] = (unsigned char)Argument;
arg[1] = (unsigned char)(Argument >> 8);
arg[2] = (unsigned char)(Argument >> 16);
arg[3] = (unsigned char)(Argument >> 24);
SPI_W(Cmd | 0x40);
SPI_W(arg[3]);
SPI_W(arg[2]);
SPI_W(arg[1]);
SPI_W(arg[0]);
SPI_W(CRC);
}
//*************SD卡初始化
unsigned char SD_Init(){
unsigned int delay = 0;
unsigned char i;
unsigned char Response = 0xff;
CS = 1;
for(i = 0; i<10; i++){
SPI_W(0xff);//上电后给74个以上的时间脉冲
};
CS = 0;
SD_Cmd(0x00, 0, 0x95);//命令CMD0,复位SD卡
//等待复位成功
i = 0;
while(SD_Response() != 0x01){//等待SD卡回应信号
i++;
if(i > 100){
return 0;//失败返回0
};
};
CS = 1;
SPI_W(0xff);//关片选后写8个空脉冲,SD卡复位完毕
//设置SPI
i = 0;
CS = 0;
while(Response != 0x00){//循环等待成功回应,若成功,回应信号为0x00
SD_Cmd(0x01, 0, 0xff);//CMD1,将SD卡设置为SPI模式,无需CRC校验,填入0xff
Response = SD_Response();
if(i > 100){
return 0;//尝试100次,失败返回0
};
};
CS = 1;
SPI_W(0xff);//给8个空脉冲
return 1;
}
//****************从sd卡读数据块
void SD_Block_R(unsigned char* block, unsigned long address){
unsigned int i;
CS = 0;
SD_Cmd(0x11, address, 0xff);//CMD11,数据块读写命令,
while(SD_Response()!=0x00);//循环等待命令回应0x00
while(SPI_R() != 0xfe); //0xfe为块读出的头, 后面紧跟512字节的数据块+2字节的CRC
for(i=0; i<512 ; i++){
block = SPI_R();//读数据
};
SPI_R();
SPI_R();//两个字节的CRC。舍弃
CS =1;
SPI_R();//8个空脉冲
}
发cmd0复位后,一直没有收到01回应,在此求助一下大家
可是sd卡一直都初始化不了,求助一下大家,都是自学的
//***********模拟spi写函数
void SPI_W(unsigned char Data){
unsigned char i;
for(i = 0; i<8; i++){
Data <<= 1;
CLK = 0;
DI = CY;
CLK = 1;
};
DI = 1;
}
//***********spi读函数
unsigned char SPI_R(){
unsigned char Data,i;
DO = 1;//设置DO接口为输入状态
for(i = 0; i<8; i++){
Data <<= 1;
CLK = 0;
CLK = 1;
Data |= DO;
};
return Data;
}
//**************读sd卡回应
unsigned char SD_Response(){
unsigned char i,Response;
for(i = 0; i<10; i++){
Response = SPI_R();
if(Response == 0x00)
break;
if(Response == 0x01)
break;
};
return Response;
}
//***************向SD发命令
void SD_Cmd(unsigned char Cmd, unsigned long Argument, unsigned char CRC){
unsigned char arg[4];
arg[0] = (unsigned char)Argument;
arg[1] = (unsigned char)(Argument >> 8);
arg[2] = (unsigned char)(Argument >> 16);
arg[3] = (unsigned char)(Argument >> 24);
SPI_W(Cmd | 0x40);
SPI_W(arg[3]);
SPI_W(arg[2]);
SPI_W(arg[1]);
SPI_W(arg[0]);
SPI_W(CRC);
}
//*************SD卡初始化
unsigned char SD_Init(){
unsigned int delay = 0;
unsigned char i;
unsigned char Response = 0xff;
CS = 1;
for(i = 0; i<10; i++){
SPI_W(0xff);//上电后给74个以上的时间脉冲
};
CS = 0;
SD_Cmd(0x00, 0, 0x95);//命令CMD0,复位SD卡
//等待复位成功
i = 0;
while(SD_Response() != 0x01){//等待SD卡回应信号
i++;
if(i > 100){
return 0;//失败返回0
};
};
CS = 1;
SPI_W(0xff);//关片选后写8个空脉冲,SD卡复位完毕
//设置SPI
i = 0;
CS = 0;
while(Response != 0x00){//循环等待成功回应,若成功,回应信号为0x00
SD_Cmd(0x01, 0, 0xff);//CMD1,将SD卡设置为SPI模式,无需CRC校验,填入0xff
Response = SD_Response();
if(i > 100){
return 0;//尝试100次,失败返回0
};
};
CS = 1;
SPI_W(0xff);//给8个空脉冲
return 1;
}
//****************从sd卡读数据块
void SD_Block_R(unsigned char* block, unsigned long address){
unsigned int i;
CS = 0;
SD_Cmd(0x11, address, 0xff);//CMD11,数据块读写命令,
while(SD_Response()!=0x00);//循环等待命令回应0x00
while(SPI_R() != 0xfe); //0xfe为块读出的头, 后面紧跟512字节的数据块+2字节的CRC
for(i=0; i<512 ; i++){
block = SPI_R();//读数据
};
SPI_R();
SPI_R();//两个字节的CRC。舍弃
CS =1;
SPI_R();//8个空脉冲
}
发cmd0复位后,一直没有收到01回应,在此求助一下大家
是不是写入速度太高了 具体怎么调呢 谢谢大家了
//等待复位成功
i = 0;
while(SD_Response() != 0x01){//等待SD卡回应信号
i++;
if(i > 100){
return 0;//失败返回0
};
};
执行完这一句就返回0 导致后面读取不成功 实在不懂为什么求助各位大神
求助大家