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

📄 3543

📁 触摸屏在S3C2410上的应用实例
💻
字号:
#include <string.h>
#include "2410addr.h"
#include "2410lib.h"
#include "Ts_auto.h"
#include "def.h"
#include "lcdlib.h"
#include "glib.h"
#include "lcd.h"

#define TFT640_480	1
#define TFT320_240	2
#define LCD_TYPE	TFT320_240

#define ADCPRS 49
#define Touch_Up 0x1
#define Touch_Down	0x2

#define TouchSample 9


#define TOUCH_MAX_X 1000
#define TOUCH_MIN_X 45
#define TOUCH_MAX_Y 1000
#define TOUCH_MIN_Y 35

#define TOUCH_X	320
#define TOUCH_Y	240

#define FILTER_LIMIT 10

// Maximum allowed variance in the X coordinate samples
#define DELTA_X_COORD_VARIANCE          0x2
// Maximum allowed variance in the X coordinate samples.
#define DELTA_Y_COORD_VARIANCE          0x2
	
//ADC delay time 
#define ADC_DELAY_TIME	0x1400


int	Flag_Touch;
int	gTouchStartSample;

int Touch_Pen_filtering(int *px, int *py)
{
	int RetVal = TRUE;
	// TRUE  : Valid pen sample
	// FALSE : Invalid pen sample
	static int count = 0;
	static int x[2], y[2];
	int TmpX, TmpY;
	int dx, dy;
	
	count++;

	if (count > 2) 
	{ 
		// apply filtering rule
		count = 2;
		
		// average between x,y[0] and *px,y
		TmpX = (x[0] + *px) / 2;
		TmpY = (y[0] + *py) / 2;
		
		// difference between x,y[1] and TmpX,Y
		dx = (x[1] > TmpX) ? (x[1] - TmpX) : (TmpX - x[1]);
		dy = (y[1] > TmpY) ? (y[1] - TmpY) : (TmpY - y[1]);
		
		if ((dx > FILTER_LIMIT) || (dy > FILTER_LIMIT)) 
		{
			// Invalid pen sample

			*px = x[1];
			*py = y[1]; // previous valid sample
			RetVal = FALSE;
			count = 0;
		} 
		else 
		{
			// Valid pen sample
			x[0] = x[1]; y[0] = y[1];		
			x[1] = *px; y[1] = *py; // reserve pen samples
			
			RetVal = TRUE;
		}
		
	} 
	else 
	{ // till 2 samples, no filtering rule
	
		x[0] = x[1]; y[0] = y[1];		
		x[1] = *px; y[1] = *py; // reserve pen samples
		
		RetVal = FALSE;	
	}
	
	return RetVal;
}


void Touch_CoordinateConversion(int *px, int *py)
{
	int TmpX, TmpY;
	int temp;

	TmpX = (*px >= TOUCH_MAX_X) ? (TOUCH_MAX_X) : *px;
	TmpY = (*py >= TOUCH_MAX_Y) ? (TOUCH_MAX_Y) : *py;

	TmpX -= TOUCH_MIN_X;
	TmpY -= TOUCH_MIN_Y;

	TmpX = (TmpX) ? TmpX : 0;
	TmpY = (TmpY) ? TmpY : 0;

	*px = (TmpX* TOUCH_X)  / (TOUCH_MAX_X-TOUCH_MIN_X);
	*py = (TmpY* TOUCH_Y)  /  (TOUCH_MAX_Y-TOUCH_MIN_Y) ;
	
	return;
}

void __irq Adc_or_TsAuto(void)
{
	int ptx,pty;
	rINTSUBMSK|=(BIT_SUB_ADC|BIT_SUB_TC);	// Mask sub interrupt (ADC and TC) 

    // TC(Touch screen Control) Interrupt	
    if(rADCTSC&0x100)
    {
	//Uart_Printf("Stylus Up!!\n");
	Flag_Touch=Touch_Up;
	rADCTSC&=0xff;	// Set stylus down interrupt
    }
    else 
    {
	//Uart_Printf("Stylus Down!!\n");
	// <Auto X-Position and Y-Position Read>
	rADCTSC=(0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0);
	// Stylus Down,Don't care,Don't care,Don't care,Don't care,XP pullup Dis,Auto,No operation

	rADCCON|=0x1;	// Start Auto conversion

	while(rADCCON & 0x1);		//check if Enable_start is low
	while(!(0x8000&rADCCON));	// Check ECFLG

	ptx=(0x3ff&rADCDAT0);
	pty=(0x3ff&rADCDAT1);
	
	rADCTSC=(1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);
	Flag_Touch=Touch_Down;
   	// Stylus Up,Don't care,Don't care,Don't care,Don't care,XP pullup En,Normal,Waiting mode
    }
    rSUBSRCPND|=BIT_SUB_TC;
    rINTSUBMSK=~(BIT_SUB_TC);	// Unmask sub interrupt (TC)     
    ClearPending(BIT_ADC);
}

