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

📄 touch.c

📁 这是一个基于消费类电子的触摸屏程序
💻 C
字号:
/****************************************************************************/
/*                                                                          */
/* FILE NAME                                      VERSION                   */
/*                                                                          */
/* TOUCH.C                                         1.0                      */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*     TOUCH CODE for EB44B0(S3C44B0X)                                      */
/*                                                                          */
/*                                                                          */
/* DATA STRUCTURES                                                          */
/*                                                                          */
/* FUNCTIONS :                                                              */
/*     在JX44B0教学实验箱进行触摸屏驱动的实验                               */
/*                                                                          */
/* DEPENDENCIES                                                             */
/*     JX44B0-1                                                             */
/*     JX44B0-2                                                             */
/*     JX44B0-3                                                             */
/*                                                                          */
/*                                                                          */
/* NAME:                                                                    */
/* REMARKS:                                                                 */
/*                                                                          */ 
/*                                                                          */
/*								Copyright (C) 2003 Wuhan CVTECH CO.,LTD     */
/****************************************************************************/

/****************************************************************************/
/* 学习JX44B0中触摸屏的驱动方法:                                           */
/* 注意:                                                                   */
/*     1. 程序运行后必须先进行校准工作,根据提示先触摸屏幕左下角位置,然后  */
/*     触摸屏幕右上角位置,然后才能够进入触摸屏测试                         */
/*     2. 触摸时请使用触摸笔或者手指,尽量保证触摸面积小                    */
/*     3. 请不要使用金属等导电工具触摸,以免损坏触摸屏                      */
/*     4. 请不要使用小刀等尖的工具触摸,以免损坏触摸屏                      */
/****************************************************************************/

/* 包含文件 */
#include "44b.h"
#include "44blib.h"

/* defines */
#define LCD_X_MAX 320					/* or 320 dot (lcd type)			*/
#define LCD_Y_MAX 240					/* or 240 dot						*/

#define TOUCH_MAX_X_LIMIT 800			/* touch value limit x				*/
#define TOUCH_MAX_Y_LIMIT 500			/* touch value limit y				*/

/* functions */
void Eint3Isr(void);
void Touch_Int_Enable(void);			/* external interrupt 3Init & Enable*/

/*****************************************************************************
// Function name	: port_init
// Description	    : 触摸屏端口初始化
// Return type		: void
// Argument         : void
*****************************************************************************/
void port_init(void)
{
	rPUPE=0xff;							/*不使用上拉						*/
	rPCONE=(rPCONE & 0x300ff) | 0x0400; /*pe5(touch_yh)out, pe467 input		*/
	rPDATE=(rPDATE & 0x0f) | 0x0;       /* data low 4,6,7 ( 5= low out )	*/
	
	Delay(100);
}

/*****************************************************************************
// Function name	: wait_for_touch
// Description	    : 等待触摸事件
// Return type		: void
// Argument         : void
*****************************************************************************/
void wait_for_touch()
{
	while( rPDATG & 0x8 );
}

/*****************************************************************************
// Function name	: set_row_input
// Description	    : 向X方向上施加电压,等待采集Y方向的数据
// Return type		: void
// Argument         : void
*****************************************************************************/
void set_row_input()
{
	rADCPSR=0xF;						/*AD转换时钟预除率MCLK/(2*(15+1)*16)*/

	/*PE4       PE5        PE6       PE7		*/
	/*TOUCH_YL  TOUCH_YH   TOUCH_XL  TOUCH_XH	*/

	rPCONE=((rPCONE & 0x300FF) | 0x0500);	/*PE5,4 OUT, 7,6 INPUT			*/

	rPDATE= 0x20;						/* PE5, high, PE4 low				*/
	
	Delay(200);
}

/*****************************************************************************
// Function name	: get_row_input
// Description	    : 采集Y方向的数据
// Return type		: int,采集的数据
// Argument         : void
*****************************************************************************/
int get_row_input()
{
	int index ;
	int sum = 0;
	
	for( index = 0; index < 16; index++ ) 
	{
		rADCCON= (0x1 | (0x0<<2));        	/*ADC0 Enable,0通道*/
		while( rADCCON & 0x1 );				/*是否开始转换	*/
		while(!(rADCCON & 0x40)) ;			/*转换是否完成	*/
		sum += rADCDAT;						/*读入转换值	*/
		Delay(1);
	}

	sum /= 16;								/*取平均值*/
	
	return sum;
}

