我用的是STC8H单片机,用IO模拟SPI的方式读TF卡,用小容量(512M)读是对的,同样的程序,但是换用8G卡的时候,读出来的是乱码,三张8G卡0x00地址读出都是一样的。所有TF卡都是FAT32 4096字节格式化的。2张8g卡是class10,1张是class6.
比如:
单片机读8G TF卡的0x00地址:
3C ED FA 5B 95 DD 72 5B 3E E7 05 C6 E5 B9 B7 8F
58 4F 7B 2C 21 63 F5 CD 42 43 8F E8 8A 0D F1 5F
B6 C7 2B 2F 71 83 34 3A C0 33 9D 7A 50 1E FB 4A
37 7F 27 DB F1 C7 C0 27 78 97 01 EB 78 9E 62 71
B3 3A 33 0E 06 09 49 AF 6E 8A 8A A1 5D A0 6D 17
96 D7 5B 48 B0 85 76 D0 22 0A 4F D7 5C 9F 16 19
AA D2 60 4F A4 9E 7C 53 3B 1C 53 FE 0C E9 A0 52
F7 C9 3C 3F 1F 34 7C CC D3 1F A9 F7 E8 81 7D 47
B5 22 9B 55 96 9E AD 66 5F 3A ED 57 45 13 61 08
98 F4 DF F9 E1 2C B0 E5 79 6A 7D EF 41 DB F0 C2
82 23 4B AC 40 AF BE 8C E2 03 84 13 04 B7 F1 7A
1D CA 09 CF 51 21 09 ED 7B 8B 1C 46 6E F3 36 64
.........................................
用winhex查看0x00地址是正常的
请问大容量卡的spi读写和小容量的步骤不一样?
贴个程序
//写一个字节到sd卡 模拟spi总线方式
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卡读一个字节,模拟spi方式
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卡的响应
unsigned char sdresponse()
{
unsigned char i=0,response;
while(i<=8)
{
response=sdread();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//发送命令到sd卡
void sdcommand(unsigned char command,unsigned long argument,unsigned char CRC)
{
sdwrite(command|0x40); // 0x40:0100 0000
sdwrite(((unsigned char *)&argument)[0]);
sdwrite(((unsigned char *)&argument)[1]);
sdwrite(((unsigned char *)&argument)[2]);
sdwrite(((unsigned char *)&argument)[3]);
sdwrite(CRC);
}
//初始化sd卡
unsigned char sdlnit()
{
int delay=0,trials=0;
unsigned char i;
unsigned char response=0x01;
sd_cs=1;
for(i=0;i<9;i++)
sdwrite(0xff);
sd_cs=0;
// sdcommand(0x00,0,0x95); // 512M的TF卡用这个命令
sdcommand(0x00,0,0x87); //8G TF卡用这个命令
response=sdresponse();
if(response!=0x01)
{
return 0;
}
while(response==0x01)
{
sd_cs=1;
sdwrite(0xff);
sd_cs=0;
sdcommand(0x01,0x00ffc0000,0xff);
response=sdresponse();
}
sd_cs=1;
sdwrite(0xff);
return 1;
}
//从sd卡指定地址读取数据,一次最多512字节
unsigned char sdreadblock(unsigned char *block,unsigned long address,int len)
{
unsigned int count;
sd_cs=0;
sdcommand(0x11,address,0xff);
if(sdresponse()==0) //检测sd卡响应 如果是0
{
while(sdread()!=0xfe);
for(count=0;count<len;count++)
*block++=sdread();
for(count=0;count<512;count++)
sdread();
sdread();
sdread();
sd_cs=1;
sdread();
return 1;
}
}
//主程序
void main()
{
unsigned long a,test;
IO_int(); //IO初始化
LCD_initial(); //LCD屏初始化
LCD_Fill(0,0,lcdwidth,lcdheight,white);//设置背景颜色
UartInit() ;
sdlnit();
sdreadblock(DAT,0,512); //先读取0x00地址开始的512个字节到缓存
for(a=0;a<512;a++)
{
UartSend(DAT[a]);
}
while(1)
{
}
}
|