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

📄 drv_tp.c

📁 The combined demo is dedicated for S1C33L05, so DMT33L05 should be used to load and run the demo. F
💻 C
字号:
/************************************************************************/
/*									                                	*/
/*  Copyright (C) SHANGHAI EPSON CORP. 2001  				        	*/
/*                                  									*/
/*  File name: drv_tp.c                     							*/
/*    This is the driver code of ad convert for touch panel     		*/
/*                                  									*/
/*  Revision history                        							*/
/*      2001.05.14  Stella.Yuan Start           						*/
/*		2004.05.27	David.Ji	Modulize								*/
/************************************************************************/

//-----Include---------
#include "drv_tp.h"

//----Global var-------
T_TPCtlDev		stTPCtlDev;

//----Draw coordinate-----
extern const unsigned short pCordinateBmp[];

/*************************** Touchpanel Operations ****************************/

/*******************************************************************************
 *fnTP_InitHL(void)
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :initalize touch panel hardware
  ******************************************************************************/
void fnTP_InitHL(void)
{
	fnTPHL_InitP();					/* inital GPIO port	*/
	fnTPHL_InitK();					/* inital Input port*/
	fnTPHL_InitTM();				/* inital 16timer	*/
	fnTPHL_InitADC();				/* inital ADC		*/
}

/*******************************************************************************
 *fnTP_InitAL(void)
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :initalize touch panel logic setting
  ******************************************************************************/
void fnTP_InitAL(void)
{
	unsigned short usTmp1X, usTmp1Y, usTmp2X, usTmp2Y;
	
	// inital stTPCtlDev
	stTPCtlDev.ucStatus = TP_STATUS_PENUP;
	stTPCtlDev.ucStage = TP_STAGE_INIT;
	
	// enable tp sample
	fnTP_EnTP();
	
	// 1st adjust dot
//	fnDxLCD_DrawLine();
	lcd_SetImage( 0, 0, 20, 20, (unsigned short *)pCordinateBmp ,20*2);
	
	// check position 1
	do{
		if(stTPCtlDev.ucStatus == TP_STATUS_PENDOWN){
			stTPCtlDev.ucStatus = TP_STATUS_PENUP;
			usTmp1X = stTPCtlDev.usADC_X;
			usTmp1Y = stTPCtlDev.usADC_Y;
		}
	}
	while( \
	!fnTP_ChkRng(usTmp1X, usTmp1Y, TP_RNG1_XL, TP_RNG1_YL, TP_RNG1_XH, TP_RNG1_YH));
	
	TP_M_STOPTM;
//	stTPCtlDev.ucStatus = TP_STATUS_PENUP;

	// 2nd adjust dot
//	fnDxLCD_FillRect();	
//	fnDxLCD_DrawLine();	
	lcd_FillRect(0, 0, 20, 20, 0xffff);
	lcd_SetImage( 300, 220, 20, 20, (unsigned short *)pCordinateBmp ,20*2);
	
	fnTP_EnTP();
	// check position 2
	do{
		if(stTPCtlDev.ucStatus == TP_STATUS_PENDOWN){
			stTPCtlDev.ucStatus = TP_STATUS_PENUP;
			usTmp2X = stTPCtlDev.usADC_X;
			usTmp2Y = stTPCtlDev.usADC_Y;
		}
	}
	while( \
	!fnTP_ChkRng(usTmp2X, usTmp2Y, TP_RNG2_XL, TP_RNG2_YL, TP_RNG2_XH, TP_RNG2_YH));
	
	
	TP_M_STOPTM;
	TP_M_STOPADC;
//	stTPCtlDev.ucStatus = TP_STATUS_PENUP;
	
	// get step value
	if(usTmp1X > usTmp2X)
		stTPCtlDev.fStep_X = ((float)usTmp1X-(float)usTmp2X)/(float)(SCREEN_WIDTH - SCREEN_MARGIN*2);
	else
		stTPCtlDev.fStep_X = ((float)usTmp2X-(float)usTmp1X)/(float)(SCREEN_WIDTH - SCREEN_MARGIN*2);	
	
	if(usTmp1Y > usTmp2Y)
		stTPCtlDev.fStep_Y = ((float)usTmp1Y-(float)usTmp2Y)/(float)(SCREEN_HEIGHT - SCREEN_MARGIN*2);
	else
		stTPCtlDev.fStep_Y = ((float)usTmp2Y-(float)usTmp1Y)/(float)(SCREEN_HEIGHT - SCREEN_MARGIN*2);
	
	stTPCtlDev.stPoint_UL.usX = usTmp1X;
	stTPCtlDev.stPoint_UL.usY = usTmp1Y;
	stTPCtlDev.stPoint_BR.usX = usTmp2X;
	stTPCtlDev.stPoint_BR.usY = usTmp2Y;
	
	lcd_FillRect(300, 220, 20, 20, 0xffff);
//	fnDxLCD_FillRect();
	stTPCtlDev.ucStage = TP_STAGE_SAMPL;
	fnTP_EnTP();
}

