TFT显示图片不正常,
程序如下
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;
void TFT_SPI_Write_Byte(u8 num)
{
u8 count=0;
for(count=0;count<8;count++)
{
CLR_SCL();
if(num&0x80)SET_SDI();
else CLR_SDI();
num<<=1;
SET_SCL();
}
}
void LCD_WR_REG(u8 CMD)
{
CLR_CS();
CLR_SCL();
CLR_SDI();
SET_SCL();
TFT_SPI_Write_Byte(CMD);
SET_CS();
}
void LCD_WR_DATA(u8 DATA)
{
CLR_CS();
CLR_SCL();
SET_SDI();
SET_SCL();
TFT_SPI_Write_Byte(DATA);
SET_CS();
}
//*****************************************************************************************
//*************************************************************************
void LCD_WriteRAM_Prepare(void)
{
LCD_WR_REG(0X2C);
}
//开启显示
void LCD_DisplayOn(void)
{
LCD_WR_REG(0X29);
}
//LCD关闭显示
void LCD_DisplayOff(void)
{
LCD_WR_REG(0X28);
}
//设置光标
inline void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
#if USE_HORIZONTAL
#else
LCD_WR_REG(0x2A);
LCD_WR_DATA(Xpos>>8);
LCD_WR_DATA(0xFF&Xpos);
LCD_WR_REG(0x2B);
LCD_WR_DATA(Ypos>>8);
LCD_WR_DATA(0xFF&Ypos);
#endif
}
//画点
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_SetCursor(x,y);
LCD_WriteRAM_Prepare();
LCD_WR_DATA(POINT_COLOR>>8);
LCD_WR_DATA(POINT_COLOR);
}
//清屏
void LCD_Clear(u16 Color)
{
u32 index=0;
LCD_SetCursor(0x00,0x0000);
LCD_WriteRAM_Prepare();
for(index=0;index<320*240;index++)
{
LCD_WR_DATA(Color>>8);
LCD_WR_DATA(Color);
}
}
//填充颜 {MOD}
void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
{
u16 i,j;
u16 xlen=0;
#if USE_HORIZONTAL==1
xlen=yend-ysta+1;
for(i=xsta;i<=xend;i++)
{
LCD_SetCursor(i,ysta);
LCD_WriteRAM_Prepare();
for(j=0;j<xlen;j++)
{
LCD_WR_DATA(color>>8);
LCD_WR_DATA(color);
}
}
#else
xlen=xend-xsta+1;
for(i=ysta;i<=yend;i++)
{
LCD_SetCursor(xsta,i);
LCD_WriteRAM_Prepare();
for(j=0;j<xlen;j++)
{
LCD_WR_DATA(color>>8);
LCD_WR_DATA(color);
}
}
#endif
}
//画线
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
{
u16 t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1;
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1;
else if(delta_x==0)incx=0;
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if(delta_y==0)incy=0;
else{incy=-1;delta_y=-delta_y;}
if( delta_x>delta_y)distance=delta_x;
else distance=delta_y;
for(t=0;t<=distance+1;t++ )
{
LCD_DrawPoint(uRow,uCol);
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
//画矩形
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
{
LCD_DrawLine(x1,y1,x2,y1);
LCD_DrawLine(x1,y1,x1,y2);
LCD_DrawLine(x1,y2,x2,y2);
LCD_DrawLine(x2,y1,x2,y2);
}
//画圆
void Draw_Circle(u16 x0,u16 y0,u8 r)
{
int a,b;
int di;
a=0;b=r;
di=3-(r<<1);
while(a<=b)
{
LCD_DrawPoint(x0-b,y0-a); //3
LCD_DrawPoint(x0+b,y0-a); //0
LCD_DrawPoint(x0-a,y0+b); //1
LCD_DrawPoint(x0-b,y0-a); //7
LCD_DrawPoint(x0-a,y0-b); //2
LCD_DrawPoint(x0+b,y0+a); //4
LCD_DrawPoint(x0+a,y0-b); //5
LCD_DrawPoint(x0+a,y0+b); //6
LCD_DrawPoint(x0-b,y0+a);
a++;
if(di<0)di +=4*a+6;
else
{
di+=10+4*(a-b);
b--;
}
LCD_DrawPoint(x0+a,y0+b);
}
}
//显示字符
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{
#if USE_HORIZONTAL==1
#define MAX_CHAR_POSX 312
#define MAX_CHAR_POSY 232
#else
#define MAX_CHAR_POSX 232
#define MAX_CHAR_POSY 312
#endif
u8 temp;
u8 pos,t;
u16 x0=x;
u16 colortemp=POINT_COLOR;
if(x>MAX_CHAR_POSX||y>MAX_CHAR_POSY)return;
num=num-' ';
if(!mode)
{
for(pos=0;pos<size;pos++)
{
if(size==12)temp=asc2_1206[num][pos];
else temp=asc2_1608[num][pos];
for(t=0;t<size/2;t++)
{
if(temp&0x01)POINT_COLOR=colortemp;
else POINT_COLOR=BACK_COLOR;
LCD_DrawPoint(x,y);
temp>>=1;
x++;
}
x=x0;
y++;
}
}else
{
for(pos=0;pos<size;pos++)
{
if(size==12)temp=asc2_1206[num][pos];
else temp=asc2_1608[num][pos];
for(t=0;t<size/2;t++)
{
if(temp&0x01)LCD_DrawPoint(x+t,y+pos);
temp>>=1;
}
}
}
POINT_COLOR=colortemp;
}
u32 mypow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//显示数字
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
continue;
}else enshow=1;
}
LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0);
}
}
//************************************************************
void LCD_Show2Num(u16 x,u16 y,u16 num,u8 len,u8 size,u8 mode)
{
u8 t,temp;
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode);
}
}
//显示字符串
void LCD_ShowString(u16 x,u16 y,const u8 *p)
{
while(*p!=' ')
{
if(x>MAX_CHAR_POSX){x=0;y+=16;}
if(y>MAX_CHAR_POSY){y=x=0;LCD_Clear(WHITE);}
LCD_ShowChar(x,y,*p,16,0);
x+=8;
p++;
}
}
//**********************************************************
void DelayLcd(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
//****************************************************************
void LCD_Reset(void)
{
GPIO_SetBits(GPIOC, GPIO_Pin_5 );
Delay_8ms;
GPIO_ResetBits(GPIOC, GPIO_Pin_5 );
Delay_375ms;
GPIO_SetBits(GPIOC, GPIO_Pin_5 );
DelayLcd(0xFFF);
}
void Lcd_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
LCD_Reset();
Delay_375ms();
//***************************************************
LCD_WR_REG(0xCF);
LCD_WR_DATA (0x00);
LCD_WR_DATA (0x81);
LCD_WR_DATA (0X30);
LCD_WR_REG(0xED);
LCD_WR_DATA (0x64);
LCD_WR_DATA (0x03);
LCD_WR_DATA (0X12);
LCD_WR_DATA (0X81);
LCD_WR_REG(0xE8);
LCD_WR_DATA (0x85);
LCD_WR_DATA (0x00);
LCD_WR_DATA (0x79);
LCD_WR_REG(0xCB);
LCD_WR_DATA (0x39);
LCD_WR_DATA (0x2C);
LCD_WR_DATA (0x00);
LCD_WR_DATA (0x34);
LCD_WR_DATA (0x02);
LCD_WR_REG(0xF7);
LCD_WR_DATA (0x20);
LCD_WR_REG(0xEA);
LCD_WR_DATA (0x00);
LCD_WR_DATA (0x00);
LCD_WR_REG(0xC0); //Power control
LCD_WR_DATA (0x1D); //VRH[5:0]
LCD_WR_REG(0xC1); //Power control
LCD_WR_DATA (0x12); //SAP[2:0];BT[3:0]
LCD_WR_REG(0xC5); //VCM control
LCD_WR_DATA (0x30);
LCD_WR_DATA (0x3F);
LCD_WR_REG(0xC7); //VCM control
LCD_WR_DATA (0x9E);
LCD_WR_REG(0x3A); // Memory Access Control
LCD_WR_DATA (0x55);
LCD_WR_REG(0x36); // Memory Access Control
LCD_WR_DATA (0x08);
LCD_WR_REG(0xB1);
LCD_WR_DATA (0x00);
LCD_WR_DATA (0x17);
LCD_WR_REG(0xB6); // Display Function Control
LCD_WR_DATA (0x0A);
LCD_WR_DATA (0xA2);
LCD_WR_REG(0xF2); // 3Gamma Function Disable
LCD_WR_DATA (0x00);
LCD_WR_REG(0x26); //Gamma curve selected
LCD_WR_DATA (0x01);
LCD_WR_REG(0xE0); //Set Gamma
LCD_WR_DATA (0x0F);
LCD_WR_DATA (0x22);
LCD_WR_DATA (0x1C);
LCD_WR_DATA (0x1B);
LCD_WR_DATA (0x08);
LCD_WR_DATA (0x0F);
LCD_WR_DATA (0x48);
LCD_WR_DATA (0xB8);
LCD_WR_DATA (0x34);
LCD_WR_DATA (0x05);
LCD_WR_DATA (0x0C);
LCD_WR_DATA (0x09);
LCD_WR_DATA (0x0F);
LCD_WR_DATA (0x07);
LCD_WR_DATA (0x00);
LCD_WR_REG(0XE1); //Set Gamma
LCD_WR_DATA (0x00);
LCD_WR_DATA (0x23);
LCD_WR_DATA (0x24);
LCD_WR_DATA (0x07);
LCD_WR_DATA (0x10);
LCD_WR_DATA (0x07);
LCD_WR_DATA (0x38);
LCD_WR_DATA (0x47);
LCD_WR_DATA (0x4B);
LCD_WR_DATA (0x0A);
LCD_WR_DATA (0x13);
LCD_WR_DATA (0x06);
LCD_WR_DATA (0x30);
LCD_WR_DATA (0x38);
LCD_WR_DATA (0x0F);
LCD_WR_REG(0x2B);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x01);
LCD_WR_DATA(0x3f);
LCD_WR_REG(0x2A);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0xef);
LCD_WR_REG(0x11); //Exit Sleep
Delay_375ms();
LCD_WR_REG(0x29); //Display on
//LCD_LED=1;
LCD_Clear(BLACK);
}
//**************************** 写RAM*************************************
void LCD_WriteRAM(u16 RGB_Code)
{
LCD_WR_DATA(RGB_Code);
}
u16 LCD_BGR2RGB(u16 c)
{
u16 r,g,b,rgb;
b=(c>>0)&0x1f;
g=(c>>5)&0x3f;
r=(c>>11)&0x1f;
rgb=(b<<11)+(g<<5)+(r<<0);
return(rgb);
}
//*************************************************************************************************
//*****************************************************************
void LCDOpenWindows(u16 x, u16 y, u16 Xend,u16 Yend)
{
LCD_WR_REG(0x2A);
LCD_WR_DATA(x >> 8);
LCD_WR_DATA(x & 0xff);
LCD_WR_DATA(Xend >> 8);
LCD_WR_DATA(Xend & 0xff);
LCD_WR_REG(0x2B);
LCD_WR_DATA(y >> 8);
LCD_WR_DATA(y & 0xff);
LCD_WR_DATA(Yend >> 8);
LCD_WR_DATA(Yend & 0xff);
}
/****************************************************************************
* 名 称:void LCD_DrawPicture(u16 StartX,u16 StartY,u16 EndX,u16 EndY,const unsigned char *pic)
* 功 能:在指定座标范围显示一副图片
* 入口参数:StartX 行起始座标
* StartY 列起始座标
* EndX 行结束座标
* EndY 列结束座标
pic 图片头指针
* 出口参数:无
* 说 明:图片取模格式为水平扫描,16位颜 {MOD}模式
* 调用方法:LCD_DrawPicture(0,0,100,100,(u16*)demo);
****************************************************************************/
void LCD_DrawPicture(u16 StartX,u16 StartY,u16 Xend,u16 Yend,const unsigned char *pic)
{
u16 i=0;
LCDOpenWindows(StartX,StartY,Xend,Yend);
LCD_SetCursor(StartX,StartY);
LCD_WriteRAM_Prepare();
for (i=0;i<(Xend*Yend);i++)
{
LCD_WriteRAM(*pic++);
}
}
一周热门 更多>