⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 touch_screen.c

📁 ads下2410的测试程序
💻 C
字号:
/****************************************************************************
【文  件  名  称】Touch_Screen.c
【功  能  描  述】FFT-2410教学平台实验程序
【程  序  版  本】3.0
【创建及创建日期】傅立业公司/2005-XX-XX
【修改及修改日期】2005-5-23
****************************************************************************/
//头文件定义
#include "def.h"
#include "2410addr.h"
#include "mmu.h"

#include "config.h"
#include "board.h"
#include "Touch_Screen.h"
#include "LCD_LTS350Q1_PE1.h"

#define ADCPRS 39
#define	NULL1	(void *)0

#define GET_XY_TIMES 5	
volatile U16 TP_BUF[GET_XY_TIMES][2];

static volatile U32 PenDown = 0;

//*************************************************************
#define UD_SEN			(1 << 8)
#define DOWN_INT		(UD_SEN*0)
#define UP_INT			(UD_SEN*1)
#define YM_SEN			(1 << 7)
#define YM_HIZ			(YM_SEN*0)
#define YM_GND			(YM_SEN*1)
#define YP_SEN			(1 << 6)
#define YP_EXTVLT		(YP_SEN*0)
#define YP_AIN			(YP_SEN*1)
#define XM_SEN			(1 << 5)
#define XM_HIZ			(XM_SEN*0)
#define XM_GND			(XM_SEN*1)
#define XP_SEN			(1 << 4)
#define XP_EXTVLT		(XP_SEN*0)
#define XP_AIN			(XP_SEN*1)
#define XP_PULL_UP		(1 << 3)
#define XP_PULL_UP_EN	(XP_PULL_UP*0)
#define XP_PULL_UP_DIS	(XP_PULL_UP*1)
#define AUTO_PST		(1 << 2)
#define CONVERT_MAN		(AUTO_PST*0)
#define CONVERT_AUTO	(AUTO_PST*1)
#define XP_PST(x)		(x << 0)
#define	WAIT_INT_MODE	3

#define wait_down_int()	do { rADCTSC = DOWN_INT | XP_PULL_UP_EN | \
				XP_AIN | XM_HIZ | YP_AIN | YM_GND | \
				XP_PST(WAIT_INT_MODE); } while(0)
#define wait_up_int()	do { rADCTSC = UP_INT | XP_PULL_UP_EN | XP_AIN | XM_HIZ | \
				YP_AIN | YM_GND | XP_PST(WAIT_INT_MODE); } while(0)


/***************************************************************************
Function name: GetPenXY
Parameter    : void
Description  : 得到X通道,Y通道的值
Return	     : void
Argument     : 从ADCDTA0和ADCDATA1
Autor & date :
****************************************************************************/	
static __inline void GetPenXY(void)
{
	int i;
	for(i=0;i<GET_XY_TIMES;i++)
	{
		rADCTSC  = (1<<7)|(1<<6)|(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0);            
		rADCCON |= 0x1;             //Start Auto conversion
		while(rADCCON & 0x1);       //Check if Enable_start is low
		while(!(0x8000&rADCCON));   //Check ECFLG
		
		TP_BUF[i][0] = (0x3ff&rADCDAT0) * 10 ;
		TP_BUF[i][1] = (0x3ff&rADCDAT1) * 10 ;
	}
        
}

/***************************************************************************
Function name: Touch_Screen
Parameter    : void
Description  : 触摸屏中断处理函数
Return	     : void
Argument     : 
Autor & date :
****************************************************************************/
void __irq Touch_Screen( void )
{
    rINTSUBMSK |= (BIT_SUB_ADC | BIT_SUB_TC);     //Mask sub interrupt (ADC and TC) 
    
    if(!PenDown) {
		PenDown = 1;
//		printf("PEN down\n");

		//GetPenXY();		
		//wait_up_int();  	  
	} else {
		PenDown = 0;
		//printf("PEN up\n");
		wait_down_int();
	}
	
    rSUBSRCPND |= BIT_SUB_TC;
    rINTSUBMSK  =~(BIT_SUB_TC);   //Unmask sub interrupt (TC)     
    ClearPending(BIT_ADC);
}

/***************************************************************************
Function name: Touch_Screen_Init
Parameter    : void
Description  : 触摸屏初始化函数
Return	     : void
Argument     : 
Autor & date :
****************************************************************************/
void Touch_Screen_Init(void)
{
    rADCDLY = (30000);    // ADC Start or Interval Delay

    rADCCON = (1<<14)|(ADCPRS<<6)|(0<<3)|(0<<2)|(0<<1)|(0); 
  
	wait_down_int();

	rSUBSRCPND |= BIT_SUB_TC;
    rINTSUBMSK  =~(BIT_SUB_TC);
    
    pISR_ADC    = (unsigned)Touch_Screen;
    rINTMSK    &= ~(BIT_ADC);
    rINTSUBMSK &= ~(BIT_SUB_TC);
}

/***************************************************************************
Function name: Touch_Screen_Off
Parameter    : void
Description  : 禁止触摸屏中断与相关的ADC中断
Return	     : void
Argument     : 
Autor & date :
****************************************************************************/
void Touch_Screen_Off(void)
{
	rINTMSK    |= (BIT_ADC);
    rINTSUBMSK |= (BIT_SUB_TC);
}


