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

📄 touch.c

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

/* 包含文件 */
#include "..\inc\uTypes.h"
#include "..\inc\44b0x.h"
#include "..\inc\console.h"
#include "..\inc\sysUtils.h"
#include "touch.h"
#include "Gui.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				*/

#define Uart_Printf  printf
int  moPosX=310;
int  moPosY=230;
extern unsigned char  cross[];
extern unsigned char  point[];



typedef enum
{
   XL=0,
   XH=3,
   YL=6,
   YH=7,
}PinMap;

typedef enum
{
   IN,
   out,
}Dirt;


void GPIOSet(PinMap pin,unsigned char  dir,unsigned char val)
{
  
}




#define  XH_OUT   rPCONE&=(~0x2000);rPCONE|=0x1000  //set GPIOE6 output
#define  XL_OUT    rPCONE&=(~0x2);rPCONE|=0x01//set GPIOE0 output

#define  XH_IN   rPCONE&=(~0x2000);rPCONE&=(~0x1000)  //set GPIOE6 input
#define  XL_IN    rPCONE&=(~0x2);rPCONE&=(~0x01)//set GPIOE0 input

#define  XH_LO    rPDATE&=(~(1<<6))
#define  XH_HI    rPDATE|=((1<<6))

#define  XL_LO   rPDATE&=(~(1<<0))
#define  XL_HI   rPDATE|=((1<<0))


#define  YH_OUT   rPCONE&=(~0x8000);rPCONE|=0x4000  //set GPIOE7 output
#define  YL_OUT   rPCONE&=(~0x80);rPCONE|=0x40//set GPIOE3 output

#define  YH_IN   rPCONE&=(~0x8000);rPCONE&=(~0x4000)  //set GPIOE7 output
#define  YL_IN   rPCONE&=(~0x80);rPCONE&=(~0x40)//set GPIOE3 output

#define  YH_LO   rPDATE&=(~(1<<7))
#define  YH_HI    rPDATE|=((1<<7))

#define  YL_LO  rPDATE&=(~(1<<3))
#define  VL_HI  rPDATE|=((1<<3))


/* 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 )	*/
	delayMs(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)*/

	/*PE0       PE3        PE6       PE7		*/
	/*TOUCH_YL  TOUCH_YH   TOUCH_XL  TOUCH_XH	*/

	//rPCONE=((rPCONE & 0x300FF) | 0x0500);	/*PE6,0 OUT, 7,3 INPUT			*/

	//rPDATE= 0x20;						/* PE5, high, PE4 low				*/
       XH_OUT;
	XL_OUT;
	
	YH_HI;
	YL_IN;
	
	XH_HI;
	XL_LO;

	delayMs(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= (0x2 | (0x3<<2));        	        /*ADC3 Enable,3通道*/
		while( rADCCON & 0x1 );				/*是否开始转换	*/
		while(!(rADCCON & 0x40)) ;			/*转换是否完成	*/
		sum += rADCDAT;						/*读入转换值	*/
		delayMs(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			*/
	YH_OUT;
	YL_OUT;

	XH_IN;
	XL_IN;

	YH_HI;
	YL_LO;
	
	delayMs(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 | (0x2<<2));			/*ADC2 Enable,2通道*/
		while( rADCCON & 0x1 );
		while(!(rADCCON & 0x40)) ;
		sum += rADCDAT;						/*读入转换值*/
		delayMs(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_Touch(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)");
	LCD_DrawPicture(moPosX-5, moPosY+5, DecodePoint(cross),10,10,2);
	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);
             // moPosX=temp_x_lcd;
	      // moPosY=temp_y_lcd;
		DrawMousePointer(temp_x_lcd, temp_y_lcd);
		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 + -