/*******************************************************************************
 *fnTP_EnTP(void)
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :enalbe touch panel operation
  ******************************************************************************/
void fnTP_EnTP(void)
{
	fnTP_CtlP(TPCTL_INITAL);

	// reset port input 4 interrupt flag
    *(volatile unsigned char*)0x40287=0x4;

    // port input 4 interrupt enable
    *(volatile unsigned char*)0x40277|=0x04;	
}

/*******************************************************************************
 *fnTP_CtlP(enTPCtlCmd)
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :enalbe touch panel operation
  ******************************************************************************/
void fnTP_CtlP(T_TPCtlP enTPCtlP)
{
	switch(enTPCtlP)
	{
	case TPCTL_INITAL:
			/*inital setting   BXH(p07)=1,BXL(P06)=0, BYH(P42)=1,BYL(P41)=1,*/
    		*(volatile unsigned char*)0x300020|=0x06;
    		*(volatile unsigned char*) 0x402d1|=0x80;
    		*(volatile unsigned char*) 0x402d1&=0xbf;
    		break;
    		
	case TPCTL_SCANX:
			/* sample X direct BXH(p07)=1,BXL(P06)=0, BYH(P42)=0,BYL(P41)=1, X off Y on*/
			*(volatile unsigned char*) 0x402d1|=0x80;
			*(volatile unsigned char*) 0x402d1&=0xbf;
			*(volatile unsigned char*)0x300020|=0x02;
			*(volatile unsigned char*)0x300020&=0xfb;
			break;
						
	case TPCTL_SCANY:
			/* sample Y direct BXH(p07)=0,BXL(P06)=1, BYH(P42)=1,BYL(P41)=0, Y off X on*/
			*(volatile unsigned char*)0x300020|=0x04;
			*(volatile unsigned char*)0x300020&=0xfd;
			*(volatile unsigned char*) 0x402d1|=0x40;
			*(volatile unsigned char*) 0x402d1&=0x7f;
			break;
	}
}

/*******************************************************************************
 *fnTP_GetLogPos()
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :
 *
 ******************************************************************************/
void fnTP_GetLogPos(unsigned short* usLogX, unsigned short* usLogY)
{
	fnTPAL_MapPos(&stTPCtlDev);
	*usLogX = stTPCtlDev.usLOG_X;
	*usLogY = stTPCtlDev.usLOG_Y;
}

/*******************************************************************************
 *fnTP_CalAvg()
 *      Type    :void
 *      Ret val :cal result 
 *              :
 *      Argument :
 *      Function :get average of input data
  *******************************************************************************/
unsigned char fnTP_CalAvg(unsigned short* pusData, unsigned short* pusResult)
{
    unsigned char   ucCnt;
    unsigned short  usMax,usMin,usSecMax,usSecMin,usSum,usAvrg;
	unsigned char	ucRetVal;

    usMax=usMin=usSecMax=usSecMin=usSum=*pusData;
    pusData++;
    
    //usSum=0;
    for (ucCnt=0x1;ucCnt<TP_SAMPLE_TIMES;ucCnt++){
        if( *pusData>usMax){
            usSecMax=usMax;
            usMax=*pusData;
        }
        else if(*pusData<usMin){
            usSecMin=usMin;
            usMin=*pusData;
        }
        usSum+=*pusData;
        pusData++;
    }

    /* if exceed 0x10 , error */
    if ((usSecMax-usSecMin)>=0x40) {
        return 0;
    }
    
    *pusResult=(usSum-usMax-usMin)/(TP_SAMPLE_TIMES-2);
	return 1;
}

/*******************************************************************************
 *fnTP_ChkRng()
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :if in range return true
 *        		  else return false
 ******************************************************************************/
unsigned char fnTP_ChkRng ( unsigned short usX,unsigned short usY,\
	unsigned short usX1,unsigned short usY1, unsigned short usX2,unsigned short usY2)
{
    if ((usX>=usX1)&&(usX<=usX2)&&(usY>=usY1)&& (usY<=usY2))
        return 1;
    else
        return 0;
}

/*************************** Touchpanel Inter Module ***************************/

/*******************************************************************************
 *fnTPHL_InitP()
 *      Type : void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :initilize p port
  *******************************************************************************/
static void fnTPHL_InitP(void)
{
    //P07=BXH, P06=BXL, P42=BYH, P41=BYL as GPIO
    *(volatile unsigned char*) 0x402d0&=0xc0;
    *(volatile unsigned char*) 0x402df&=0x3f;
    *(volatile unsigned char*)0x300048&=0xc3;
    *(volatile unsigned char*)0x300048|=0x14;

    //P07, P06, P41, P42 as output
    *(volatile unsigned char*) 0x402d2|=0xc0;
    *(volatile unsigned char*)0x300021|=0x06;

}

