求助51与SD卡+TFT显示,不知道怎么与SD卡通信 求教~!!

2020-02-05 09:09发布

本来看论坛上面很多这方面的资料,可是都下不了。。。有的能发一份给我吗?
直接传论坛或者邮箱hkeg8y@163.com
谢谢了。。。
QQ截图20120518213157.png (24.31 KB, 下载次数: 0) 下载附件 2012-5-18 21:59 上传

//定义SD卡需要的4根信号线
sbit SD_CLK = P1^4;
sbit SD_DI  = P1^3;
sbit SD_DO  = P1^5;
sbit SD_CS  = P1^2;
//===========================================================
//===========================================================
//定义512字节缓冲区,注意需要使用 xdata关键字
unsigned char xdata DATA[512];
//===========================================================
//写一字节到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);
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 SdInit(void)
{
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;
//Send Command 0 to put MMC in SPI mode
SdCommand(0x00,0,0x95);

response=SdResponse();
if(response!=0x01)
{
return 0;
}
while(response==0x01)
{
SD_CS=1;
SdWrite(0xff);
SD_CS=0;
SdCommand(0x01,0x00ffc000,0xff);
response=SdResponse();
}
SD_CS=1;
SdWrite(0xff);
return 1;
}
//================================================================
//往SD卡指定地址写数据,一次最多512字节
unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
{
unsigned int count;
unsigned char dataResp;
//Block size is 512 bytes exactly
//First Lower SS
SD_CS=0;
//Then send write command
SdCommand(0x18,address,0xff);
if(SdResponse()==00)
{
SdWrite(0xff);
SdWrite(0xff);
SdWrite(0xff);
//command was a success - now send data
//start with DATA TOKEN = 0xFE
SdWrite(0xfe);
//now send data
for(count=0;count<len;count++) SdWrite(*Block++);
for(;count<512;count++) SdWrite(0);
//data block sent - now send checksum
SdWrite(0xff); //两字节CRC校验, 为0XFFFF 表示不考虑CRC
SdWrite(0xff);
//Now read in the DATA RESPONSE token
dataResp=SdRead();
//Following the DATA RESPONSE token
//are a number of BUSY bytes
//a zero byte indicates the MMC is busy
while(SdRead()==0);
dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
SD_CS=1;
SdWrite(0xff);
if(dataResp==0x0b)
{
//printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR ");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token. ");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC. ");
return 0;
}
//=======================================================================
//从SD卡指定地址读取数据,一次最多512字节
unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
//printf("MMC_read_block ");
SD_CS=0;
//Then send write command
SdCommand(0x11,address,0xff);
if(SdResponse()==00)
{
//command was a success - now send data
//start with DATA TOKEN = 0xFE
while(SdRead()!=0xfe);
for(count=0;count<len;count++) *Block++=SdRead();
for(;count<512;count++) SdRead();
//data block sent - now send checksum
SdRead();
SdRead();
//Now read in the DATA RESPONSE token
SD_CS=1;
SdRead();
return 1;
}
//printf("Command 0x11 (Read) was not received by the MMC. ");
return 0;
}
//============================================================
//主程序
main()
{
unsigned int x,y; //定义液晶屏坐标
unsigned long j;  //执行循环需要的临时变量
   unsigned int i;
   unsigned long AddTemp=328192;//SD卡地址第一个数据物理地址初始值,可以用winhex查看,这里是641物理扇区,512x641=328192,根据实际SD卡内容更改
   LCD_INIT_HX8347A_032() ;
SdInit();         //SD卡初始化
while(1)
{
for(j=0;j<300;j++)   //300表示一幅图片含有300x512字节的信息
     {
     SdReadBlock(DATA,AddTemp+(j*512),512);//每次读出512字节放到缓冲区
     for(i=0;i<256;i++)                    //然后写到液晶屏,可以显示256个像素,每个像素16位即2个字节
     {   
        adderset(x,x,y,y);
        LCD_WRITE_DATA(DATA[2*i],DATA[2*i+1]);  
     x++;
     if(x==240)                         //检测是否写到屏的边缘 240x320
       {
       y++;
       x=0;
       if(y==320)
         y=0;
       }
        }
      }
     AddTemp = AddTemp+((j+20)*512);          //写完一幅图片后把SD地址加300x512到下一个图片地址
      while(key1);                         //等待按键按下继续执行循环显示下一幅图片,如果没有按下则等待
   
}
用的是STC12C5A60S2芯片
TFT模块已经可以正常显示的了,就差从SD卡中读取数据
这是上网下的一个SD卡程序。。但是不知道该在SD卡中放入什么东西?试着放了一用keil生成的图片HEX,结果是花屏也不知道有没有用,是否与SD卡通信成功,而且240*320的图片已经到150K在keil中不能生成HEX,是直接放入BMP文件吗?
有没有懂的高手指点下、、、第一次接触SD卡
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。