?? sd.c
字號:
/*-----------------------------------------------
名稱:讀SD卡寫彩屏
公司:上海浩豚電子科技有限公司
網站:www.doflye.net
編寫:師訪
日期:2009.12
修改:無
內容:通過讀出SD卡中320x240像素、16位BMP圖片的HEX數(shù)據(jù),依次寫到屏上,還原圖片
注意事項:由于SD卡使用SPI模式,并且是塊讀取(512Byte),所以需要單片機提供大于512的可用RAM,以供緩沖使用
這里可以選擇STC89c52RD+、STC89C58RD+、STC89C516RD+,后綴RD+表明擴展RAM是1024字節(jié),RC表明擴展RAM256字節(jié)
------------------------------------------------*/
#include <reg52.h>
#include <stdio.h>
#include<9325TP.h>
//=============================================================
//定義SD卡需要的4根信號線
sbit SD_CLK = P1^1;
sbit SD_DI = P1^2;
sbit SD_DO = P1^0;
sbit SD_CS = P1^3;
//===========================================================
//定義按鍵端口
sbit KEY = P3^2;
//===========================================================
//定義512字節(jié)緩沖區(qū),注意需要使用 xdata關鍵字
unsigned char xdata DATA[512];
//===========================================================
//寫一字節(jié)到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卡讀一字節(jié),模擬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;
}
//================================================================
//發(fā)命令到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卡指定地址寫數(shù)據(jù),一次最多512字節(jié)
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); //兩字節(jié)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\n");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token.\n");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC.\n");
return 0;
}
//=======================================================================
//從SD卡指定地址讀取數(shù)據(jù),一次最多512字節(jié)
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\n");
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.\n");
return 0;
}
//============================================================
//主程序
main()
{
unsigned int x,y; //定義液晶屏坐標
unsigned long j; //執(zhí)行循環(huán)需要的臨時變量
unsigned int i;
unsigned long AddTemp=328192;//SD卡地址第一個數(shù)據(jù)物理地址初始值,可以用winhex查看,這里是641物理扇區(qū),512x641=328192,根據(jù)實際SD卡內容更改
CS=1;
delayms(5);
RES=0;
delayms(5);
RES=1;
delayms(5);
ILI9325_Initial();//液晶屏初始化
SdInit(); //SD卡初始化
while(1)
{
for(j=0;j<300;j++) //300表示一幅圖片含有300x512字節(jié)的信息
{
SdReadBlock(DATA,AddTemp+(j*512),512);//每次讀出512字節(jié)放到緩沖區(qū)
for(i=0;i<256;i++) //然后寫到液晶屏,可以顯示256個像素,每個像素16位即2個字節(jié)
{
LCD_SetPos(x,x,y,y);
Write_Data(DATA[2*i+1],DATA[2*i]);
x++;
if(x==240) //檢測是否寫到屏的邊緣 240x320
{
y++;
x=0;
if(y==320)
y=0;
}
}
}
AddTemp = AddTemp+((j+20)*512); //寫完一幅圖片后把SD地址加300x512到下一個圖片地址
while(KEY); //等待按鍵按下繼續(xù)執(zhí)行循環(huán)顯示下一幅圖片,如果沒有按下則等待
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -