|
网上都说,sd卡的一个扇区是512B,但我通过读csd寄存器,为1024B,是错的吗?
我有一个sd卡的写扇区的程序,但应答信息总为出错,程序如下:
uchar write_sd_sector(unsigned long addr,uchar *Buffer)
{
uchar reply,retry=0;
uint i;
uchar CMD[]={0x58,0x00,0x00,0x00,0x00,0xFF};
addr=addr<<10; //addr = addr * 512
CMD[1] = ((addr & 0xFF000000) >>24 );
CMD[2] = ((addr & 0x00FF0000) >>16 );
CMD[3] = ((addr & 0x0000FF00) >>8 );
retry=0;
do
{
reply=write_cmd(CMD); //为了可靠写入,写100次
retry++;
if(retry==100)
{
return(reply); //send commamd Error!
}
}
while(reply!=0);
for(i=0;i<20;i++) //在写之前先产生20个时钟信号
{
read_byte();
}
write_byte(0xFE);
for (i=0;i<1024;i++) //现在可以写入1024个字节
{
write_byte(*Buffer++);
}
write_byte(0xFF); //Dummy CRC
write_byte(0xFF); //CRC Code
write_byte(0xFF);
reply=read_byte(); //read response
if((reply&0x1F)!=0x05) // 写入的1024个字节是未被接受
{
dis_sd();
while(read_byte()!=0xff){};
return(reply); //Error!
}
while(read_byte()!=0xff){};
dis_sd(); //
return(3); //写入成功
}
void write_byte(uchar data)
{
TXBUF_0=data;
while(!(IFG1&UTXIFG0));
}
uchar read_byte(void)
{
uchar data=0xff;
TXBUF_0=0xff;
while(!(IFG1&URXIFG0));
data=RXBUF_0;
return(data);
}
uchar write_cmd(uchar *cmd)
{
uchar data=0xff,retry=0;
dis_sd();
write_byte(0xff);
en_sd();
for(uchar i=0;i<6;i++)
write_byte(cmd);
// data=read_byte();
do
{
data=read_byte();
if(retry++>=200)
break;
}
while(data==0xff);
return data;
}
请高手指点一下。
|
|