打印

我用51模拟SPI读写SD卡,现在就块写不成功。请帮忙!

[复制链接]
2005|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
l0p0c|  楼主 | 2008-2-22 11:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我的块写完全按照图上的时序和要求做的,但是就是写不成功!请求帮助!

/*=========================================================
函数名:SD_WritBlock()
功能:SD卡块写函数
入口参数:
出口参数:
=========================================================*/
Uchar SD_WriteBlock(Ulong Addrs)
{
    Uchar ret,Ad0,Ad1,Ad2,Ad3,i;                            //临时变量
    Ad0=((Addrs&0xFF000000)>>24);                            //
    Ad1=((Addrs&0x00FF0000)>>16);                            //
    Ad2=((Addrs&0x0000FF00)>>8);                            //
    Ad3=(Addrs&0x000000FF);                                    //
    SD_EN();                                                //SD卡使能
    i=0;                                                    //初始化变量
    do
    {
        ret=SD_SendCmd(CMD24,Ad0,Ad1,Ad2,Ad3,CMD24_R);        //发送块读指令
        i++;                                                //变量+1
    }while((ret!=0x00)&&(i<10));                            //设置循环条件
    if(i>=10)
    {
        SD_UN();                                            //释放SD卡
        return SD_ERR_CMD_WRITEBLOCK;                        //返回块写错误
    }
    i=0;                                                    //初始化寄存器
    do
    {
        SD_QWriteByte(0xFF);                                //发送时钟
        i++;                                                //时间限制
    }while(i<10);                                            //设置循环条件
    SD_QWriteByte(0xFE);                                    //写入起始字节
    for(i=0;i<32;i++)
    {
        D=0xCC;
    }
    for(i=0;i<18;i++)
    {
        SD_QWriteByte(D);                                //读取SD卡指定地址数据
    }
    ret=SD_QReadByte();                                        //忽略一个字节xxx00101b
    if((ret&0x1F)!=0x05)
    {
        SD_UN();                                            //释放SD卡
        return SD_ERR_WAIT_WRITEBLOCK;                        //返回等待错误信号
    }
    i=0;                                                    //初始化变量
    do
    {
        ret=SD_QReadByte();                                    //读SD卡状态
        i++;                                                //变量+1
    }while((ret==0x00)&&(i<10));                            //设置循环条件
    if(i>=10)
    {
        SD_UN();                                            //释放SD卡
        return SD_ERR_DATA_WRITEBLOCK;                        //返回块写错误
    }
    SD_UN();                                                //释放SD卡
    SD_QWriteByte(0xFF);                                    //最后8个CLOCK
    return SD_ERR_NO;                                        //否则返回无错误
}

相关帖子

沙发
zhxin999| | 2008-2-22 16:54 | 只看该作者

参考ZLG的代码

参考ZLG的spi读写sd卡的代码吧,里面写得很详细的。。

使用特权

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

本版积分规则

73

主题

702

帖子

1

粉丝