OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2522|回复: 1

触摸屏可以校准,划线时偏移很远,程序如下,请帮忙分析,谢谢!

[复制链接]

2

主题

19

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
314
金钱
314
注册时间
2014-8-10
在线时间
89 小时
发表于 2015-10-31 17:33:22 | 显示全部楼层 |阅读模式
5金钱
[mw_shl_code=c,true]#include "xpt2046.h" /* MOSI----PC3 ???ì??????50m MISO----PC2 ???­???? SCK-----PC0 ???ì??????50m CS------PC13 ???ì??????50m PEN-----PC1 ???­???? #define XPT_CS PCout(13) #define XPT_SCK PCout(0) #define XPT_MOSI PCout(3) #define XPT_MISO PCin(2) #define XPT_PEN PCin(1) */ float Kx,Ky; int Bx,By; void XPT_Init(void) { RCC->APB2ENR|=1<<4;//????GPIOC???±?? RCC->APB2ENR|=1<<0;//?????¨?ú?±?? GPIOC->CRL&=0XFFFF0000; //MOSI??MISO??SCK ??PEN GPIOC->CRL|=0X00003883; GPIOC->ODR|=(1<<3)|(1<<1)|(1<<0)|(1<<2); GPIOC->CRH&=0XFF0FFFFF; GPIOC->CRH|=0X00300000; GPIOC->ODR|=1<<13; //CS???­?????? } //·???×??? void XPT_SPI_Send_Byte(u8 data) { u8 i=0; for(i=0;i<8;i++) { XPT_SCK = 1; XPT_SCK = 0; if(data&0x80)//?ù????·????????????­??/?­???????? { XPT_MOSI = 1; } else { XPT_MOSI = 0; } XPT_SCK = 1; data = data<<1; } } //????×??? /* STM32????XPT????????XPT????????STM32???ù */ u8 XPT_SPI_Receive_Byte(void) { u8 i; u8 data; for(i=0;i<8;i++) { XPT_SCK = 1; XPT_SCK = 0; XPT_SCK = 1; data = data<<1; if(XPT_MISO==1) { data = data|0x01; } } return data; } /* ????X×?±ê?? */ u16 Get_XPT_Xval(void) { u16 val; u8 valh,vall; XPT_CS=0; XPT_SPI_Send_Byte(0x90); delay_us(6); XPT_SCK = 0; delay_us(1); XPT_SCK = 1; delay_us(1); XPT_SCK = 0; valh=XPT_SPI_Receive_Byte(); vall=XPT_SPI_Receive_Byte(); XPT_CS = 1; val=(valh<<8)|vall; return (val>>4);//???§??????12?? } /* ????Y×?±ê?? */ u16 Get_XPT_Yval(void) { u16 val; u8 valh,vall; XPT_CS=0; XPT_SPI_Send_Byte(0xd0); delay_us(6); XPT_SCK = 0; delay_us(1); XPT_SCK = 1; delay_us(1); XPT_SCK = 0; valh=XPT_SPI_Receive_Byte(); vall=XPT_SPI_Receive_Byte(); XPT_CS = 1; val=(valh<<8)|vall; return (val>>4);//???§??????12?? } /* ????????ADC???????????????????????í?????¨?????ò???¨ ????????10???????????ò?????·???÷2?????????ó???????ù ?????°?????ò??????×é?ó?? */ void Get_XPT_XY_Average(u16 *xval,u16 *yval) { u8 i,j,k; u16 Xtemp[10],Ytemp[10],c; u32 sumx,sumy; for(i=0;i<10;i++) { Xtemp = 4096-Get_XPT_Xval(); Ytemp = 4096-Get_XPT_Yval(); } //???ò for(j=0;j<10;j++) //?è???­?·??????10 { for(k=9;k>j;k--) //??×??ó?????????????°??????±??? { if(Xtemp[k]<Xtemp[k-1]) //±??°?????????ò???????? ????X?á????×é { c = Xtemp[k-1]; Xtemp[k-1] = Xtemp[k]; Xtemp[k] = c; } if(Ytemp[k]<Ytemp[k-1]) //±??°?????????ò???????? ????Y?á????×é { c = Ytemp[k-1]; Ytemp[k-1] = Ytemp[k]; Ytemp[k] = c; } } } sumx = sumy = 0;//?ò?? sumx??sumy·??????????????????ú???????¨????????0??×÷ for(i=2;i<8;i++) //?????·???÷???? { sumx = sumx + Xtemp; sumy = sumy + Ytemp; } *xval = sumx/6; *yval = sumy/6; // printf("\r\n%d %d",sumx/6,sumy/6); } /* ??·????????ú??×? ?®×???+?? */ void LCD_Touch_Ico(u16 xpos,u16 ypos,u32 color) { u16 i; for(i=0;i<30;i++) //?­?®×????ò30??????????15???????????ò·??ò??????30 { LCD_DrawPoint(xpos-15+i,ypos,color); LCD_DrawPoint(xpos,ypos-15+i,color); } draw_circle(xpos,ypos,8,color,0); //?­?? } /* ?è?¨??×?×?±ê */ void LCD_Touch_Adjust(void) { u8 i; u16 LCD_Pos[4][2]= //LCD??×?±ê {20,20, 220,20, 20,300, 220,300 }; u16 Touch_Pos[4][2]; //??·?????????×?±ê??×é float len1 ,len2; u16 fw; // fw=At24c02_Read_One_Byte(16); if(fw!=0X4) { again: for(i=0;i<4;i++) { LCD_Touch_Ico(LCD_Pos[0],LCD_Pos[1],RED); while(XPT_PEN==1); //???????§???÷ Get_XPT_XY_Average(&Touch_Pos[0],&Touch_Pos[1]); while(XPT_PEN==0); //???????§?????? LCD_Touch_Ico(LCD_Pos[0],LCD_Pos[1],WHITE); //???????­???÷????±ê delay_ms(300); } //???é???????????????? //?®???à?? len1 = sqrt((Touch_Pos[1][0] - Touch_Pos[0][0])*(Touch_Pos[1][0] - Touch_Pos[0][0]) + (Touch_Pos[1][1] - Touch_Pos[0][1])*(Touch_Pos[1][1] - Touch_Pos[0][1])); len2 = sqrt((Touch_Pos[3][0] - Touch_Pos[2][0])*(Touch_Pos[3][0] - Touch_Pos[2][0]) + (Touch_Pos[3][1] - Touch_Pos[2][1])*(Touch_Pos[3][1] - Touch_Pos[2][1])); if((len1/len2<0.95)||(len1/len2>1.05)) { //???????? goto again; } //???±?à?? len1 = sqrt((Touch_Pos[2][0] - Touch_Pos[0][0])*(Touch_Pos[2][0] - Touch_Pos[0][0]) + (Touch_Pos[2][1] - Touch_Pos[0][1])*(Touch_Pos[2][1] - Touch_Pos[0][1])); len2 = sqrt((Touch_Pos[3][0] - Touch_Pos[1][0])*(Touch_Pos[3][0] - Touch_Pos[1][0]) + (Touch_Pos[3][1] - Touch_Pos[1][1])*(Touch_Pos[3][1] - Touch_Pos[1][1])); if((len1/len2<0.95)||(len1/len2>1.05)) { //???????? goto again; } //???????à?? len1 = sqrt((Touch_Pos[3][0] - Touch_Pos[0][0])*(Touch_Pos[3][0] - Touch_Pos[0][0]) + (Touch_Pos[3][1] - Touch_Pos[0][1])*(Touch_Pos[3][1] - Touch_Pos[0][1])); len2 = sqrt((Touch_Pos[2][0] - Touch_Pos[1][0])*(Touch_Pos[2][0] - Touch_Pos[1][0]) + (Touch_Pos[2][1] - Touch_Pos[1][1])*(Touch_Pos[2][1] - Touch_Pos[1][1])); if((len1/len2<0.95)||(len1/len2>1.05)) { //???????? goto again; } //??????×??????? Kx=((float)(LCD_Pos[1][0]-LCD_Pos[0][0]))/(Touch_Pos[1][0]-Touch_Pos[0][0]); Bx=LCD_Pos[0][0]-Kx * Touch_Pos[0][0]; Ky=((float)(LCD_Pos[2][1]-LCD_Pos[0][1]))/(Touch_Pos[2][1]-Touch_Pos[0][1]); By=LCD_Pos[0][1]-Ky * Touch_Pos[0][1]; At24c02_Write_Bytes(0,4,(u8 *)&Kx); At24c02_Write_Bytes(4,4,(u8 *)&Ky); At24c02_Write_Bytes(8,4,(u8 *)&Bx); At24c02_Write_Bytes(12,4,(u8 *)&By); At24c02_Write_One_Byte(16,0x4); } else { At24c02_Read_Len_Byte(0,4,(u8 *)&Kx); At24c02_Read_Len_Byte(4,4,(u8 *)&Ky); At24c02_Read_Len_Byte(8,4,(u8 *)&Bx); At24c02_Read_Len_Byte(12,4,(u8 *)&By); } // printf("\r\n%f %d",Kx,Bx); // printf("\r\n%f %d",Ky,By); } void Get_XPT_Adjust_XYval(u16 *xval,u16 *yval) { u16 xtemp,ytemp; Get_XPT_XY_Average(&xtemp,&ytemp);//??????????????????×?±ê *xval = Kx*xtemp + Bx; *yval = Ky*ytemp + By; }[/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code]

