为什么从sd卡读取出来的图片在TFT显示是这样的,屏幕上怎会出现这么多白色的斜线,求大神指点啊~~~
这是SD卡操作:
#include <reg52.h>
#include <stdio.h>
#include<9325TP.h>
//=============================================================
//定义SD卡需要的4根信号线
sbit SD_CLK = P1^7;
sbit SD_DI = P1^5;
sbit SD_DO = P1^6;
sbit SD_CS = P1^0;
//===========================================================
//定义按键端口
sbit KEY = P3^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\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卡指定地址读取数据,一次最多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\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; //执行循环需要的临时变量
unsigned int i;
unsigned long AddTemp=262144;//SD卡地址第一个数据物理地址初始值,可以用winhex查看,这里是512扇区,512x512=262144,根据实际SD卡内容更改
//CS=1;
delayms(25);
RES=0;
delayms(150);
RES=1;
delayms(25);
ILI9325_Initial();//液晶屏初始化
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个字节
{
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); //等待按键按下继续执行循环显示下一幅图片,如果没有按下则等待
}
}
这是9325TP.h
#include"reg52.h"
//============================================================
//根据芯片资料定义
#define WINDOW_XADDR_START 0x0050 // Horizontal Start Address Set
#define WINDOW_XADDR_END 0x0051 // Horizontal End Address Set
#define WINDOW_YADDR_START 0x0052 // Vertical Start Address Set
#define WINDOW_YADDR_END 0x0053 // Vertical End Address Set
#define GRAM_XADDR 0x0020 // GRAM Horizontal Address Set
#define GRAM_YADDR 0x0021 // GRAM Vertical Address Set
#define GRAMWR 0x0022 // memory write
//=============================================================
//定义液晶屏接口
sbit CS=P2^1; //片选
sbit RES=P2^3; //复位
sbit RS=P2^4; //数据/命令选择
sbit RW=P2^5;
//数据口使用P0
//====================================================//
//函数声明
void ILI9325_Initial(void);
void Write_Cmd_Data(unsigned char x, unsigned int y);
void Write_Cmd(unsigned char DH,unsigned char DL);
void Write_Data(unsigned char DH,unsigned char DL);
void delayms(unsigned int tt);
void Write_Data_U16(unsigned int y);
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1);
void ClearScreen(unsigned int bColor);
//===============================================================
//清屏
void ClearScreen(unsigned int bColor)
{
unsigned int i,j;
LCD_SetPos(0,240,0,320);//320x240
for (i=0;i<320;i++)
{
for (j=0;j<240;j++)
Write_Data_U16(bColor);
}
}
//===============================================================
//写命令数据
void Write_Cmd_Data (unsigned char x,unsigned int y)
{
unsigned char m,n;
m=y>>8;
n=y;
Write_Cmd(0x00,x);
Write_Data(m,n);
}
//==============================================================
//写16位数据
void Write_Data_U16(unsigned int y)
{
unsigned char m,n;
m=y>>8;
n=y;
Write_Data(m,n);
}
//=============================================================
//写命令
void Write_Cmd(unsigned char DH,unsigned char DL)
{
CS=0;
RS=0;
P0=DH;
RW=0;
RW=1;
P0=DL;
RW=0;
RW=1;
CS=1;
}
//===================================================================
//写数据
void Write_Data(unsigned char DH,unsigned char DL)
{
CS=0;
RS=1;
P0=DH;
RW=0;
RW=1;
P0=DL;
RW=0;
RW=1;
CS=1;
}
//============================================================
//延时程序
void delayms(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
{
for(j=0;j<260;j++);
}
}
//=============================================================
//液晶初始化
void ILI9325_Initial(void)
{
delayms(50); //根据不同晶振速度可以调整延时,保障稳定显示
Write_Cmd_Data(0x0001,0x0010);
Write_Cmd_Data(0x0002,0x0700);
Write_Cmd_Data(0x0003,0x0030);
Write_Cmd_Data(0x0004,0x0000);
Write_Cmd_Data(0x0008,0x0207);
Write_Cmd_Data(0x0009,0x0000);
Write_Cmd_Data(0x000A,0x0000);
Write_Cmd_Data(0x000C,0x0000);
Write_Cmd_Data(0x000D,0x0000);
Write_Cmd_Data(0x000F,0x0000);
//power on sequence VGHVGL
Write_Cmd_Data(0x0010,0x0000);
Write_Cmd_Data(0x0011,0x0007);
Write_Cmd_Data(0x0012,0x0000);
Write_Cmd_Data(0x0013,0x0000);
//vgh
Write_Cmd_Data(0x0010,0x1290);
Write_Cmd_Data(0x0011,0x0227);
delayms(100);
//vregiout
Write_Cmd_Data(0x0012,0x001d); //0x001b
delayms(100);
//vom amplitude
Write_Cmd_Data(0x0013,0x1500);
delayms(100);
//vom H
Write_Cmd_Data(0x0029,0x0018);
Write_Cmd_Data(0x002B,0x000D);
//gamma
Write_Cmd_Data(0x0030,0x0004);
Write_Cmd_Data(0x0031,0x0307);
Write_Cmd_Data(0x0032,0x0002);// 0006
Write_Cmd_Data(0x0035,0x0206);
Write_Cmd_Data(0x0036,0x0408);
Write_Cmd_Data(0x0037,0x0507);
Write_Cmd_Data(0x0038,0x0204);//0200
Write_Cmd_Data(0x0039,0x0707);
Write_Cmd_Data(0x003C,0x0405);// 0504
Write_Cmd_Data(0x003D,0x0F02);
//ram
Write_Cmd_Data(0x0050,0x0000);
Write_Cmd_Data(0x0051,0x00EF);
Write_Cmd_Data(0x0052,0x0000);
Write_Cmd_Data(0x0053,0x013F);
Write_Cmd_Data(0x0060,0xA700);
Write_Cmd_Data(0x0061,0x0001);
Write_Cmd_Data(0x006A,0x0000);
//
Write_Cmd_Data(0x0080,0x0000);
Write_Cmd_Data(0x0081,0x0000);
Write_Cmd_Data(0x0082,0x0000);
Write_Cmd_Data(0x0083,0x0000);
Write_Cmd_Data(0x0084,0x0000);
Write_Cmd_Data(0x0085,0x0000);
//
Write_Cmd_Data(0x0090,0x0010);
Write_Cmd_Data(0x0092,0x0600);
Write_Cmd_Data(0x0093,0x0003);
Write_Cmd_Data(0x0095,0x0110);
Write_Cmd_Data(0x0097,0x0000);
Write_Cmd_Data(0x0098,0x0000);
Write_Cmd_Data(0x0007,0x0133);
// Write_Cmd_Data(0x0022);//
}
//===============================================================
//定义坐标
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
{
Write_Cmd_Data(WINDOW_XADDR_START,x0);
Write_Cmd_Data(WINDOW_XADDR_END,x1);
Write_Cmd_Data(WINDOW_YADDR_START,y0);
Write_Cmd_Data(WINDOW_YADDR_END,y1);
Write_Cmd_Data(GRAM_XADDR,x0);
Write_Cmd_Data(GRAM_YADDR,y0);
Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
}
|