/*****************************************************************************
// Function name	: set_col_input
// Description	    : 向Y方向上施加电压,等待采集X方向的数据
// Return type		: void
// Argument         : void
*****************************************************************************/
void set_col_input()
{
	rADCPSR= 0xF;
	rPCONE=((rPCONE & 0x300FF) | 0x5000);	/*PE7,6 OUT, 5,4 INPUT			*/
	rPDATE= 0x80;							/* PE6, low, PE7 high			*/
	
	Delay(200);
}

/*****************************************************************************
// Function name	: get_col_input
// Description	    : 采集X方向的数据
// Return type		: int,采集的数据
// Argument         : void
*****************************************************************************/
int get_col_input()
{
	int index ;
	int sum = 0;
	
	for( index = 0; index < 16; index++ ) 
	{
		rADCCON=(0x1 | (0x1<<2));			/*ADC1 Enable,1通道*/
		while( rADCCON & 0x1 );
		while(!(rADCCON & 0x40)) ;
		sum += rADCDAT;						/*读入转换值*/
		Delay(10);
	}
	
	sum /= (index-1);						/*取平均值*/
	
	return sum;
}

/*****************************************************************************
// Function name	: get_x_lcd
// Description	    : 将采集的数据参照LCD进行转换,调整为LCD坐标
// Return type		: int,LCD X坐标
// Argument         : x_min, 触摸屏X方向的最小值
//                    x_max,触摸屏X方向的最大值
//                    x,当前采集的坐标
*****************************************************************************/
int get_x_lcd(int x_min, int x_max, int x)
{
	int lcdx = (int)(320.0/(x_max - x_min)*(x - x_min));
	if(lcdx < 0)
		lcdx = 0;
	if(lcdx >= 320)
		lcdx = 319;
	return lcdx;
}

/*****************************************************************************
// Function name	: get_y_lcd
// Description	    : 将采集的数据参照LCD进行转换,调整为LCD坐标
// Return type		: int,LCD Y坐标
// Argument         : y_min, 触摸屏Y方向的最小值
//                    y_max,触摸屏Y方向的最大值
//                    y,当前采集的坐标
*****************************************************************************/
int get_y_lcd(int y_min, int y_max, int y)
{
	int lcdy = (int)(240.0/(y_max - y_min)*(y - y_min));
	if(lcdy < 0)
		lcdy = 0;
	if(lcdy >= 240)
		lcdy = 239;
	return lcdy;
}

/*****************************************************************************
// Function name	: Main
// Description	    : 触摸屏测试程序主函数
// Return type		: int
// Argument         : void
*****************************************************************************/
void Main(void)
{
	int temp_x_min, temp_y_min, temp_x_max, temp_y_max;
	int temp_x_touch, temp_y_touch, temp_x_lcd, temp_y_lcd;
	
	rPUPG=0xff; 						/*不上拉*/
	rPCONG=(rPCONG & 0xff3f);  			/*eint3 input mode*/
	
	/* LCD(0,0)坐标校准 */
	Uart_Printf("\rTouch the LCD (0, 0)");
	port_init();
	wait_for_touch();	
	set_row_input();
	temp_x_min = get_row_input();
	set_col_input();
	temp_y_min = get_col_input();
	Uart_Printf("\r\tX touch=(%3d), Y touch=(%3d)\n", 
		temp_x_min,
		temp_y_min);

	/* LCD(0,0)坐标校准 */
	Uart_Printf("\rTouch the LCD (319, 239)");
	port_init();
	wait_for_touch();	
	set_row_input();
	temp_x_max = get_row_input();
	set_col_input();
	temp_y_max = get_col_input();
	Uart_Printf("\r\tX touch=(%3d), Y touch=(%3d)\n", 
		temp_x_max,
		temp_y_max);

	/* get the touch point */
	while(1)
	{		
		port_init();
		wait_for_touch();
		set_row_input();
		temp_x_touch = get_row_input();
		temp_x_lcd = get_x_lcd(temp_x_min, temp_x_max, temp_x_touch);
		set_col_input();
		temp_y_touch = get_col_input();
		temp_y_lcd = get_y_lcd(temp_y_min, temp_y_max, temp_y_touch);
		Uart_Printf("\rX touch=(%3d), Y touch=(%3d), X lcd=(%3d), Y lcd=(%3d)\n", 
			temp_x_touch, 
			temp_y_touch, 
			temp_x_lcd, 
			temp_y_lcd);
	}
}

⌨️ 快捷键说明

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