int main(void)
{

 u16 xval,yval;
 char buff[100];
 float val=0;
 Stm32_Clock_Init(9);
 delay_init(72);
 uart_init(72,115200);
 IIC_Init(); 
 Key_Init();
 LED_Init();
 LCD_Init();
 XPT_Init();
 val=KEY_Scan(0);
 LCD_Clear(WHITE);
 LCD_Touch_Adjust();
 Adc1_Init();
 
   while (1)
   {
  
//  val=Get_Adc1_Ch1_Val();
//  printf("\r\n%f",val);
  
  // delay_ms(50);
//   sprintf(buff,"%.6f",val);
//   lcd_dis_string8x16(100,100,buff,WHITE,BLACK);
  
      if(XPT_PEN==0)
      {

        Get_XPT_Adjust_XYval(&xval,&yval);
        LCD_DrawPoint(xval,yval,RED);
        LCD_DrawPoint(xval+1,yval,RED);
        LCD_DrawPoint(xval,yval+1,RED);
        LCD_DrawPoint(xval+1,yval+1,RED);
   printf("\r\n%d %d",xval,yval);
       }
//     if(val==KEY0_PRES)
//   {
//   LCD_Clear(WHITE) ;

//    }
    }
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

12

主题

336

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1576
金钱
1576
注册时间
2015-8-9
在线时间
625 小时
发表于 2015-10-31 22:24:07 | 显示全部楼层
其实这样很难看出问题...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-19 17:13

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表