|
目前初始化以及扇区读写都成功。。。现在贴出代码造福需要的人。。。。本人亲测通过 - void SD_SendByte(u8 Byte) //向SD写入一个字节,模拟SPI
- {
- u8 i,temp;
- temp=Byte;
- CS=0;
- for(i=0;i<8;i++)
- {
- CLK=0;
- if(temp&0x80) //从高位写入
- MOSI=1;
- else
- MOSI=0;
- temp<<=1;
- CLK=1;
- }
- }
- u8 SD_ReceiveByte(void) //从SD读出一个字节
- {
- u8 Byte,i;
- CS=0;
- for(i=0;i<8;i++)
- {
- Byte<<=1;
- CLK=0;
- if(MISO==1)
- Byte|=0x01;
- else
- Byte&=0xfe;
- CLK=1;
- }
- return Byte;
- }
- u8 SD_WaitReady() //等待SD卡准备好,返回1:失败,0:OK
- {
- u8 i,temp;
- for(i=0;i<200;i++)
- {
- temp=SD_ReceiveByte();
- if(temp==0xff)
- return 0;
- }
- return 1;
- }
- u8 SD_SendCommand(u8 Command,u32 arg,u8 CRC) //返回1:失败,其他:成功
- {
- u8 response,i=200;
- while(SD_WaitReady()) //先等待SD卡准备好
- CS=1;
- SD_SendByte(0xff); //发送8个同步时钟
- CS=0;
- SD_SendByte(Command|0x40); //开始发送命令
- SD_SendByte((arg&0xff000000)>>24);
- SD_SendByte((arg&0x00ff0000)>>16);
- SD_SendByte((arg&0x0000ff00)>>8);
- SD_SendByte((arg&0x000000ff)>>0);
- SD_SendByte(CRC);
- i=200;
- do
- {
- response=SD_ReceiveByte();
- i--;
- if(i==0)
- return 1; //获得应答失败或者超时
- }while(response&0x80);
- CS=1;
- SD_SendByte(0xff); //8个同步时钟
- return response;
-
- }
- u8 SD_Init() //SD卡初始化 返回0:成功 其他:失败或超时
- {
- u8 response,i=0;
- delay1(100); //等待SD卡电压升高至工作电压
- while(SD_WaitReady()); //等待SD卡准备就绪
- CS=0;
- do
- {
- response=SD_SendCommand(CMD0,0,0x95); //CMD0复位
- i++;
- if(i==200)
- return 0xff;
- }while(response!=0x01);
- i=0;
- do
- {
- response=SD_SendCommand(CMD8,0x01aa,0x87); //判断SD卡版本
- i++;
- if(i==200)
- return 0xff;
- }while(response!=0x01); //version 2.0
- i=0;
- do
- {
- SD_SendCommand(CMD55,0x00,0x01);
- response=SD_SendCommand(CMD41,0x0040,0x01);
- i++;
- if(i==200)
- return 0xff;
- }while(response!=0); //判断初始化是否完成
- i=0;
- do
- {
- response=SD_SendCommand(CMD16,0x0200,0x01); //设置扇区大小为512字节
- i++;
- if(i==200)
- return 0xff;
- }while(response!=0);
- CS=1;
- SD_SendByte(0xff);
- return 0;
-
- }
- u8 SD_ReadSingleSector(u8 *buff,u32 sector) //SD读取单个扇区,返回0:成功,其他:失败
- {
- u8 response,i=0;
- u16 cnt;
- while(SD_Init()); //先初始化SD卡
- while(SD_WaitReady());
- CS=0;
- sector*=512;
- do
- {
- response=SD_SendCommand(CMD17,sector,0x01);
- i++;
- if(i==200)
- return 1;
- }while(response!=0);
- while(SD_WaitReady());
- for(i=0;i<200;i++)
- {
- response=SD_ReceiveByte();
- if(response==0xfe)
- break;
- }
- if(response!=0xfe)
- {
- return 1;
- }
- for(cnt=0;cnt<512;cnt++)
- {
- *buff++=SD_ReceiveByte();
- }
- SD_ReceiveByte();
- SD_ReceiveByte();
- CS=1;
- SD_SendByte(0xff);
- return 0;
- }
- u8 SD_WriteSingleSector(u8 *buff,u32 sector) //SD卡写单个扇区,返回0:成功,1:失败
- {
- u8 response,i;
- u16 cnt;
- if(SD_Init()) //先初始化SD卡
- return 1;
- while(SD_WaitReady());
- CS=0;
- sector*=512;
- do
- {
- response=SD_SendCommand(CMD24,sector,0x01);
- i++;
- if(i==200)
- return 1;
- }while(response!=0);
- for(i=0;i<10;i++)
- {
- SD_SendByte(0xff);
- }
- SD_SendByte(0xfe);
- for(cnt=0;cnt<512;cnt++)
- {
- SD_SendByte(*buff++);
- }
- SD_SendByte(0xff);
- SD_SendByte(0xff);
- while(((SD_ReceiveByte()&0x1f)!=0x05)&&i<=200)
- {
- i++;
- }
- if(i==200)
- return 1;
- else
- {
- SD_WaitReady();
- CS=1;
- SD_SendByte(0xff);
- return 0;
- }
-
- }
复制代码 |
|