2019-03-24 18:06发布
chunyang 发表于 2015-2-9 19:08 列出详细的程序流程,再跟SD卡的读写时序比较,这样更容易发现问题。直接贴代码,简单是简单了,结果呢?
最多设置5个标签!
- uchar SD_ReadSector(uchar *buf,long int sector)
- {
- uchar response,i=0;
- sector*=512;
- CMD17[1]=((sector)>>24);
- CMD17[2]=((sector)>>16);
- CMD17[3]=((sector)>>8);
- CMD17[4]=sector;
- CS=1;
- SEND_BYTE(0xff);
- CS=0;
- do
- {
- response=SEND_COM(CMD17);
- i++;
- if(i==200)
- {
- return response;
- }
- }while(response!=0);
- i=0;
- while(READ_BYTE()!=0xfe);
- for(i=0;i<512;i++)
- {
- *buf++=READ_BYTE();
- }
- CS=1;
- SEND_BYTE(0xff);
- return 0;
-
- }
- uchar SD_WriteSector(uchar *buf,long int sector)
- {
- uchar response,i=0;
- sector*=512;
- CMD24[1]=((sector&0xff000000)>>24);
- CMD24[2]=((sector&0x00ff0000)>>16);
- CMD24[3]=((sector&0x0000ff00)>>8);
- CMD24[4]=sector&0x000000ff;
- CS=1;
- SEND_BYTE(0xff);
- CS=0;
- do
- {
- response=SEND_COM(CMD24);
- i++;
- if(i==200)
- {
- return response;
- }
- }while(response!=0);
- SEND_BYTE(0xfe);
- i=0;
- for(i=0;i<512;i++)
- {
- SEND_BYTE(*buf++);
- }
- SEND_BYTE(0xff);
- SEND_BYTE(0xff);
- response=READ_BYTE();
- if(response&0x1f!=0x05)
- return response;
- while(READ_BYTE()!=0xff);
- CS=1;
- SEND_BYTE(0xff);
- return 0;
- }
复制代码是按照时序来的,还参考了好几个别人的程序。。。
一周热门 更多>