在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
芯片精品文章合集(500篇!)    创芯人才网--重磅上线啦!
查看: 2652|回复: 1

[原创] SD卡开发 单片机读写SD卡 模拟SPI总线的子程序

[复制链接]
发表于 2011-9-19 11:05:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
模拟SPI总线的子程序:
sbit CS=P1^0;
sbit CLK= P1^1;
sbit DATaI=P1^2;
sbit DATaO=P1^3;
#define SD_Disable() CS=1 //Disable CS
#define SD_Enable() CS=0 //Enable CS
unsigned char SPI_TransferByte(unsigned char val)
{
  unsigned char BitCounter;
  for(BitCounter=8; BiCounter!=0; BitCounter--)
  {
    CLK=0; DATaI=0; // write
    if(val&0x80) DATaI=1;
    val<<=1; CLK=1;
    if(DATaO)val|=1; // read
  }
  CLK=0;
  return val;
}


初始化SD卡的子程序
unsigned char SD_Init(void)
{
  unsigned char retry,temp;
  unsigned char i;
   for (i=0;i<0x0f;i++)
   { SPI_TransferByte(0xff); //delay }
    SD_Enable(); //Enable Chip select
    SPI_TransferByte(SD_RESET); //send a reset command
    SPI_TransferByte(0x00);
    SPI_TransferByte(0x00);
    SPI_TransferByte(0x00);         
    SPI_TransferByte(0x00);
    SPI_TransferByte(0x95);
    SPI_TransferByte(0xff);
    SPI_TransferByte(0xff);
    retry=0;
    do
    {
        temp=Write_Command_SD(SD_INIT,0); //Send the initialization command
        retry++;
        if(retry==100) //retry 100 times
        {
              SD_Disable(); //disable chip select
              return(INIT_CMD1_ERROR); //If retry fails returns an error number
        }
     }while(temp!=0);
     SD_Disable(); //disable card
     return(TRUE); //return success
}
乐诚科技:www.lcemb.cn
发表于 2011-11-16 17:03:06 | 显示全部楼层
有没有完整的SD.h?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-5 21:14 , Processed in 0.018990 second(s), 11 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表