最近做项目,碰到写触摸屏的校准和去抖,在网上找了找都没有找到,憋了很长时间,在同事和经理的帮助下今天终于完美的解决了,完成的那一刻很激动,这是毕业半个月后攻克的最难得问题(对于我来说).下面具体说说是怎么实现的.
void screen_adjust(void)
{
uint8_t n = 1,k = 0,n1 = 0,n2 = 0,n3 = 0,n4 = 0;
// float tpkbuf[4],tpbbuf[4];
float lcdx = 0,lcdy = 0;
float AA[2],BB[2],CC[2],DD[2];
float AA_tpx,AA_tpy,BB_tpx,BB_tpy,CC_tpx,CC_tpy,DD_tpx,DD_tpy;
float Atp[2],Btp[2],Ctp[2],Dtp[2];
uint16_t quit_location[4];
// quit_position = (uint16_t *)malloc(sizeof(uint16_t)*2);
Active_Window(0,319,0,239);
Chk_Busy();
Memory_Clear_with_Font_BgColor();//8E中的0位为1 , 以文字背景色清除
Chk_Busy();
Memory_Clear();//开始清除
Chk_Busy();
Draw_cross(n);//画四个十字坐标
while(1)
{
TP_enable();//开启触摸屏中断
if(wipe_shake())//是否有触摸,如果有触摸为真且把值存放到tpx和tpy中
{
wipe_shake_count();
TP_disable();
clear_lcd_position(&lcd);
// coordinate_to_pixel(&lcd);
lcd[0] = 320 - 320*tpx/1024;
lcd[1] = 240*tpy/1024;
if(1 == n)
{
if(in_scope(lcd,gImage_cross1_value))
{
TP_disable();
buzzer300();
n++;
Draw_cross(2);
AA[0] = tpx;
AA[1] = tpy;
n1++;
continue;
}
}
if(2 == n)
{
if(in_scope(lcd,gImage_cross2_value))
{
buzzer300();
n++;
Draw_cross(3);
BB[0] = tpx;
BB[1] = tpy;
n2++;
continue;
}
}
if(3 == n)
{
if(in_scope(lcd,gImage_cross3_value))
{
buzzer300();
n++;
Draw_cross(4);
CC[0] = tpx;
CC[1] = tpy;
n3++;
continue;
}
}
if(4 == n)
{
if(in_scope(lcd,gImage_cross4_value))
{
buzzer300();
n = 1;
Draw_cross(n);
DD[0] = tpx;
DD[1] = tpy;
n4++;
if(n1>=1 && n2>=1 && n3>= 1 && n4 >= 1)
{
break;
}
}
}
}
}
TP_disable();
Draw_cross(0);//最后点击完显示都是黑色的cross
//把四个点本来正常的tpx,tpy求出来
pixel_to_tp(25,25,&Atp);
pixel_to_tp(295,25,&Btp);
pixel_to_tp(295,215,&Ctp);
pixel_to_tp(25,215,&Dtp);
//count_ABCDEF(Atp,Btp,Ctp,AA,BB,CC);
/*
AA_tpx = (AA[0][0] + AA[1][0] + AA[2][0])/3;
AA_tpy = (AA[0][1] + AA[1][1] + AA[2][1])/3;
BB_tpx = (BB[0][0] + BB[1][0] + BB[2][0])/3;
BB_tpy = (BB[0][1] + BB[1][1] + BB[2][1])/3;
CC_tpx = (CC[0][0] + CC[1][0] + CC[2][0])/3;
CC_tpy = (CC[0][1] + CC[1][1] + CC[2][1])/3;
DD_tpx = (DD[0][0] + DD[1][0] + DD[2][0])/3;
DD_tpy = (DD[0][1] + DD[1][1] + DD[2][1])/3;
*/
AA_tpx = AA[0];
AA_tpy = AA[1];
BB_tpx = BB[0];
BB_tpy = BB[1];
CC_tpx = CC[0];
CC_tpy = CC[1];
DD_tpx = DD[0];
DD_tpy = DD[1];
lefttop_x = (AA_tpx + DD_tpx)/2; // 这里得到的值是模数转换后的值 数字值
rightbot_x = (CC_tpx + BB_tpx)/2; //
lefttop_y = (AA_tpy + BB_tpy)/2; //lefttop相当于A的位置 LCD像素的位置为25,25
rightbot_y = (CC_tpy + DD_tpy)/2; //rightbot相当于C的位置 LCD像素方向的坐标为295,215
// lefttop_x = AD_coordinate(ts_lefttop_x,1);
// lefttop_y = AD_coordinate(ts_lefttop_x,0);
// rightbot_x = AD_coordinate(ts_rightbot_x,1);
// rightbot_y = AD_coordinate(ts_rightbot_y,0);
//根据ts_lefttop 和 ts_rightbot的坐标可以校准了,把这个值应该写入flash中
tpkx = 280/(lefttop_x-rightbot_x);
tpbx = 20;
tpky = 200/(rightbot_y-lefttop_y);
tpby = 20;
//上面的几个值的意义,tpkx,tpky分别是x,y方向的斜率,tpbx,tpby分别是x,y方向的b值 求LCD像素方向的x,y要分别求每个方向的k,b 这是一个线性关系
RA_Button_create(140,100,40,50,150,110,"Quit",0,0);
// RA_Button_create(20,20,280,200,150,110,"Quit",0,0);
TP_enable();
//下面是对触摸是否准确进行测试用的
while(1)
{
TP_enable();
if(wipe_shake())
{
wipe_shake_count();
lcdx = 0;
lcdy = 0;
lcdx = 320 - (tpkx*(tpx-rightbot_x)+tpbx);
lcdy = tpky*(tpy-lefttop_y)+tpby;
//lcdx = tpkx*(tpx-rightbot_x)+tpbx;
//lcdy = tpky*(tpy-rightbot_x)+tpby;
// lcdx = 320-tpx*320/1024;
// lcdy = 320*tpy/1024;
buzzer300();
Text_Foreground_Color(0xE0);
Geometric_Coordinate(lcdx,lcdx+2,lcdy,lcdy+2);
Draw_square_fill();
TP_disable();
}
}
while(1)
{
TP_enable();
if(TP_XY())//是否有触摸,如果有触摸为真且把值存放到tpx和tpy中
{
// wipe_shake_count();
clear_lcd_position(&lcd);
// lcd[0] = 0;
// lcd[1] = 0;
TP_disable();
coordinate_to_pixel(&lcd);
// lcdx = tpkx*(tpx-rightbot_x)+tpbx;
// lcdy = tpky*(tpy-lefttop_y)+tpby;
// pixel_to_coordinate(gImage_quit_value,&quit_location);//获取点击位置的坐标
// tpx = A*tpx1 + B*tpy1 + C;
// tpy = D*tpx1 + E*tpy1 + F;
if(in_scope(lcd,gImage_quit_value))
{
buzzer300();
break;
}
}
}
}
uint8_t wipe_shake(void)//去抖动函数,关键点是要去除前十次的值和最后一次的值,你可以用串口输出值看看,前面的值比较不稳定,而且是间隔10ms采样一次
{
uint8_t ss,i = 0,j = 0,k = 0;
if(is_touch())
{
tpx=0;
tpy=0;
}
// mSDelay(100);
for(ss=0;ss<20;ss++)
{
tpx = 0;
tpy = 0;
if(is_touch())
{
i++;
tpy_array[j++]=tpy;
tpx_array[k++]=tpx;
}
else
{
break;
}
mSDelay(10);
}
TP_disable();
tpx = 0;
tpy = 0;
if(i == 20)
{
return 1;
}
return 0;
}
void wipe_shake_count(void)
{
uint32_t sumx = 0,sumy = 0;
uint8_t times = 1;
for(times = 10; times<19 ;times++)
{
sumx = sumx + tpx_array[times];
sumy = sumy + tpy_array[times];
}
tpx = sumx/9;
tpy = sumy/9;
}
具体代码都贴出来了,有什么不懂的可以留言
文章评论(0条评论)
登录后参与讨论