/*******************************************************************************
 *  fnTPHL_InitK()
 *      Type : void
 *      Ret val :void
 *
 *      Argument :  void
 *      Function : initilize input port
  *******************************************************************************/
static void fnTPHL_InitK(void)
{
    // port input 4 interrupt disable
    *(volatile unsigned char*)0x40277&=0xfb;

    // K64 as input port
    *(volatile unsigned char*)0x402c3&=0xef;

    // port 4 interrupt level = 4
    *(volatile unsigned char*)0x4026c&=0xf0;
    *(volatile unsigned char*)0x4026c|=0x04;

    // port input 4 interrupt request
    *(volatile unsigned char*)0x40293&=0x7f;

    // port input 4 IDMA disable
    *(volatile unsigned char*)0x40297&=0x7f;

    // K64 as input port selection
    *(volatile unsigned char*)0x402c7&=0xfc;

    // port input 4 low polarity
    *(volatile unsigned char*)0x402c8&=0xef;

    // port input 4 edge selection
    *(volatile unsigned char*)0x402c9|=0x10;

    // reset port input 4 interrupt flag
    *(volatile unsigned char*)0x40287=0x4;

}

/*******************************************************************************
 *  fnTPHL_InitADC()
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :initilize ad convert
  *******************************************************************************/
static void fnTPHL_InitADC(void)
{
    // A/D interrupt disable
    *(volatile unsigned char*)0x40277&=0xfe;

    // A/D convertion stop,reset overwrite error flag ,A/D disable
    *(volatile unsigned char*)0x40244&=0x00;

    // k62 as AD2, k63 as AD3
    *(volatile unsigned char*)0x402c3|=0x0c;

    // A/D converter clock control on, clk/32
    *(volatile unsigned char*)0x4014f=0x0c;

    /* A/D converter sampling 9 clock SPT=6.5us, OSC3 48MHZ,clk/32 */
    *(volatile unsigned char*)0x40245=0x3;

    /* A/D trigger normal ,software trigger, */
    *(volatile unsigned char*)0x40242=0;

    /* set A/D convert interrupt level is 0x4 */
    *(volatile unsigned char*)0x4026a&=0x0f;
    *(volatile unsigned char*)0x4026a|=0x40;

    /* A/D interrupt request */
    *(volatile unsigned char*)0x40293&=0xfb;
    /* A/D IDMA disable */
    *(volatile unsigned char*)0x40297=0xfb;

    /* A/D interrupt flag reset*/
    *(volatile unsigned char*)0x40287=0x1;
}

/*******************************************************************************
 *fnTPHL_InitTM()
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :void
 *      Function :initalize the 16-timer
 ******************************************************************************/
static void fnTPHL_InitTM(void)
{
    /*16-bit  timer 3 clock clk/256 */
    *(volatile unsigned char*)REG_TM16_3_PSC = 0x0d;

    /* 	16-bit timer3 fine mode=normal,buffer disable,
    output inversion normal, interal clk, output off, reset, stop*/;
    *(volatile unsigned char*)REG_TM16_3_CTL = 0x2;

    /* priority level =4 */
    *(volatile unsigned char*)REG_TM16_3_ILEVL &= 0x0f;
    *(volatile unsigned char*)REG_TM16_3_ILEVL |= 0x40;

    /*load regiter interrupt 10ms*(48*1000000*1/256)*/
    *(volatile unsigned short*)REG_TM16_3_CMPB = 0x1e00;

    /* reset 16-bit timer comparsion B intterupt factor */
    *(volatile unsigned char*)REG_TM16_3_IFLAG |= 0x40;

    /* 16-bit timer comparison B interrupt enable */
//    *(volatile unsigned char*)REG_TM16_3_IMASK |= 0x40;
}

/*******************************************************************************
 *fnTPAL_MapPos(T_TPCtlDev)
 *      Type    :void
 *      Ret val :void
 *
 *      Argument :T_TPCtlDev
 *      Function :map phical value 
 *				to logical pos
 ******************************************************************************/
static void fnTPAL_MapPos(T_TPCtlDev *pstTPCtlDev)
{	

	pstTPCtlDev->usLOG_X = (unsigned short)(pstTPCtlDev->stPoint_UL.usX - pstTPCtlDev->usADC_X) \
							/pstTPCtlDev->fStep_X + SCREEN_MARGIN; 
	
	pstTPCtlDev->usLOG_Y = (unsigned short)(pstTPCtlDev->stPoint_UL.usY - pstTPCtlDev->usADC_Y) \
							/pstTPCtlDev->fStep_Y + SCREEN_MARGIN; 
	
}

/*------------ End of this file -----------------------*/

⌨️ 快捷键说明

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