大家好,在这学生都要忙着放假过年的日子里,我的DIY设计是用STM32F3的开发板驱动了一个7寸的电阻触摸屏。
下面给大家看下基本程序
void Drow_Touch_Point(u16 x,u16 y)
{
LCD_DrawLine(x-12,y,x+13,y);
LCD_DrawLine(x,y-12,x,y+13);
LCD_DrawPoint(x+1,y+1);
LCD_DrawPoint(x-1,y+1);
LCD_DrawPoint(x+1,y-1);
LCD_DrawPoint(x-1,y-1);
Draw_Circle(x,y,6);
}
void Draw_Big_Point(u16 x,u16 y)
{
LCD_DrawPoint(x,y);//?DD?μ?
LCD_DrawPoint(x+1,y);
LCD_DrawPoint(x,y+1);
LCD_DrawPoint(x+1,y+1);
}
void Refreshes_Screen(void)
{
ssd1963_Clear(BLACK);//???á
WriteString(776,0,"RST",BLUE,BLACK);
}
void Convert_Pos(void)
{
if(Read_ADS2(&Pen_Point.X,&Pen_Point.Y))
{
Pen_Point.X0=Pen_Point.xfac*Pen_Point.X+Pen_Point.xoff;
Pen_Point.Y0=Pen_Point.yfac*Pen_Point.Y+Pen_Point.yoff;
}
}
void Touch_Adjust(void)
{
u16 pos_temp[4][2];
u8 cnt=0;
u16 d1,d2;
u32 tem1,tem2;
float fac;
cnt=0;
ssd1963_Clear(BLACK);
Drow_Touch_Point(20,20);
Pen_Point.Key_Sta=Key_Up;
Pen_Point.xfac=0;
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:
ssd1963_Clear(WHITE);
Drow_Touch_Point(780,20);
break;
case 2:
ssd1963_Clear(WHITE);
Drow_Touch_Point(20,460);
break;
case 3:
ssd1963_Clear(WHITE);
Drow_Touch_Point(780,460);
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);
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);
fac=(float)d1/d2;
if(fac<0.95||fac>1.05||d1==0||d2==0)
{
cnt=0;
ssd1963_Clear(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);
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);
fac=(float)d1/d2;
if(fac<0.95||fac>1.05)
{
cnt=0;
ssd1963_Clear(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);
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);
fac=(float)d1/d2;
if(fac<0.95||fac>1.05)
{
cnt=0;
ssd1963_Clear(WHITE);
Drow_Touch_Point(20,20);
continue;
}
Pen_Point.xfac=(long double)760/(pos_temp[1][0]-pos_temp[0][0]);
Pen_Point.xoff=(800-Pen_Point.xfac*(pos_temp[1][0]+pos_temp[0][0]))/2;
Pen_Point.yfac=(long double)440/(pos_temp[2][1]-pos_temp[0][1]);
Pen_Point.yoff=(480-Pen_Point.yfac*(pos_temp[2][1]+pos_temp[0][1]))/2;//μ?μ?yoff
ssd1963_Clear(BLACK);
WriteString(35,110,"Touch Screen Adjust OK!",RED,BLACK);
Delay1(20);
ssd1963_Clear(BLACK);
return;
}
}
}
}
void Touch_Init()
{
Touch_Configuration();
Read_ADS(&Pen_Point.X,&Pen_Point.Y);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOG, GPIO_PinSource7);
EXTI_InitStructure.EXTI_Line = EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_GenerateSWInterrupt(EXTI_Line7);
#ifdef ADJ_SAVE_ENABLE
if(Get_Adjdata())return;
else
{
ili9320_Clear(WHITE);//???á
Touch_Adjust();
Save_Adjdata();
}
Get_Adjdata();
#else
ssd1963_Clear(WHITE);
Touch_Adjust();
#endif
}
|