📄 tp.c
字号:
/*********************************************************************************************
* File: tp.c
* Author: embest
* Desc: LCD touch screen control function
* History:
*********************************************************************************************/
/*--- include files ---*/
#include <string.h>
#include "def.h"
#include "44b.h"
#include "44blib.h"
#include "tp.h"
#include "lcd.h"
volatile int CheckTSP;
volatile int isTSPint = 0;
void Vxy_lcd(int tx,int ty, int hpad);
void TS_Test(void)
{
int i = 0;
//Lcd_TC();
TS_init();
Check_Sel();
Uart_Printf("\n Pixel: 320x240. Coordinate: (0,0) - (320,240)\n");
Uart_Printf("\nLCD TouchScreen Test Example(please touch LCD screen)\n");
Uart_Printf("\npress any key to exit...\n");
CLR_view();
LCD_view(vcol,vrow,BLACK,"Pixel: 320x240. Coordinate: (0,0) - (320,240)");
LCD_view(vcol,vrow+pad,BLACK,"TouchScreen Test Example(please touch LCD screen)");
LCD_view(vcol,vrow+pad*6,BLACK,"press any key to exit...");
isTSPint =0;
while(!(Uart_GetKey()|(key_read() != -1)))
{
if(isTSPint)
{
if (i>3) i = 0;
Vxy_lcd(Vx,Vy,i+2);
isTSPint = 0;
i++;
}
}
TS_close();
}
/*--- function code ---*/
/*********************************************************************************************
* name: TSInt
* func: TouchScreen interrupt handler function
* para: none
* ret: none
* modify:
* comment:
********************************************************************************************/
void TSInt(void)
{
int i;
char fail = 0;
ULONG tmp;
ULONG Pt[6];
// <X-Position Read>
// TSPX(GPE4_Q4(+)) TSPY(GPE5_Q3(-)) TSMY(GPE6_Q2(+)) TSMX(GPE7_Q1(-))
// 0 1 1 0
rPDATE=0x68;
rADCCON=0x1<<2; // AIN1
DelayTime(1000); // delay to set up the next channel
for( i=0; i<5; i++ )
{
rADCCON |= 0x1; // Start X-position A/D conversion
while( rADCCON & 0x1 ); // Check if Enable_start is low
while( !(rADCCON & 0x40) ); // Check ECFLG
Pt[i] = (0x3ff&rADCDAT);
}
// read X-position average value
Vx = (Pt[0]+Pt[1]+Pt[2]+Pt[3]+Pt[4])/5;
// <Y-Position Read>
// TSPX(GPE4_Q4(-)) TSPY(GPE5_Q3(+)) TSMY(GPE6_Q2(-)) TSMX(GPE7_Q1(+))
// 1 0 0 1
rPDATE=0x98;
rADCCON=0x0<<2; // AIN0
DelayTime(1000); // delay to set up the next channel
for( i=0; i<5; i++ )
{
rADCCON |= 0x1; // Start Y-position conversion
while( rADCCON & 0x1 ); // Check if Enable_start is low
while( !(rADCCON & 0x40) ); // Check ECFLG
Pt[i] = (0x3ff&rADCDAT);
}
// read Y-position average value
Vy = (Pt[0]+Pt[1]+Pt[2]+Pt[3]+Pt[4])/5;
if(!CheckTSP)
{
if(!((Vx < Xmin)|(Vx > Xmax)|(Vy < Ymin)|(Vy > Ymax))) // Is valid value?
{
Vx = 320*(Vx - Xmin)/(Xmax - Xmin); // X - position
//Uart_Printf("X-Posion[AIN1] is %04d ", Vx);
Vy = 240*(Vy - Xmin)/(Ymax - Ymin);
//Uart_Printf(" Y-Posion[AIN0] is %04d\n", Vy);
}
}
isTSPint += 1;
//Vxy_lcd(Vx,Vy);
rPDATE = 0xb8; // should be enabled
while(!(tmp = rPDATG & 4)); // delay to reset
rI_ISPC = BIT_EINT2; // clear pending_bit
}
/*********************************************************************************************
* name: TS_init
* func: initialize TouchScreen
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void TS_init(void)
{
/* enable interrupt */
rINTMOD=0x0;
rINTCON=0x1;
rI_ISPC |= BIT_EINT2; // clear pending_bit
// TSPX(GPE4_Q4(-)) TSPY(GPE5_Q3(-)) TSMY(GPE6_Q2(-)) TSMX(GPE7_Q1(+))
// 1 1 0 1
rPUPE = 0x0; // Pull up
rPDATE = 0xb8; // should be enabled
DelayTime(100);
rEXTINT |= 0x200; // falling edge trigger
pISR_EINT2=(int)isrEINT2; // set interrupt handler
//Init X Y rectangle
Xmax = 750; Xmin = 200;
Ymax = 620; Ymin = 120;
rCLKCON = 0x7ff8; // enable clock
rADCPSR = 0x1;//0x4; // A/D prescaler
rINTMSK =~(BIT_GLOBAL|BIT_EINT2);
}
/*********************************************************************************************
* name: TS_close
* func: close TouchScreen
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void TS_close(void)
{
/* Mask interrupt */
rINTMSK |=BIT_GLOBAL|BIT_EINT2;
pISR_EINT2 = (int)NULL;
}
void Lcd_TC(void)
{
#if 0
/* initial LCD controller */
Lcd_Init();
/* clear screen */
Lcd_Clr();
#endif
/* draw rectangle pattern */
Lcd_Draw_Box(0,0,80,60,15);
Lcd_Draw_Box(80,0,160,60,15);
Lcd_Draw_Box(160,0,240,60,15);
Lcd_Draw_Box(240,0,320,60,15);
Lcd_Draw_Box(0,60,80,120,15);
Lcd_Draw_Box(80,60,160,120,15);
Lcd_Draw_Box(160,60,240,120,15);
Lcd_Draw_Box(240,60,320,120,15);
Lcd_Draw_Box(0,120,80,180,15);
Lcd_Draw_Box(80,120,160,180,15);
Lcd_Draw_Box(160,120,240,180,15);
Lcd_Draw_Box(240,120,320,180,15);
Lcd_Draw_Box(0,180,80,240,15);
Lcd_Draw_Box(80,180,160,240,15);
Lcd_Draw_Box(160,180,240,240,15);
Lcd_Draw_Box(240,180,320,240,15);
/* output ASCII symbol */
Lcd_DspAscII6x8(37,26,BLACK,"0");
Lcd_DspAscII6x8(117,26,BLACK,"1");
Lcd_DspAscII6x8(197,26,BLACK,"2");
Lcd_DspAscII6x8(277,26,BLACK,"3");
Lcd_DspAscII6x8(37,86,BLACK,"4");
Lcd_DspAscII6x8(117,86,BLACK,"5");
Lcd_DspAscII6x8(197,86,BLACK,"6");
Lcd_DspAscII6x8(277,86,BLACK,"7");
Lcd_DspAscII6x8(37,146,BLACK,"8");
Lcd_DspAscII6x8(117,146,BLACK,"9");
Lcd_DspAscII6x8(197,146,BLACK,"A");
Lcd_DspAscII6x8(277,146,BLACK,"B");
Lcd_DspAscII6x8(37,206,BLACK,"C");
Lcd_DspAscII6x8(117,206,BLACK,"D");
Lcd_DspAscII6x8(197,206,BLACK,"E");
Lcd_DspAscII6x8(277,206,BLACK,"F");
//Lcd_Dma_Trans();
Delay(400);
}
/*********************************************************************************************
* name: DesignREC
* func: confirm the coordinate rang
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void DesignREC(ULONG tx, ULONG ty)
{
int tm;
Vxy_lcd( tx, ty, 5); // first
isTSPint = 0;
while(!isTSPint);
Vxy_lcd( Vx, Vy, 6); // second
// Vx,Vy --- as X,Y max
if(Vx < tx )
{
tm = tx; tx = Vx; Vx = tm; // tx as Xmin
}
if(Vy < ty )
{
tm = ty; ty = Vy; Vy = tm; // ty as Ymin
}
Xmax = Vx; Xmin = tx;
Ymax = Vy; Ymin = ty;
}
void Check_Sel(void)
{
int yn;
do{
rINTMSK |=BIT_GLOBAL|BIT_EINT2;
Uart_Printf("\n\r Touch Screen coordinate:\n");
Uart_Printf(" (Xmin,Ymin) is :(%04d,%04d)\n",Xmin,Ymin);
Uart_Printf(" (Xmax,Ymax) is :(%04d,%04d)\n",Xmax,Ymax);
Uart_Printf("\n To use current settings. Press any key. ");
Uart_Printf("\n\n\r Want to Set Again(Y --yes)? ");
CLR_view();
sprintf(buf, "Touch Screen coordinate:");
LCD_view(vcol,vrow,BLACK,buf);
sprintf(buf, " (Xmin,Ymin) is :(%04d,%04d)\n",Xmin,Ymin);
LCD_view(vcol,vrow+pad,BLACK,buf);
sprintf(buf, " (Xmax,Ymax) is :(%04d,%04d)\n",Xmax,Ymax);
LCD_view(vcol,vrow+pad*2,BLACK,buf);
sprintf(buf, " To use current settings. Press any key. ");
LCD_view(vcol,vrow+pad*3,BLACK,buf);
sprintf(buf, " Want to Set Again(E --yes)? ");
LCD_view(vcol,vrow+pad*4,BLACK,buf);
//yn = Uart_Getch();
while(!(yn = Uart_GetKey()))
{
yn = key_read();
if(yn != -1) break;
}
rI_ISPC = BIT_EINT2; // clear pending_bit
rINTMSK =~BIT_GLOBAL;
rINTMSK &= ~BIT_EINT2;
if((yn == 0x59)|(yn == 0x79)) Uart_SendByte(yn);
if((yn == 0xE )|(yn == 0x59)|(yn == 0x79)|(yn == 0x0D))
{
Uart_Printf("\n\n Touch TSP's Cornor to ensure Xmax,Ymax,Xmax,Xmin");
sprintf(buf, " Touch TSP's Cornor to ensure Xmax,Ymax,Xmax,Xmin");
LCD_view(vcol,vrow,BLACK,buf);
isTSPint = 0;
CheckTSP = 1; // mask to check
/*----------- check to ensure Xmax Ymax Xmin Ymin ------------*/
while(!isTSPint);
DesignREC(Vx,Vy);
CheckTSP = 0;
}else break;
}while(1);
}
void Vxy_lcd(int tx,int ty, int hpad)
{
Uart_Printf("\n\r User touch coordinate(X,Y) is :(%04d,%04d)",tx,ty);
sprintf(buf, "User touch coordinate(X,Y) is :(%04d,%04d)",tx,ty);
LCD_view(vcol,vrow+pad*hpad,BLACK,buf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -