📄 tp.c
字号:
/*
程序更新:
* 日期:
2003-12
* 更新内容:
中断处理;
XY坐标计算;
DesignREC(tmp,Pt[5]);
*/
/*********************************************************************************************
* 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"
#define TSInt(void) user_handle(void)
extern void user_irq();
void DesignREC(ULONG tx, ULONG ty);
/*--- global variables ---*/
char xyTouch;
/*--- function declare ---*/
void TSInt(void);// __attribute__ ((interrupt ("IRQ")));
/*--- function code ---*/
/*********************************************************************************************
* name: TSInt
* func: TouchScreen interrupt handler function
* para: none
* ret: none
* modify:
* comment:
********************************************************************************************/
void TSInt(void)
{
char fail = 0;
int i;
ULONG tmp;
ULONG Pt[6];
/*--------------------- A/D转换 -------------------------------
// TSPX(GPE4) TSPY(GPE5) TSMY(GPE6) TSMX(GPE7)
// 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 A/D conversion
while( rADCCON & 0x1 ); // Check if Enable_start is low
while( !(rADCCON & 0x40) ); // Check ECFLG
Pt[i] = (0x3ff&rADCDAT);
}
Pt[5] = (Pt[0]+Pt[1]+Pt[2]+Pt[3]+Pt[4])/5;// read average value
tmp = Pt[5];
if(UserTouch ==0) /*----------- Xmax Ymax Xmin Ymin ------------*/
{
//TSPX(GPE4) TSPY(GPE5) TSMY(GPE6) TSMX(GPE7)
// 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 A/D conversion
while( rADCCON & 0x1 ); // Check if Enable_start is low
while( !(rADCCON & 0x40) ); // Check ECFLG
Pt[i] = (0x3ff&rADCDAT);
}
Pt[5] = (Pt[0]+Pt[1]+Pt[2]+Pt[3]+Pt[4])/5;// read average value
DesignREC(tmp,Pt[5]);/*----------- Xmax Ymax Xmin Ymin ------------*/
}else{ /*-------------- 用户TSP坐标输入 ---------------*/
/*---------------------- - position --------------------------*/
/*
* 程序更新:
*
* 日期:2003-12
*/
if((Pt[5] < Xmin)|(Pt[5] > Xmax)) // Is valid value?
{
fail =1;
}else{
//tmp = (Ymax - Pt[5])*240/(Ymax - Ymin); // Y - position
tmp = 320 - (320*(Xmax - Pt[5]))/(Xmax - Xmin); // X - position
}
/*----------------------------------------------------------------*/
/*--------------------- A/D转换 -------------------------------
// TSPX(GPE4) TSPY(GPE5) TSMY(GPE6) TSMX(GPE7)
// 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 A/D conversion
while( rADCCON & 0x1 ); // Check if Enable_start is low
while( !(rADCCON & 0x40) ); // Check ECFLG
Pt[i] = (0x3ff&rADCDAT);
}
Pt[5] = (Pt[0]+Pt[1]+Pt[2]+Pt[3]+Pt[4])/5;// read average value
/*---------------------- - position --------------------------*/
/*
* 程序更新:
*
* 日期:2003-12
*/
if((Pt[5] < Ymin)|(Pt[5] > Ymax)) // Is valid value?
{
fail = 1;
}else{
//Pt[5] = (Xmax - Pt[5])*320/(Xmax - Xmin); // X - position
Pt[5] = 240 - (240*(Ymax - Pt[5]))/(Ymax - Ymin); // Y - position
}
/*----------------------------------------------------------------*/
/*-------------- print X & Y coordinate -------------------------*/
if(fail)
{
//Uart_Printf(" Out of Place value! (%04d,%04d)",Pt[5],tmp); // Print & Bell
}else{
Uart_Printf("\n\rXmin = %04d, Ymin = %04d, Xmax = %04d, Ymax = %04d",Xmin,Ymin,Xmax,Ymax);
Uart_Printf("\n\r User touch coordinate(X,Y) is :");
Uart_Printf("(%04d,%04d)\n\n",tmp,Pt[5]);
Vx = tmp; // save return value
Vy = Pt[5];
}
/*------------------------------------------------------------------*/
}// end if(UserTouch ==0)
rPDATE = 0xb8; // should be enabled
DelayTime(3000); // delay to set up the next channel
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_ZDMA0; //clear pending
rI_ISPC |= BIT_EINT2; // clear pending_bit
//TSPX(GPE4) TSPY(GPE5) TSMY(GPE6) TSMX(GPE7)
// 0 1 1 0
rPUPE = 0x0; // Pull up
rPDATE = 0xb8; // should be enabled
DelayTime(100);
rEXTINT |= 0x200; // falling edge trigger
pISR_EINT2=(int)user_irq; // set interrupt handler
rCLKCON = 0x7ff8; // enable clock
rADCPSR = 0x10;//0x4; // A/D prescaler
rINTMSK =~(BIT_GLOBAL|BIT_EINT2);
//Init X Y rectangle
Xmax = 750; Xmin = 200;
Ymax = 620; Ymin = 120;
Vx = 0;
Vy = 0;
xyTouch = 0;
}
/*********************************************************************************************
* 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;
}
/*********************************************************************************************
* name: DesignREC
* func: confirm the coordinate rang
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void DesignREC(ULONG tx, ULONG ty)
{
int tm;
Uart_Printf("\n\r User touch coordinate(X,Y) is :");
Uart_Printf("(%04d",tx);
Uart_Printf(",%04d)\n",ty);
/*-----------------------------------------------------------
------------------------------------------------------------*/
if(xyTouch == 0)
{
Vx = tx; // Vx as Xmax
Vy = ty; // Vy as Ymax
xyTouch = 1;
}else{
if(Vx < tx )
{
tm = tx; tx = Vx; Vx = tm; // tx as Xmin
}
if(Vy < ty )
{
tm = ty; ty = Vy; Vy = tm; // ty as Ymin
}
/*-----------------------------------------------------------
最小XY轴向界定
-----------------------------------------------------------*/
if(tx > Xmin) tx = Xmin;
if(ty > Ymin) ty = Ymin;
if(Vx < Xmax) Vx = Xmax;
if(Vy < Ymax) Vy = Ymax;
/*-----------------------------------------------------------
坐标范围定位
-----------------------------------------------------------*/
Xmax = Vx; Xmin = tx;
Ymax = Vy; Ymin = ty;
xyTouch = 0;
UserTouch = 1;
}// end if(xyTouch == 0)
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -