硬件 TFT触摸显示屏(模组:HX8347D) W78E516D 8位连接
现象:图片显示正常,小区域(方格)显示不受控 ,送不到指定地点。
现象用红色字体描述
程序
/***************************************************************************************************
IC: HX8347D
LCD: CMO F02421-01U
****************************************************************************************************/
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define ulint unsigned long int
void TestSingleColor(uchar dh,uchar dl);
sbit reset=P3^4;
sbit rs =P3^5;
sbit wr =P3^6;
sbit rd =P3^7;
sbit cs =P3^0;
sbit Stop =P3^3;
//******TP TEST define start******//
sbit TP_CS =P3^1;
sbit TP_DCLK =P1^0;
sbit TP_DIN =P1^7;
sbit TP_PENIRQ=P3^2;
sbit TP_DOUT =P1^6;
uint vcm=0,vdv=0;
uint value=0;
uchar test1=6;
uint XX,YY;
uchar TP_N=0;
#define ymin 24
#define xmin 12
#define ymax 239
#define xmax 232
void PutPixel(uint x,uint y,uchar hbit,uchar lbit);
void RAMAddressRelease(uchar hs,uchar he,uchar vs,uchar ve); //
//***********16级Palette(240*320)***********//
uchar code palette_r[]={0x00,0x00, 0x10,0x00, 0x20,0x00, 0x30,0x00, 0x40,0x00, 0x50,0x00, 0x60,0x00, 0x70,0x00,
/*green gray scale*/ 0x80,0x00, 0x90,0x00, 0xa0,0x00, 0xb0,0x00, 0xc0,0x00, 0xd0,0x00, 0xe0,0x00, 0xf0,0x00,};
uchar code palette_g[]={0x00,0x00, 0x00,0x80, 0x01,0x00, 0x01,0x80, 0x02,0x00, 0x02,0x80, 0x03,0x00, 0x03,0x80,
/*blue gray scale*/ 0x04,0x00, 0x04,0x80, 0x05,0x00, 0x05,0x80, 0x06,0x00, 0x06,0x80, 0x07,0x00, 0x07,0x80,};
uchar code palette_b[]={0x00,0x00, 0x00,0x02, 0x00,0x04, 0x00,0x06, 0x00,0x08, 0x00,0x0a, 0x00,0x0c, 0x00,0x0e,
/*white gray scale*/ 0x00,0x10, 0x00,0x12, 0x00,0x14, 0x00,0x16, 0x00,0x18, 0x00,0x1a, 0x00,0x1c, 0x00,0x1F,};
uchar code palette_w[]={0x00,0x00, 0x10,0xA2, 0x21,0x04, 0x31,0xA6, 0x42,0x08, 0x52,0xAA, 0x63,0x0C, 0x7B,0xEF,
0x8C,0x71, 0x9C,0xF3, 0xAD,0x75, 0xBD,0xF7, 0xCE,0x79, 0xDE,0xFB, 0xEF,0x7D, 0xFF,0xFF,};
uchar code gImage_tree[60000];
void delay(uint ms)
{
uint i;
uchar j;
for (i=0;i<ms;i++)
for (j=0;j<250;j++);
}
void Pause_EX0(void) interrupt 2
{
while(!Stop);
delay(50);
while(Stop);
delay(50);
while(!Stop);
delay(50);
}
void Enable_interrupt1()
{
EA=1; //all interrupt enable
PX1=1; //int1 pri
EX1=1; //int1 interrupt enable
// IT1=0; //LOW ACTIVE
IE=0x84; //EA**ES ET1 EX1 ET0 EX0
}
/*
void Mainwrite_data_8bit(uchar dath,uchar datl)
{
rs=1;
cs=0;
P1=dath;
wr=0;
wr=1;
P1=datl;
wr=0;
wr=1;
cs=1;
}
*/
void write_command_8bit(uchar CMD)
{
rd=1;
rs=0;
cs=0;
P1=CMD;
wr=0;
wr=1;
cs=1;
}
/*
void write_data_8bit(uint DAT)
{
Mainwrite_data_8bit(DAT>>8,DAT);
}
*/
void write_data_8bit(uchar dat)
{
rd=1;
rs=1;
cs=0;
P1=dat;
wr=0;
wr=1;
cs=1;
}
void Set_LCD_8B_REG(uint c,uint d)
{
write_command_8bit(c);
write_data_8bit(d);
}
void Reset(void)
{
reset=1;
delay(50); // Delay 1ms
reset=0;
delay(50); // Delay 1ms // This delay time is necessary
reset=1;
delay(100);
}
void initial_cus()
{
Reset();
//Set_LCD_8B_REG(0x01,0x00);
Set_LCD_8B_REG(0x2E,0x79); //
Set_LCD_8B_REG(0xEE,0x0C); //
// Driving ability Setting
Set_LCD_8B_REG(0xEA,0x00); //PTBA[15:8]
Set_LCD_8B_REG(0xEB,0x20); //PTBA[7:0]
Set_LCD_8B_REG(0xEC,0x08); //STBA[15:8]
Set_LCD_8B_REG(0xED,0xC4); //STBA[7:0]
Set_LCD_8B_REG(0xE8,0x40); //OPON[7:0]
Set_LCD_8B_REG(0xE9,0x38); //OPON1[7:0]
Set_LCD_8B_REG(0xF1,0x01); //OTPS1B
Set_LCD_8B_REG(0xF2,0x10); //GEN
Set_LCD_8B_REG(0x27,0xA3); //
//Gamma 2.2 Setting
Set_LCD_8B_REG(0x40,0x01); //
Set_LCD_8B_REG(0x41,0x07); //
Set_LCD_8B_REG(0x42,0x07); //
Set_LCD_8B_REG(0x43,0x13); //
Set_LCD_8B_REG(0x44,0x11); //
Set_LCD_8B_REG(0x45,0x24); //
Set_LCD_8B_REG(0x46,0x10); //
Set_LCD_8B_REG(0x47,0x57); //
Set_LCD_8B_REG(0x48,0x09); //
Set_LCD_8B_REG(0x49,0x14); //
Set_LCD_8B_REG(0x4A,0x19); //
Set_LCD_8B_REG(0x4B,0x19); //
Set_LCD_8B_REG(0x4C,0x16); //
Set_LCD_8B_REG(0x50,0x1B); //
Set_LCD_8B_REG(0x51,0x2E); //
Set_LCD_8B_REG(0x52,0x2C); //
Set_LCD_8B_REG(0x53,0x38); //
Set_LCD_8B_REG(0x54,0x38); //
Set_LCD_8B_REG(0x55,0x3E); //
Set_LCD_8B_REG(0x56,0x2A); //
Set_LCD_8B_REG(0x57,0x6F); //
Set_LCD_8B_REG(0x58,0x09); //
Set_LCD_8B_REG(0x59,0x06); //
Set_LCD_8B_REG(0x5A,0x06); //
Set_LCD_8B_REG(0x5B,0x0B); //
Set_LCD_8B_REG(0x5C,0x16); //
Set_LCD_8B_REG(0x5D,0xCC); //
//Power Voltage Setting
Set_LCD_8B_REG(0x1B,0x1B); //VRH=4.65V
Set_LCD_8B_REG(0x1A,0x01); //BT (VGH~15V,VGL~-10V,DDVDH~5V)
Set_LCD_8B_REG(0x24,0x2F); //VMH(VCOM High voltage ~3.2V)
Set_LCD_8B_REG(0x25,0x57); //VML(VCOM Low voltage -1.2V)
//VCOM offset
Set_LCD_8B_REG(0x23,0x8a); //for Flicker adjust //can reload from OTP
// Power on Setting
Set_LCD_8B_REG(0x18,0x36); //I/P_RADJ,N/P_RADJ, Normal mode 75Hz
Set_LCD_8B_REG(0x19,0x01); //OSC_EN='1', start Osc
Set_LCD_8B_REG(0x01,0x00); //DP_STB='0', out deep sleep
Set_LCD_8B_REG(0x1F,0x88);// GAS=1, VOMG=00, PON=0, DK=1, XDK=0, DVDH_TRI=0, STB=0
delay(5);
Set_LCD_8B_REG(0x1F,0x80);// GAS=1, VOMG=00, PON=0, DK=0, XDK=0, DVDH_TRI=0, STB=0
delay(5);
Set_LCD_8B_REG(0x1F,0x90);// GAS=1, VOMG=00, PON=1, DK=0, XDK=0, DVDH_TRI=0, STB=0
delay(5);
Set_LCD_8B_REG(0x1F,0xD0);// GAS=1, VOMG=10, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
delay(5);
//262k/65k color selection
Set_LCD_8B_REG(0x17,0x05); //default 0x06 262k color // 0x05 65k color
//Set_LCD_8B_REG(0x16,0x00);
//SET PANEL
Set_LCD_8B_REG(0x36,0x00); //SS_P, GS_P,REV_P,BGR_P
//Display ON Setting
Set_LCD_8B_REG(0x28,0x38); //GON=1, DTE=1, D=1000
delay(40);
Set_LCD_8B_REG(0x28,0x3C); //GON=1, DTE=1, D=1100
// Set GRAM Area
Set_LCD_8B_REG(0x02,0x00);
Set_LCD_8B_REG(0x03,0x00); //Column Start
Set_LCD_8B_REG(0x04,0x00);
Set_LCD_8B_REG(0x05,0xEF); //Column End
Set_LCD_8B_REG(0x06,0x00);
Set_LCD_8B_REG(0x07,0x00); //Row Start
Set_LCD_8B_REG(0x08,0x01);
Set_LCD_8B_REG(0x09,0x3F); //Row End
write_command_8bit(0x22); //Start GRAM write
}
void Test_palette()
{
uchar i,j,h;
// RAMAddressRelease(0,239,0,319);
rs=1;
cs=0;
rd=1;
for (i=64;i>0;i--)
for (j=16;j>0;j--)
{
for (h=0;h<15;h++)
{
P1=palette_r[2*j-2];
wr=0;wr=1;
P1=0x00;
wr=0;wr=1;
}
}
for (i=0;i<64;i++)
for (j=0;j<16;j++)
{
for (h=0;h<15;h++)
{
P1=palette_g[2*j];
wr=0;wr=1;
P1=palette_g[2*j+1];
wr=0;wr=1;
}
}
for (i=64;i>0;i--)
for (j=16;j>0;j--)
{
for (h=0;h<15;h++)
{
P1=0x00; wr=0;wr=1;
P1=palette_b[2*j-1]; wr=0;wr=1;
}
}
for (i=0;i<128;i++)
for (j=0;j<16;j++)
{
for (h=0;h<15;h++)
{
P1=palette_w[2*j];
wr=0;wr=1;
P1=palette_w[2*j+1];
wr=0;wr=1;
}
}
/* for (i=64;i>0;i--)
for (j=16;j>0;j--)
{
for (h=0;h<15;h++)
{
P1=palette_w[2*j-2];
wr=0;wr=1;
P1=palette_w[2*j-1];
wr=0;wr=1;
}
} */
}
void RAMAddressRelease(uchar hs,uchar he,uchar vs,uchar ve) //
{
// Set_LCD_8B_REG(0x02,0x00);
// Set_LCD_8B_REG(0x03,0x00); //Column Start
Set_LCD_8B_REG(0x02,0x00);
Set_LCD_8B_REG(0x03,hs); //Column Start
Set_LCD_8B_REG(0x04,0x00);
Set_LCD_8B_REG(0x05,he); //Column End
// Set_LCD_8B_REG(0x06,0x00);
// Set_LCD_8B_REG(0x07,0x00); //Row Start
Set_LCD_8B_REG(0x06,0x00);
Set_LCD_8B_REG(0x07,vs); //Row Start
Set_LCD_8B_REG(0x08,0x01);
Set_LCD_8B_REG(0x09,ve); //Row End
write_command_8bit(0x22);
/*
Set_LCD_8B_REG(0x02,0x00);
Set_LCD_8B_REG(0x03,0x00); //Column Start
Set_LCD_8B_REG(0x04,0x00);
Set_LCD_8B_REG(0x05,0xEF); //Column End
Set_LCD_8B_REG(0x06,0x00);
Set_LCD_8B_REG(0x07,0x00); //Row Start
Set_LCD_8B_REG(0x08,0x01);
Set_LCD_8B_REG(0x09,0x3F); //Row End
*/
}
void TestSingleColor_blackwhite(uchar dat)
{
uint ver;
uchar hor;
RAMAddressRelease(0,239,0,319);
write_command_8bit(0x22);
cs=0;
rs=1;
rd=1;
P1=dat;
for(ver=0;ver<320;ver++)
{
for(hor=0;hor<10;hor++)
{
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;wr=0;wr=1;
}
}
}
void TestSingleColor(uchar dh,uchar dl)
{
uint ver;
uchar hor;
RAMAddressRelease(0,239,0,319);
write_command_8bit(0x22);
cs=0;
rs=1;
rd=1;
for(ver=0;ver<320;ver++)
{
for(hor=0;hor<240;hor++)
{
P1=dh;wr=0;wr=1;
P1=dl;wr=0;wr=1;
}
}
}
void PutPixel(uint x, uint y,uchar hbit,uchar lbit)
{
Set_LCD_8B_REG(0x02,0x00);
Set_LCD_8B_REG(0x03,0x00);
Set_LCD_8B_REG(0x02,x>>8);
Set_LCD_8B_REG(0x03,x); //Column Start
// Set_LCD_8B_REG(0x04,0x00);
// Set_LCD_8B_REG(0x05,Xn); //Column End
Set_LCD_8B_REG(0x06,0x00);
Set_LCD_8B_REG(0x07,0x00);
Set_LCD_8B_REG(0x06,y>>8);
Set_LCD_8B_REG(0x07,y); //Row Start
// Set_LCD_8B_REG(0x08,0x01);
// Set_LCD_8B_REG(0x09,Yn); //Row End
write_command_8bit(0x22);
rs=1;cs=0;
P1=hbit;
wr=0;wr=1;
P1=lbit;
wr=0;wr=1;
cs=1;
}
/*
//-------------填写单色正方形小区域------------------------
void Write_Block(uchar bh,uchar bl,uchar N)
{
uchar i,j;
cs=0;
rs=1;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
P1=bh;
wr=0; wr=1;
P1=bl;
wr=0; wr=1;
}
}
cs=1;
}
//-----------------------------------------------------------------
//---------------------TP五点测试及画线----------------------------
void TP_Test(uint x,uint y)
{
switch(TP_N)
{
//-----------左上角白点区域测试---------------------
case 0:
if((x<80)&&(y<80)) //------如果按下此区域
{
//----------------清除白色区域----------------
RAMAddressRelease(10,29,10,29);
Write_Block(0x00,0x00,20);
//-----------------显示下一次要测试的区域------------
RAMAddressRelease(210,229,10,29);
Write_Block(0xf8,0x00,20);
TP_N++; //---------测试步骤加1----------------
break;
}
else break;
//------------右上角红点区域测试---------------------
case 1:
if((x>160)&&(y<80))
{
RAMAddressRelease(210,229,10,29);
Write_Block(0x00,0x00,20);
RAMAddressRelease(10,29,290,309);
Write_Block(0x07,0xe0,20);
TP_N++;
break;
}
else break;
//-------------左下角绿点区域测试---------------------(小方格在左上角显示 触摸点在左下角)--
case 2:
if((x<80)&&(y>240))
{
RAMAddressRelease(10,29,290,309);
Write_Block(0x00,0x00,20);
RAMAddressRelease(210,229,290,309);
Write_Block(0x00,0x1f,20);
TP_N++;
break;
}
else break;
//-------------右下角蓝点区域测试-----------------------小方格在右上角显示 触摸点在右下角
case 3:
if((x>160)&&(y>240))
{
RAMAddressRelease(210,229,290,309);
Write_Block(0x00,0x00,20);
RAMAddressRelease(110,129,150,169);
Write_Block(0xf8,0x1f,20);
TP_N++;
break;
}
else break;
//-------------中心点粉红区域测试-----------------------
case 4:
if((x>80)&&(x<160)&&(y>120)&&(y<240))
{
RAMAddressRelease(110,129,150,169);
Write_Block(0x00,0x00,20);
TP_N++;
RAMAddressRelease(0,239,0,319); //-----重定RAM区域以便画线测试。
break;
}
else break;
//--------------整个区域画线测试----------------------------
case 5:
PutPixel(x,y,0xff,0xff);
default:
break;
}
}
*/
//======================================================================================
// TP写----较成熟的写时序,能保证单片机和仿真器都能正常使用
//======================================================================================
void TP_write(uchar t)
{
uchar i;
TP_DCLK=1;
TP_CS=0;
for(i=0;i<8;i++)
{
TP_DIN=(bit)(t&0x80);
TP_DCLK=1;
t=t<<1;
TP_DCLK=0;//be low ,before read
}
}
//======================================================================================
// TP读----较成熟的读时序,能保证单片机和仿真器都能正常使用
//======================================================================================
uchar TP_read(void)
{
uchar i,tp_data=0;
TP_CS=0;
TP_DOUT=1;
TP_DCLK=0;
for(i=0;i<8;i++)
{
TP_DCLK=1;
TP_DCLK=0;
if(TP_DOUT)
{
tp_data=tp_data|(1<<(7-i));
}
}
return(tp_data);
TP_CS=1;
}
//======================================================================================
// TP取值 中值滤波法--将取道的数据先排序,再取中间值
//======================================================================================
void GetInformation(uchar CRT_BYTE)//give the control byte
{
uint temp;
uchar i=0,j=0,k=0;
uint TP[11]; //此算法取11比较合适
TP_CS=0; //此处能保证
TP_write(CRT_BYTE+1); //在仿真器上
TP_CS=1; //也能正常使用
while((i<11)&&(!TP_PENIRQ)) //关键语句----能有效去处抬笔时的飞线
{
TP_CS=0;
TP_write(CRT_BYTE+1);
TP=TP_read();
i++;
TP_CS=1;
}
if(i!=0)
{
for(j=0;j<i;j++)
for(k=0;k<i-j;k++)
if (*(TP+k)>*(TP+k+1)) //使用指针访问数组要比使用数组下标访问更快
{
temp=*(TP+k);
*(TP+k)=*(TP+k+1);
*(TP+k+1)=temp;
}
value=TP[i/2];
}
}
void TP_Line(void)
{
uint TY=0,TX=0; //coordinate value read from TP
delay(25); //关键语句----能有效去掉落笔时的飞线,经验证,25mS比较适中
while(!TP_PENIRQ) //关键语句----保证读取触摸屏数据的连续性
{
GetInformation(0xd8);//8bit mode ,x-position measure
XX=value; //用于TP定位(使用MedWin的观察窗口)
TX=(value-xmin)*120/((xmax-xmin)/2); //注意计算时要分子分母同时除以2,以免超出单片机计算范围
GetInformation(0x98);//8bit mode ,y-position measure
YY=value; //用于TP定位(使用MedWin的观察窗口)
TY=(value-ymin)*160/((ymax-ymin)/2);
// TX=240-TX; //TP与LCD点阵映射相反
TY=320-TY; //
if((!TP_PENIRQ)&&(TX>=0&&TX<240)&&(TY>=0&&TY<320)) //关键语句----对去掉触摸屏边缘飞线很有效
{
PutPixel(TX,TY,0xff,0xff);
// TP_Test(TX,TY);
}
}
}
void Enable_interrupt0()
{
EA=1; //all interrupt enable
PX0=1; //int0 pri
EX0=1; //int0 interrupt enable
// IT1=0; //LOW ACTIVE
IE=0x81; //EA**ES ET1 EX1 ET0 EX0
}
void TP_EX0(void) interrupt 0
{
TP_Line(); //TP画线
}
void Image_display()
{
uchar l,k;
uint count=0;
RAMAddressRelease(45,194,58,257); //
rs=1;cs=0;
for(k=0;k<200;k++)
for(l=0;l<150;l++)
{
P1=gImage_tree[count];
wr=0;
wr=1;
P1=gImage_tree[count+1];
wr=0;
wr=1;
count=count+2;
}
cs=1;
}
void main(void)
{
Enable_interrupt1();
initial_cus();
// RAMAddressRelease(0,239,0,319);
Test_palette();
delay(800);
TestSingleColor_blackwhite(0x00);
delay(800);
TestSingleColor_blackwhite(0xff);
delay(800);
// TestSingleColor(0xff,0xff);
// delay(800);
TestSingleColor(0xf8,0x00);
delay(800);
TestSingleColor(0x07,0xe0);
delay(800);
TestSingleColor(0x00,0x1f);
delay(800);
Image_display();
delay(800);
TestSingleColor_blackwhite(0x00);
// TestSingleColor(0x00,0x00);
delay(400);
// RAMAddressRelease(10,29,10,29);
// Write_Block(0xff,0xff,20);
Enable_interrupt0();
while(1);
}
unsigned char code gImage_tree[60000] = { |