//触摸屏校准代码 //得到四个校准参数 void Touch_Adjust(void) { signed short pos_temp[4][2];//坐标缓存值 u8 cnt=0; u16 d1,d2; u32 tem1,tem2; float fac; cnt=0; POINT_COLOR=BLUE; BACK_COLOR =WHITE; LCD_Clear(WHITE);//清屏 POINT_COLOR=RED;//红色 LCD_Clear(WHITE);//清屏 Drow_Touch_Point(20,20);//画点1 Pen_Point.Key_Sta=Key_Up;//消除触发信号 Pen_Point.xfac=0;//xfac用来标记是否校准过,所以校准之前必须清掉!以免错误 while(1) { if(Pen_Point.Key_Sta==Key_Down)//按键按下了 { if(Read_TP_Once())//得到单次按键值 { pos_temp[cnt][0]=Pen_Point.X; pos_temp[cnt][1]=Pen_Point.Y; cnt++; } switch(cnt) { case 1: LCD_Clear(WHITE);//清屏 Drow_Touch_Point(220,20);//画点2 break; case 2: LCD_Clear(WHITE);//清屏 Drow_Touch_Point(20,300);//画点3 break; case 3: LCD_Clear(WHITE);//清屏 Drow_Touch_Point(220,300);//画点4 break; case 4: //全部四个点已经得到 //对边相等 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.95||fac>1.05||d1==0||d2==0)//不合格 { cnt=0; LCD_Clear(WHITE);//清屏 Drow_Touch_Point(20,20); ADJ_INFO_SHOW("ver fac is:"); LCD_ShowNum(40+24,40,pos_temp[0][0],4,16); //显示数值 LCD_ShowNum(40+24+80,40,pos_temp[0][1],4,16); //显示数值 LCD_ShowNum(40+24,60,pos_temp[1][0],4,16); //显示数值 LCD_ShowNum(40+24+80,60,pos_temp[1][1],4,16); //显示数值 LCD_ShowNum(40+24,80,pos_temp[2][0],4,16); //显示数值 LCD_ShowNum(40+24+80,80,pos_temp[2][1],4,16); //显示数值 LCD_ShowNum(40+24,100,pos_temp[3][0],4,16); //显示数值 LCD_ShowNum(40+24+80,100,pos_temp[3][1],4,16); //显示数值 //扩大100倍显示 LCD_ShowNum(40,140,fac*100,3,16);//显示数值,该数值必须在95~105范围之内. 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.95||fac>1.05)//不合格 { cnt=0; LCD_Clear(WHITE);//清屏 Drow_Touch_Point(20,20); ADJ_INFO_SHOW("hor fac is:"); LCD_ShowNum(40+24,40,pos_temp[0][0],4,16); //显示数值 LCD_ShowNum(40+24+80,40,pos_temp[0][1],4,16); //显示数值 LCD_ShowNum(40+24,60,pos_temp[1][0],4,16); //显示数值 LCD_ShowNum(40+24+80,60,pos_temp[1][1],4,16); //显示数值 LCD_ShowNum(40+24,80,pos_temp[2][0],4,16); //显示数值 LCD_ShowNum(40+24+80,80,pos_temp[2][1],4,16); //显示数值 LCD_ShowNum(40+24,100,pos_temp[3][0],4,16); //显示数值 LCD_ShowNum(40+24+80,100,pos_temp[3][1],4,16); //显示数值 //扩大100倍显示 LCD_ShowNum(40,140,fac*100,3,16);//显示数值,该数值必须在95~105范围之内. continue; }//正确了 //对角线相等 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.95||fac>1.05)//不合格 { cnt=0; LCD_Clear(WHITE);//清屏 Drow_Touch_Point(20,20); ADJ_INFO_SHOW("dia fac is:"); LCD_ShowNum(40+24,40,pos_temp[0][0],4,16); //显示数值 LCD_ShowNum(40+24+80,40,pos_temp[0][1],4,16); //显示数值 LCD_ShowNum(40+24,60,pos_temp[1][0],4,16); //显示数值 LCD_ShowNum(40+24+80,60,pos_temp[1][1],4,16); //显示数值 LCD_ShowNum(40+24,80,pos_temp[2][0],4,16); //显示数值 LCD_ShowNum(40+24+80,80,pos_temp[2][1],4,16); //显示数值 LCD_ShowNum(40+24,100,pos_temp[3][0],4,16); //显示数值 LCD_ShowNum(40+24+80,100,pos_temp[3][1],4,16); //显示数值 //扩大100倍显示 LCD_ShowNum(40,140,fac*100,3,16);//显示数值,该数值必须在95~105范围之内. continue; }//正确了 //计算结果 Pen_Point.xfac=(float)200/(pos_temp[1][0]-pos_temp[0][0]);//得到xfac Pen_Point.xoff=(240-Pen_Point.xfac*(pos_temp[1][0]+pos_temp[0][0]))/2;//得到xoff Pen_Point.yfac=(float)280/(pos_temp[2][1]-pos_temp[0][1]);//得到yfac Pen_Point.yoff=(320-Pen_Point.yfac*(pos_temp[2][1]+pos_temp[0][1]))/2;//得到yoff if(abs(Pen_Point.xfac)>2||abs(Pen_Point.yfac)>2)//触屏和预设的相反了. { cnt=0; LCD_Clear(WHITE);//清屏 Drow_Touch_Point(20,20); LCD_ShowString(35,110,"TP Need readjust!"); Pen_Point.touchtype=!Pen_Point.touchtype;//修改触屏类型. if(Pen_Point.touchtype)//X,Y方向与屏幕相反 { CMD_RDX=0X90; CMD_RDY=0XD0; }else //X,Y方向与屏幕相同 { CMD_RDX=0XD0; CMD_RDY=0X90; } delay_ms(500); continue; } POINT_COLOR=BLUE; LCD_Clear(WHITE);//清屏 LCD_ShowString(35,110,"Touch Screen Adjust OK!");//校正完成 delay_ms(500); LCD_Clear(WHITE);//清屏 return;//校正完成 } } } }
//触摸屏校准代码 //得到四个校准参数 void drv_touchAdjust(void) { uint16_t pos_temp[4][2];//坐标缓存值 uint8_t cnt = 0; uint16_t d1, d2; uint32_t tem1, tem2; float fac; cnt = 0; POINT_COLOR = BLUE; BACK_COLOR = WHITE; drv_lcdClear(WHITE);//清屏 POINT_COLOR = RED; //红色 drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(20, 20); //画点1 Pen_Point.Key_Sta = Pen_Up; //消除触发信号 Pen_Point.xfac = 0; //xfac用来标记是否校准过,所以校准之前必须清掉!以免错误 while(1) { if(Pen_Point.Key_Sta == Pen_Down) //按键按下了 { Pen_Point.Key_Sta = Pen_Up; if(Read_TP_Once())//得到单次按键值 { pos_temp[cnt][0] = Pen_Point.X; pos_temp[cnt][1] = Pen_Point.Y; cnt++; } switch(cnt) { case 1: drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(220, 20); //画点2 break; case 2: drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(20, 300); //画点3 break; case 3: drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(220, 300); //画点4 break; case 4: //全部四个点已经得到 //对边相等 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.95 || fac > 1.05 || d1 == 0 || d2 == 0) //不合格 { cnt = 0; drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(20, 20); 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.95 || fac > 1.05) //不合格 { cnt = 0; drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(20, 20); continue; }//正确了 //对角线相等 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.95 || fac > 1.05) //不合格 { cnt = 0; drv_lcdClear(WHITE);//清屏 Drow_Touch_Point(20, 20); continue; }//正确了 //计算结果 Pen_Point.xfac = (float)200 / (pos_temp[1][0] - pos_temp[0][0]); //得到xfac Pen_Point.xoff = (240 - Pen_Point.xfac * (pos_temp[1][0] + pos_temp[0][0])) / 2; //得到xoff Pen_Point.yfac = (float)280 / (pos_temp[2][1] - pos_temp[0][1]); //得到yfac Pen_Point.yoff = (320 - Pen_Point.yfac * (pos_temp[2][1] + pos_temp[0][1])) / 2; //得到yoff POINT_COLOR = BLUE; drv_lcdClear(WHITE);//清屏 drv_lcdShowString(35, 110, "Touch Screen Adjust OK!"); //校正完成 Delay_ms(1000); drv_lcdClear(WHITE);//清屏 return;//校正完成 } } } }