打印

51读SD卡复位成功,初始化不成功,请求大家帮忙。

[复制链接]
2312|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yjhdddf|  楼主 | 2011-12-15 10:13 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 yjhdddf 于 2011-12-15 10:19 编辑

51读SD卡复位成功,初始化不成功,请求大家帮忙。

#include "STC12C5A32S2.H"
#include "string.h"
sbit SD_CS =P2^0; //SD卡片选    输入
sbit SD_DI =P2^1; //SD卡同步数据  输入
sbit SD_CLK=P2^2; //SD卡同步时钟  输入
sbit SD_DO =P2^3; //SD卡同步数据  输出
//函数声明
void delay(unsigned int time); //延时函数
void senddata(char dat);
//延时函数
void delay(unsigned int time)
{
while(time--);
}
//串口发送一个字节
void senddata(char dat)   //send a data
{  
   ES = 0;
   SBUF = dat;
   while(!TI);
   TI = 0;
   ES = 1;  
}
//串口初始化
void serialinit() //
{
   SCON = 0x50;
   BRT = 0xfa;
   AUXR = 0x11;
   AUXR1 = 0x80;
   ES = 1;    //Pemit serial interrupt
   EA = 1;   //Pemit all interrupt
}
//串口中断函数
void sio_int() interrupt 4
{
ES=0;
ES=1;
}
//往SD卡定一字节 www.mcusky.com
void SdWrite(unsigned char  n)
{
   
  unsigned char i;
  for(i=8;i;i--)
   {
    SD_CLK=0;
    SD_DI=(n&0x80);
    n<<=1;
    SD_CLK=1;
   }
  SD_DI=1;      
}   
//================================================================
//从SD卡读一字节 www.mcusky.com
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i;i--)
  {
   SD_CLK=0;
   SD_CLK=1;
   n<<=1;
   if(SD_DO) n|=1;
  }
return n;
}
//================================================================
//读SD卡响应 www.mcusky.com
unsigned char SdResponse()
{
  unsigned char i=0,response;
  
  while(i<=8)
  {
    response=SdRead();
    if(response==0x00)
      break;
    if(response==0x01)
      break;
    i++;
  }
  return response;
}  
//================================================================
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);
}
//================================================================
unsigned char SdInit(void)
{
  int delay=0, trials=0;
  unsigned char i;
  unsigned char response;
  SD_CS=1;
  for(i=0;i<=9;i++)
    SdWrite(0xff);
  SD_CS=0;
  senddata(0);
  //Send Command 0 to put MMC in SPI mode
  SdCommand(0x00,0,0x95);
  
  response=SdResponse();
  senddata(response);
  if(response!=0x01)
   {
    return 0;
   }
  
  while(response==0x01)
  {
    SD_CS=1;
    SdWrite(0xff);
    SD_CS=0;
    SdCommand(0x01,0x00ffc000,0xff);
    response=SdResponse();
    senddata(response);
  }
   
  SD_CS=1;
  SdWrite(0xff);
  return 1;  
}

void main()
{
   int i;
   i=1;
   serialinit();
   i=SdInit(); //SD卡初始化
   senddata(i);
   
   //for(j=0;j<512;j++)  pbuf[j]=0;
   //SD_Read_Sector(0x00000000,pbuf);
   /* for(j=0;j<512;j++)
{
     senddata(pbuf[j]);
}  */
while(1)
{

}
   
}

相关帖子

沙发
yjhdddf|  楼主 | 2011-12-23 15:56 | 只看该作者
问题已经解决  SD卡与SDHC卡 初始化不同。

使用特权

评论回复
板凳
wukunshan| | 2011-12-23 17:06 | 只看该作者
:o

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

19

帖子

1

粉丝