高级会员

- 积分
- 500
- 金钱
- 500
- 注册时间
- 2014-9-1
- 在线时间
- 77 小时
|
5金钱
之前手上有一块电阻屏,触摸一直没调好,SPI读过来的数据不规律,同一个点附近读过来的(x, y)经常会不一样,移动触笔数据变化感觉也不对,怀疑是坏了。
最近新买了一块,还是出现类似问题,就想问一下是电阻屏坏的可能性大还是程序不对!
上程序,试过两点校正,三点校正。用原子的TP_Adjust(void)直接都不合格!
请大神们帮忙开一下!
[mw_shl_code=c,true]void Touch_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC ,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_6);//éÏà-êäèë
//′¥ÃtÆáCSDÅoÅ£¬íÆíìêä3ö£¬μíμçÆ½óDD§£¡
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//ÅäÖÃ′¥ÃtÆá±êÖD¶ÏÅäÖÃòy½Å C4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
// Ñóê±
void SpiDelay(unsigned int DelayCnt)
{
unsigned int i;
for(i=0;i<DelayCnt;i++);
}
void TOUCH_RESET(void)
{
TCLK=0;
TCS=1;
TDIN=0;
TCLK=0;
DOUT=0;
TCS=0;
}
void ADS7843_SPI_Start( void )
{
TCLK=0;
TCS=1;
TDIN=1;
TCLK=1;
TCS=0;
}
void ADS7843_Write (unsigned char cmd )
{
u8 count=0;
for(count=0;count<8;count++)
{
if(cmd&0x80)TDIN=1;
else TDIN=0;
cmd<<=1;
TCLK=0;
SpiDelay(1);
TCLK=1; //éÏéyÑØóDD§
}
}
unsigned int ADS7843_Read (unsigned char CMD )
{
u8 count=0;
u16 Num=0;
TCLK=0; //Ïèà-μíê±Öó
TDIN=0; //à-μíêy¾YÏß
TCS=0; //Ñ¡ÖD′¥ÃtÆáIC
ADS7843_Write(CMD);//·¢ËíÃüáî×Ö
SpiDelay(6);//ADS7846μÄ×a»»ê±¼ä×î3¤Îa6us
TCLK=0;
SpiDelay(1);
TCLK=1; //¸ø1¸öê±Öó£¬Çå3yBUSY
SpiDelay(1);
TCLK=0;
for(count=0;count<16;count++)//¶á3ö16λêy¾Y,Ö»óD¸ß12λóDD§
{
Num<<=1;
TCLK=0; //ϽμÑØóDD§
SpiDelay(1);
TCLK=1;
if(DOUT)Num++;
}
Num>>=4; //Ö»óD¸ß12λóDD§.
TCS=1; //êí·ÅƬѡ
return(Num);
}
/*******************************************************************************
* Function Name : SPI_ReadWriteByte
* Description : SPI¶áD′ò»¸ö×Ö½ú£¨·¢Ëííê3éoó·μ»Ø±¾′Îí¨Ñ¶¶áè¡μÄêy¾Y£©
* Input : u8 TxData ′y·¢ËíμÄêy
* Output : None
* Return : u8 RxData êÕμ½μÄêy
*******************************************************************************/
u8 SPI1_ReadWriteByte(u8 TxData)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, TxData);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
}
/******************************************************
¶á′¥ÃtÆá X
******************************************************/
u16 TPReadX(void) //16λêy¾Y′|àí
{
u16 x=0;
TCS=0;
SpiDelay(10);//΢Ãëus,êÖ2áéÏòaÇó200ns
x=ADS7843_Read(0xD0);
x<<=8;
x+=ADS7843_Read(0xD0);
SpiDelay(10);
TCS=1; //êy¾Y¶áè¡íê±Ï
SpiDelay(10);
x = x>>3;
x&=0xfff;
return (x);
}
/******************************************************
¶á′¥ÃtÆá Y
******************************************************/
u16 TPReadY(void)
{
u16 y=0;
TCS=0;
SpiDelay(10);
y=SPI1_ReadWriteByte(0x90);
y<<=8;
y+=SPI1_ReadWriteByte(0x90);
SpiDelay(10);
TCS=1;
SpiDelay(10);
y = y>>3;
y&= 0xfff;
return (y);
}
void touch_calibrate_two(void)//á½μãD£×¼oˉêy
{
TouchValue *TpCali;
ili9320_Clear(0x0000);
touch_calibrate_point=1;
touch_calibrate_enable=1;
while (touch_calibrate_enable)
{
while(touch_calibrate_point==1)
{
/*»-×óéϽÇμúò»¸öμã*/
ili9320_SetPoint(10,10,0xf800);//»-μúò»¸öμã
PutString(60,140," lease Adjust!",0xffff,0x0000);
if(PEN==0)
{
touch_calibrate_point=2;
Delay(120);
TpCali=GetXY();
x1=TpCali->x;
y1=TpCali->y;
}
}
Delay(1000);
while(touch_calibrate_point==2)
{
/*×óϽŻ-μú¶t¸öμã*/
ili9320_SetPoint(470,310,0xf800);//»-μú¶t¸öμã
if(PEN==0)
{
touch_calibrate_point=3;
Delay(120);
TpCali=GetXY();
x2=TpCali->x;
y2=TpCali->y;
}
}
Delay(1000);
while(touch_calibrate_point==3)
{
touch_calibrate_point=1;
touch_calibrate_enable=0;
fTouchKx=(float)(460)/(x2-x1);
fTouchbx=(10+470)/2-fTouchKx*(x1+x2)/2;
fTouchKy=(float)(300)/(y2-y1);
fTouchby=(10+310)/2-fTouchKy*(y1+y2)/2; //¼ÆËãÎïàí×ø±êμ½ÏñËØ×ø±ê±ä»»μÄÏμêy
ili9320_Clear(0x0000);
PutString(60,140,"Adjust OK!",0xffff,0x0000);//D£×¼íê3é
Delay(1000);
}
}
}
void touch_calibrate_three(void)//èyμãD£×¼oˉêy
{
TouchValue *TpCali;
ili9320_Clear(0x0000);
touch_calibrate_point=1;
touch_calibrate_enable=1;
while (touch_calibrate_enable)
{
while(touch_calibrate_point==1)
{
/*»-×óéϽÇμúò»¸öμã*/
ili9320_SetPoint(10,10,0xf800);//»-μúò»¸öμã
PutString(60,140," lease Adjust!",0xffff,0x0000);
if(PEN==0)
{
touch_calibrate_point=2;
Delay(120);
TpCali=GetXY();
x1=TpCali->x;
y1=TpCali->y;
}
}
Delay(1000);
while(touch_calibrate_point==2)
{
/*×óϽŻ-μú¶t¸öμã*/
ili9320_SetPoint(470,310,0xf800);//»-μú¶t¸öμã
if(PEN==0)
{
touch_calibrate_point=3;
Delay(120);
TpCali=GetXY();
x2=TpCali->x;
y2=TpCali->y;
}
}
Delay(1000);
while(touch_calibrate_point==3)
{
ili9320_SetPoint(10,310,0xf800);//»-μúèy¸öμã
if(PEN==0)
{
touch_calibrate_point=4;
Delay(120);
TpCali=GetXY();
x3=TpCali->x;
y3=TpCali->y;
}
}
Delay(1000);
while(touch_calibrate_point==4)
{
touch_calibrate_point=1;
touch_calibrate_enable=0;
A=(float)((XL1-XL3)*(y2-y3)-(XL2-XL1)*(y1-y3))/((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3));
B=(float)((x1-x3)*(XL2-XL3)-(XL1-XL3)*(x2-x3))/((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3));
C=(float)(y1*(x3*XL2-x2*XL3)+y2*(x1*XL3-x3*XL1)+y3*(x2*XL1-x1*XL2))/((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3));
D=(float)((YL1-YL3)*(y2-y3)-(YL2-YL3)*(y1-y3))/((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3));
E=(float)((x1-x3)*(YL2-YL3)-(YL1-YL3)*(x2-x3))/((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3));
F=(float)(y1*(x3*YL2-x2*YL3)+y2*(x1*YL3-x3*YL1)+y3*(x2*YL1-x1*YL2))/((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3));
ili9320_Clear(0x0000);
PutString(200,200,"Adjust OK!",0xffff,0x0000);//D£×¼íê3é
Delay(1000);
}
}
}
TouchValue *GetXY(void)//¶áè¡XY×ø±ê2¢×÷ÂË2¨′|àí
{
int m0,m1,m2,temp[3];
uint8_t count=0;
static TouchValue tp;
int buffer[2][9]={{0},{0}}; /* ×ø±êXoíY½øDD¶à′Î2éÑù */
int THRESHOLD=10; /*2îÖμÃÅÏTÖμ,ì«D¡μÄ»°Ëä軾«è·£¬μ«êÇÄÑD£Ñé3é1|*/
do /* Ñ-»·2éÑù9′Î */
{
buffer[0][count]=TPReadX();
buffer[1][count]=TPReadY();
count++;
}
while((1-GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4))&& count<9); /* μ±óû§μã»÷′¥ÃtÆáê±TP_INT_IN»á±»ÖÃμí */
if(count==9) /* 3é1|2éÑù9′Î,½øDDÂË2¨ */
{
/* Îa¼õéùÔËËãá¿,·Ö±e·Ö3×éè¡Æ½¾ùÖμ ,Îïàí×ø±êxμÄ′|àí*/
temp[0]=(buffer[0][0]+buffer[0][1]+buffer[0][2])/3;
temp[1]=(buffer[0][3]+buffer[0][4]+buffer[0][5])/3;
temp[2]=(buffer[0][6]+buffer[0][7]+buffer[0][8])/3;
/* ¼ÆËã3×éêy¾YμÄ2îÖμ */
m0=temp[0]-temp[1];
m1=temp[1]-temp[2];
m2=temp[2]-temp[0];
/* ¶ÔéÏêö2îÖμ衾ø¶ÔÖμ */
m0=m0>0?m0 -m0);
m1=m1>0?m1 -m1);
m2=m2>0?m2 -m2);
/* ÅD¶Ï¾ø¶Ô2îÖμêÇ·ñ¶¼3¬1y2îÖμÃÅÏT£¬èç1ûÕa3¸ö¾ø¶Ô2îÖμ¶¼3¬1yÃÅÏTÖ죬ÔòÅD¶¨Õa′Î2éÑùμãÎaò°μã,Åׯú2éÑùμ㣬2îÖμÃÅÏTè¡Îa2 */
if( m0>THRESHOLD && m1>THRESHOLD && m2>THRESHOLD ) return 0;
/* ¼ÆËãËüÃÇμÄÆ½¾ùÖ죬í¬ê±¸3Öμ¸øscreen */
if(m0<m1)
{
if(m2<m0)
tp.x=(temp[0]+temp[2])/2;
else
tp.x=(temp[0]+temp[1])/2;
}
else if(m2<m1)
tp.x=(temp[0]+temp[2])/2;
else
tp.x=(temp[1]+temp[2])/2;
/* í¬éÏ ¼ÆËãYμÄÆ½¾ùÖμ */
temp[0]=(buffer[1][0]+buffer[1][1]+buffer[1][2])/3;
temp[1]=(buffer[1][3]+buffer[1][4]+buffer[1][5])/3;
temp[2]=(buffer[1][6]+buffer[1][7]+buffer[1][8])/3;
m0=temp[0]-temp[1];
m1=temp[1]-temp[2];
m2=temp[2]-temp[0];
m0=m0>0?m0 -m0);
m1=m1>0?m1 -m1);
m2=m2>0?m2 -m2);
if(m0>THRESHOLD&&m1>THRESHOLD&&m2>THRESHOLD) return 0;
if(m0<m1)
{
if(m2<m0)
tp.y=(temp[0]+temp[2])/2;
else
tp.y=(temp[0]+temp[1])/2;
}
else if(m2<m1)
tp.y=(temp[0]+temp[2])/2;
else
tp.y=(temp[1]+temp[2])/2;
return &tp; //·μ»Ø¾«è·μÄXYÎïàí×ø±ê£¡
}
return 0;
}
void Touch_Adjust(void)
{
u16 pos_temp[4][2];//×ø±ê»o′æ
u8 cnt=0;
u16 d1,d2;
u32 tem1,tem2;
float fac;
cnt=0;
ili9320_Clear(0x0000);
ili9320_SetPoint(20,20,0xffff);//»-μúò»¸öμã
ili9320_Delay(1000);
while(1)
{
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4)==0)//¿éòÔÅD¶Ï¶á衱êÖD¶Ïòy½Å C4½Å
{
pos_temp[cnt][0]=TPReadX();
pos_temp[cnt][1]=TPReadY();
cnt++;
ili9320_Delay(100);
switch(cnt)
{
case 1:
ili9320_Clear(0x0000);
ili9320_Delay(1000);
ili9320_SetPoint(460,20,0xffff);//»-μú¶t¸öμã
break;
case 2:
ili9320_Clear(0x0000);
ili9320_Delay(1000);
ili9320_SetPoint(20,300,0xffff);//»-μúèy¸öμã
break;
case 3:
ili9320_Clear(0x0000);
ili9320_Delay(1000);
ili9320_SetPoint(460,300,0xffff);//»-μúËĸöμã
break;
case 4: //ËĸöμãòѾ-è«2¿μÃμ½
tem1=abs(pos_temp[0][0]-pos_temp[1][0]);//x1-x2
tem2=abs(pos_temp[0][1]-pos_temp[1][1]);//y1-y2
tem1*=tem1;
tem2*=tem2;
d1=sqrt(tem1+tem2);//μÃμ½1,2μľààë
tem1=abs(pos_temp[2][0]-pos_temp[3][0]);//x3-x4
tem2=abs(pos_temp[2][1]-pos_temp[3][1]);//y3-y4
tem1*=tem1;
tem2*=tem2;
d2=sqrt(tem1+tem2);//μÃμ½3,4μľààë
fac=(float)d1/d2;
// if(fac<0.6||fac>1.4||d1==0||d2==0)//2»oϸñÇé¿ö£¡
// {
// cnt=0;
// ili9320_Clear(0x0000);
// ili9320_SetPoint(20,20,0xffff);//»-μúò»¸öμã
// continue;
// }
// tem1=abs(pos_temp[0][0]-pos_temp[2][0]);//x1-x3
// tem2=abs(pos_temp[0][1]-pos_temp[2][1]);//y1-y3
// tem1*=tem1;
// tem2*=tem2;
// d1=sqrt(tem1+tem2);//μÃμ½1,3μľààë
//
// tem1=abs(pos_temp[1][0]-pos_temp[3][0]);//x2-x4
// tem2=abs(pos_temp[1][1]-pos_temp[3][1]);//y2-y4
// tem1*=tem1;
// tem2*=tem2;
// d2=sqrt(tem1+tem2);//μÃμ½2,4μľààë
// fac=(float)d1/d2;
// if(fac<0.6||fac>1.4)//2»oϸñ
// {
// cnt=0;
// ili9320_Clear(0x0000);
// ili9320_SetPoint(20,20,0xffff);//»-μúò»¸öμã
// continue;
// }//Õyè·£¡
//
// //¶Ô½ÇÏßÏàμè
// tem1=abs(pos_temp[1][0]-pos_temp[2][0]);//x1-x3
// tem2=abs(pos_temp[1][1]-pos_temp[2][1]);//y1-y3
// tem1*=tem1;
// tem2*=tem2;
// d1=sqrt(tem1+tem2);//μÃμ½1,4¾ààë
//
// tem1=abs(pos_temp[0][0]-pos_temp[3][0]);//x2-x4
// tem2=abs(pos_temp[0][1]-pos_temp[3][1]);//y2-y4
// tem1*=tem1;
// tem2*=tem2;
// d2=sqrt(tem1+tem2);//μÃμ½2,3¾ààë
// fac=(float)d1/d2;
// if(fac<0.6||fac>1.4)//2»oϸñ
// {
// cnt=0;
// ili9320_Clear(0x0000);
// ili9320_SetPoint(20,20,0xffff);//»-μúò»¸öμã
// continue;
// }//Õyè·
// //¼ÆËã½á1û£¡
xfac=(float)(240-40)/(pos_temp[1][0]-pos_temp[0][0]);//μÃμ½xfac
xoff=(240-xfac*(pos_temp[1][0]+pos_temp[0][0]))/2;//μÃμ½xoff
yfac=(float)(320-40)/(pos_temp[2][1]-pos_temp[0][1]);//μÃμ½yfac
yoff=(320-yfac*(pos_temp[2][1]+pos_temp[0][1]))/2;//μÃμ½yoff
ili9320_Clear(0x0000);
PutString(120,120,"Touch Screen Adjust OK!",0x0000,0xffff);//D£×¼íê3é
ili9320_Delay(1000);
ili9320_Clear(0x9876);
return;//D£×¼3é1|£¡
}
}
}
}
[/mw_shl_code] |
最佳答案
查看完整内容[请看2#楼]
是电阻触摸屏的问题。换个好的触摸屏就好了。
我之前用的是个7寸的,自带的那个触摸屏实在是不怎么样,后来换个好的电阻触摸屏以后,可以和电容屏媲美了。
|