//---------------------------------------------------------------------
void __irq Timer4Intr(void)
{

	rINTMSK |= BIT_TIMER4 ;
	if(Flag_Touch==Touch_Down)
		gTouchStartSample=TRUE;
	else 
		gTouchStartSample=FALSE;
	ClearPending(BIT_TIMER4);
	rINTMSK = ~(BIT_TIMER4) ;
}
			

void Ts_Auto(void)
{
	int 	i,ptx[TouchSample],pty[TouchSample];
	int   px[3],py[3];
	int 	tmx,tmy;
	int    	dlXDiff0;
    	int    	dlXDiff1;
    	int    	dlXDiff2;
    	int    	dlYDiff0;
    	int    	dlYDiff1;
    	int    	dlYDiff2;
	char 	command;
		
	Uart_Printf("[Touch Screen Test.]\n");
	Uart_Printf("Auto X/Y position conversion mode test\n");

// init LCD
	Lcd_Port_Init();

	Lcd_Init(MODE_TFT_16BIT_320240);
	Glib_Init(MODE_TFT_16BIT_320240);

	Lcd_PowerEnable(0, 1);
	Lcd_EnvidOnOff(1);

 	Glib_FilledRectangle(0,0,319,239,0xf800);    

	Uart_Printf("Init  TFT 640*480 16BPP, input any key to continue test \n");
	Uart_Getch();  	
	
	Glib_ClearScr(0, MODE_TFT_16BIT_320240);
  
	Uart_Printf("\nStart on Touch Screen test   \n\n1,input 'c' is clear sreen to black color  \n2,other key is exit this test to menu \n\n\n");

	gTouchStartSample=FALSE;
	Flag_Touch=Touch_Up;

	
	rADCDLY=(ADC_DELAY_TIME);	// ADC Start or Interval Delay

	//rADCCON = (1<<14)|(ADCPRS<<6)|(0<<3)|(0<<2)|(0<<1)|(0);	
	rADCCON	= (1<<14)|(ADCPRS<<6)|(7<<3);	
	// Enable Prescaler,Prescaler,AIN5/7 fix,Normal,Disable read start,No operation
	rADCTSC=(0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);//tark
	// Down,YM:GND,YP:AIN5,XM:Hi-z,XP:AIN7,XP pullup En,Normal,Waiting for interrupt mode

	//Init TIMER4 Register
	rTCFG0 = rTCFG0 &(~(0xFF<<8))| (0xF<<8);         //Prescaler1=15(0x0f)
	rTCFG1  =rTCFG1 & (~( 0xF<<16))&(~(0xF<<20));         //all interrupt,Mux4=1/2
	 //Timer input clock frequency = PCLK/(prescaler value+1)/(divider value)
	rTCNTB4 = 0x3D09;           //(1/(50MHz/16/2 )) * 0x3D09(15625) = 0.01s (100Hz)

	pISR_ADC=(unsigned)Adc_or_TsAuto;
	pISR_TIMER4=(unsigned)Timer4Intr;
	rINTMSK=~(BIT_ADC);
	rINTSUBMSK=~(BIT_SUB_TC);
	rINTMSK = ~(BIT_TIMER4) ;

	rTCON   = rTCON  & (~(0x7<<20)) | (0x6<<20);         //Auto reload, Manual update,  Stop     
	rTCON   = rTCON  & (~(0x7<<20)) | (0x5<<20);         //Auto reload,Start

	while(1)
	{
		while(gTouchStartSample)
		{
			gTouchStartSample=FALSE;
			
			for(i=0;i<TouchSample;i++)
			{
				// <Auto X-Position and Y-Position Read>
				rADCTSC=(0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0);
				// Stylus Down,Don't care,Don't care,Don't care,Don't care,XP pullup Dis,Auto,No operation
				rADCCON|=0x1;	// Start Auto conversion

				while(rADCCON & 0x1);		//check if Enable_start is low
				while(!(0x8000&rADCCON));	// Check ECFLG

				ptx[i]=(0x3ff&rADCDAT0);
				pty[i]=(0x3ff&rADCDAT1);
			}

			px[0]=(ptx[0]+ptx[1]+ptx[2])/3;
			px[1]=(ptx[3]+ptx[4]+ptx[5])/3;
			px[2]=(ptx[6]+ptx[7]+ptx[8])/3;

			py[0]=(pty[0]+pty[1]+pty[2])/3;
			py[1]=(pty[3]+pty[4]+pty[5])/3;
			py[2]=(pty[6]+pty[7]+pty[8])/3;

		//X  Difference 
			dlXDiff0 = px[ 0 ] - px[ 1 ];
			dlXDiff1 = px[ 1 ] - px[ 2 ];
			dlXDiff2 = px[ 2 ] - px[ 0 ];
			dlXDiff0 = dlXDiff0 > 0  ? dlXDiff0 : -dlXDiff0;
			dlXDiff1 = dlXDiff1 > 0  ? dlXDiff1 : -dlXDiff1;
			dlXDiff2 = dlXDiff2 > 0  ? dlXDiff2 : -dlXDiff2;
			
		// The final X coordinate is the average of coordinates of the two MIN of the differences.
			if ( dlXDiff0 < dlXDiff1 )
			{
			    if ( dlXDiff2 < dlXDiff0 )
			    {
			        tmx = ( ( px[ 0 ] + px[ 2 ] ) >> 1 ) ;
			    }
			    else
			    {
			        tmx =  ( ( px[ 0 ] + px[ 1 ] ) >> 1 );
			    }
			}
			else if ( dlXDiff2 < dlXDiff1 )
			{
			    tmx = ( ( px[ 0 ] + px[ 2 ] ) >> 1 ) ;
			}
			else
			{
			    tmx = ( ( px[ 1 ] + px[ 2 ] ) >> 1 ) ;
			}
			
		//Y Difference 
			dlYDiff0 = py[ 0 ] - py[ 1 ];
			dlYDiff1 = py[ 1 ] - py[ 2 ];
			dlYDiff2 = py[ 2 ] - py[ 0 ];
			dlYDiff0 = dlYDiff0 > 0  ? dlYDiff0 : -dlYDiff0;
			dlYDiff1 = dlYDiff1 > 0  ? dlYDiff1 : -dlYDiff1;
			dlYDiff2 = dlYDiff2 > 0  ? dlYDiff2 : -dlYDiff2;

		// The final Y coordinate is the average of coordinates of the two MIN of the differences.
			if ( dlYDiff0 < dlYDiff1 )
			{
			    if ( dlYDiff2 < dlYDiff0 )
			    {
			        tmy = ( ( py[ 0 ] + py[ 2 ] ) >> 1 ) ;
			    }
			    else
			    {
			        tmy =  ( ( py[ 0 ] + py[ 1 ] ) >> 1 );
			    }
			}
			else if ( dlYDiff2 < dlYDiff1 )
			{
			    tmy = ( ( py[ 0 ] + py[ 2 ] ) >> 1 ) ;
			}
			else
			{
			    tmy = ( ( py[ 1 ] + py[ 2 ] ) >> 1 ) ;
			}

			if ( 	dlXDiff0 > DELTA_X_COORD_VARIANCE &&
				dlXDiff1 > DELTA_X_COORD_VARIANCE &&
				dlXDiff2 > DELTA_X_COORD_VARIANCE &&
				dlYDiff0 > DELTA_Y_COORD_VARIANCE &&
				dlYDiff1 > DELTA_Y_COORD_VARIANCE &&
				dlYDiff2 > DELTA_Y_COORD_VARIANCE 	)
			{
			//if =-1 the option will omit
				tmx=-1;
				tmy=-1;
			}

			rADCTSC=(1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);

			if(tmx!=-1|| tmy!=-1)
			{
				Touch_CoordinateConversion(&tmx,&tmy);

				Touch_Pen_filtering(&tmx,&tmx);

				PutPixel(tmx,tmy,0xf800);
			}

			command=Uart_GetKey();
			if(command=='c')
				Glib_ClearScr(0, MODE_TFT_16BIT_640480);
			else if(command !=0)
				return;
			
			Uart_Printf("[%04d,", tmx);
			Uart_Printf("%04d]     ", tmy);
		}
	}

	Uart_Printf("\nType any key to exit!!!\n");
	Uart_Printf("\nStylus Down, please...... \n");
	Uart_Getch();

	rINTSUBMSK|=BIT_SUB_TC;
	rINTMSK|=BIT_ADC;
	Uart_Printf("[Touch Screen Test.]\n");

	MoveViewPort(MODE_TFT_16BIT_320240);
	Lcd_MoveViewPort(0,0,MODE_TFT_16BIT_320240);
	Lcd_EnvidOnOff(0);
	Lcd_Port_Return();
}


⌨️ 快捷键说明

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