//static U16 x_factor = 37 ;
//static U16 y_factor = 27 ;
static U16 x_factor = 11 ;
static U16 y_factor = 19 ;

static U16 x_offset = 2300 ;
static U16 y_offset = 9800 ;
static U16 x_inverse = 0 ;
static U16 y_inverse = 1 ;
static int calibrate = 0 ;

/***************************************************************************
Function name: CheckTouchPanelEvent
Parameter    : *x : 用来存放触摸屏到LCD转换的X坐标的值
			   *y : 用来存放触摸屏到LCD转换的Y坐标的值
			   *tm: 一个标志变量
Description  : 检测触摸屏事件,如果触摸屏被按下,获取X,Y通道的值,
			   并转换成对应的LCD的X,Y坐标
Return	     : 返回1或0,1: 有转换数据 0:无
Argument     : 
Autor & date :
****************************************************************************/
int CheckTouchPanelEvent(U16 *x, U16 *y, U32 *tm)
{
	int i;
	U32 x_tot = 0, y_tot = 0;

	if( !PenDown )
 		return 0;
 	
  	GetPenXY();
 //	DisableInt();
 	if(!PenDown)
	 	wait_down_int();
 	else {
		wait_up_int();		
	}
//EnableInt();

	Delay(1);

	if( !PenDown ) 
	{
   			return 0;
	}

   			
	for(i=0; i<GET_XY_TIMES; i++)
	{
		if( !TP_BUF[i][0] || !TP_BUF[i][1] )
			return -1;
		x_tot += TP_BUF[i][0] ;
		y_tot += TP_BUF[i][1] ;
	}
	
	*x = x_tot/GET_XY_TIMES;
	*y = y_tot/GET_XY_TIMES;
	
	if(calibrate)
		return 1;
//	printf("xxxx=%d,yyyy=%d\n", *x, *y);
	
	
	if(x_inverse) 
	{
		if(*x > x_offset) 
			*x =  x_offset;
		*x = (x_offset - *x)/x_factor ;
	} 
	else 
	{
		if(*x < x_offset)
			*x = x_offset;
		*x = (*x - x_offset)/x_factor ;
	}
	
	if(y_inverse) 
	{
		if(*y > y_offset) 
			*y =  y_offset;
		*y = (y_offset - *y)/y_factor ;
	} 
	else 
	{
		if(*y < y_offset) 
			*y =  y_offset;
		*y = (*y - y_offset)/y_factor ;
	}
	
	if(tm)
		*tm = 0;
		
	return 1;
}


extern void LcdBkLtSet(U32 HiRatio);

/***************************************************************************
Function name: TPDrawPixel
Parameter    : 
Description  : 触摸屏画点实验函数,在触摸屏画的点通过LCD显示出来
Return	     : 
Argument     : 
Autor & date :
****************************************************************************/
int TPDrawPixel(U32 a1, U32 a2, U32 a3, U32 a4)
{
	U16 lcd_x, lcd_y;
	LcdBkLtSet(70);					//背光控制
	Lcd_Tft_LTS350Q1_PE1_Init();	//LCD初始化
	Glib_ClearScr(0xf81f);			//LCD清屏
	Touch_Screen_Init();			//触摸屏初始化
	printf( "\nWait for hit the touch panel!  ESC to Exit!\n" );

    while( 1 )
    {
    	if(SerialRxReady())
    	{
    		U8 c = getkey();
    		if(c==ESC_KEY)           //#define	ESC_KEY		0x1b
    			break;
//    		if(c==ENTER_KEY)
//    			Glib_ClearScr(0xf81f);
    	}
		if(CheckTouchPanelEvent(&lcd_x, &lcd_y, NULL1)>0)	//触摸屏触发函数
		{
			printf("x=%d y=%d\n",lcd_x,lcd_y);
			/*下面的代码主要是实现将从触摸屏上获得的点在LCD上显示出来*/
		}
		
	}
	
	return 0;
}
/*****************************************************************************/
void fft_touchtest(void)
{
	U16 lcd_x, lcd_y;
	int yingbian=0;
//	LcdBkLtSet(70);					//背光控制
//	Lcd_Tft_LTS350Q1_PE1_Init();	//LCD初始化
//	Glib_ClearScr(0xf81f);			//LCD清屏
	Touch_Screen_Init();			//触摸屏初始化
	printf( "\nWait for hit the touch panel!  ESC to Exit!\n" );

    while( 1 )
    {
    	if(SerialRxReady())
    	{
    		U8 c = getkey();
    		if(c==ESC_KEY)           //#define	ESC_KEY		0x1b
    			break;
//    		if(c==ENTER_KEY)
//    			Glib_ClearScr(0xf81f);
    	}
    	yingbian=CheckTouchPanelEvent(&lcd_x, &lcd_y, NULL);
		if(yingbian>0)	//触摸屏触发函数
		{
			printf("x=%d y=%d\n",lcd_x,lcd_y);
			
		}
		yingbian=0;
		
	}

    MMU_DisableDCache();
    MMU_DisableICache();

	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -