tchscr.c

来自「嵌入式系统Lcd触摸屏实验」· C语言 代码 · 共 354 行

C
354
字号
#include "../inc/drivers.h"
#include "../inc/bitfield.h"
#include "ucos-ii/ucos_ii.h"
#include "ucos-ii/includes.h"
#include "lib.h"
#include "gpio.h"
#include "inc/drv/spi.h"
#include "inc/sys/tchScr.h"
#include <stdio.h>

#define DPRINTF(...)		//printfNULL

#define ADS7843_PIN_PEN		(GPIO_MODE_IN | GPIO_PULLUP_EN | GPIO_F5)
#define GPFDAT (*(volatile unsigned *)0x56000054)
#define TCHSCR_IsPenNotDown		(GPFDAT&(1<<5))
#define ADS7843_PIN_CS		(GPIO_MODE_OUT | GPIO_PULLUP_DIS | GPIO_G12)

#define ADS7843_CTRL_START		0x80
#define ADS7843_GET_X			0x50
#define ADS7843_GET_Y			0x10
#define ADS7843_CTRL_12MODE	0x0
#define ADS7843_CTRL_8MODE	0x8
#define ADS7843_CTRL_SER		0x4
#define ADS7843_CTRL_DFR		0x0
#define ADS7843_CTRL_DISPWD	0x3	// Disable power down
#define ADS7843_CTRL_ENPWD	0x0	// enable power down

#define ADS7843_CMD_X	(ADS7843_CTRL_START|ADS7843_GET_X|ADS7843_CTRL_12MODE|ADS7843_CTRL_DFR|ADS7843_CTRL_ENPWD)
#define ADS7843_CMD_Y	(ADS7843_CTRL_START|ADS7843_GET_Y|ADS7843_CTRL_12MODE|ADS7843_CTRL_DFR|ADS7843_CTRL_ENPWD)

#define enable_7843()	write_gpio_bit(ADS7843_PIN_CS, 0)
#define disable_7843()	write_gpio_bit(ADS7843_PIN_CS, 1)

#define ADS7843_SPI_CHANNEL		0
#define GPFCON (*(volatile unsigned *)0x56000050)
#define GPFUP (*(volatile unsigned *)0x56000058)

#define LCDWIDTH 640
#define LCDHEIGHT 480
int TchScr_Xmin=141,TchScr_Xmax=827,
    TchScr_Ymin=233,TchScr_Ymax=818; 
 OS_EVENT *pevent;   
 int x,y;
 U32 mode; 
 void irq_TCHSCR(void);
//void TchScr_GetScrXY(int *x, int *y,int lable);
 
__inline static void set7843toIRQmode(void)
{
	enable_7843();
	SPISend(ADS7843_CMD_X, ADS7843_SPI_CHANNEL);
	SPIRecv(ADS7843_SPI_CHANNEL);
	SPIRecv(ADS7843_SPI_CHANNEL);
	disable_7843();
}

 void TchScr_init()
{
	set_gpio_ctrl(ADS7843_PIN_CS);
	write_gpio_bit(ADS7843_PIN_CS, 1);
	SPI_initIO(ADS7843_SPI_CHANNEL);
	Set_SIO_mode(ADS7843_SPI_CHANNEL, SPCON_SMOD_INT | 
		SPCON_ENSCK | SPCON_MSTR |SPCON_CPOL_HIGH | 
		SPCON_CPHA_FMTA, 33, 2, NULL, NULL, NULL);
	GPFCON=0xfbff;
	GPFUP=GPFUP&0xdf;	
	
	
    SetISR_Interrupt(IRQ_EINT5,(Interrupt_func_t)irq_TCHSCR, (void *)0);
}

 void TchScr_init1()
{
	set_gpio_ctrl(ADS7843_PIN_CS);
	write_gpio_bit(ADS7843_PIN_CS, 1);
	SPI_initIO(ADS7843_SPI_CHANNEL);
	Set_SIO_mode(ADS7843_SPI_CHANNEL, SPCON_SMOD_POLL | 
		SPCON_ENSCK | SPCON_MSTR |SPCON_CPOL_HIGH | 
		SPCON_CPHA_FMTA, 33, 2, NULL, NULL, NULL);
	GPFCON=0xf3ff;
	GPFUP=GPFUP&0xdf;	
	
	
}
   
#define CLOCK_DELAY()		do{int i; for(i=0; i<10; i++);}while(0)


void TchScr_GetScrXY(int *x,int *y)
{
	unsigned int temp;

	enable_7843();
    
	SPISend (ADS7843_CMD_X, ADS7843_SPI_CHANNEL);
	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_X, ADS7843_SPI_CHANNEL)>>3);
	*x=temp;

	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_X, ADS7843_SPI_CHANNEL) >>3);
	*x+=temp;

	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_X, ADS7843_SPI_CHANNEL)>>3);
	*x+=temp;

	temp=SendReadData(0,ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_Y, ADS7843_SPI_CHANNEL)>>3);
	*x+=temp;
	*x>>=4;		//x=x/4/4;	change to 10bit
	
	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_Y, ADS7843_SPI_CHANNEL) >>3);
	*y=temp;

	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_Y, ADS7843_SPI_CHANNEL) >>3);
	*y+=temp;
	
	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(ADS7843_CMD_Y, ADS7843_SPI_CHANNEL) >>3);
	*y+=temp;

	temp=SendReadData(0, ADS7843_SPI_CHANNEL);
	temp<<=5;
	temp|= (SendReadData(0, ADS7843_SPI_CHANNEL)>>3);
	*y+=temp;
	*y>>=4;		//y=y/4/4;
	
	
     
	disable_7843();
	//return 0;
}


U32 TchScr_GetOSXY(int *x, int *y,int lable)
{//获得触摸点坐标并返回触摸动作
	static U32 mode=0;
	static int oldx,oldy;
	
	int i,j;
	
	
	for(;;){
	if((mode!=TCHSCR_ACTION_DOWN) && (mode!=TCHSCR_ACTION_MOVE)){
			if(!TCHSCR_IsPenNotDown){//有触摸动作
				TchScr_GetScrXY(x, y);//得到触摸点坐标
				for(i=0;i<40;i++){
					if(TCHSCR_IsPenNotDown)//抬起
						break;
					//Delay(100);
					hudelay(20);
				}
				if(i<40){	//在规定的双击时间之内抬起,检测是不是及时按下
					for(i=0;i<60;i++){
						if(!TCHSCR_IsPenNotDown){
                            if (i<10) {i=60;break;}//如果单击后很短时间内按下,不视为双击
                                
							mode=TCHSCR_ACTION_DBCLICK;
							for(j=0;j<40;j++) hudelay(50);//检测到双击后延时,防止拖尾
                            break;
						}
					hudelay(20);	
                   // Delay(100);
					}
					if(i==60)		//没有在规定的时间内按下
						mode=TCHSCR_ACTION_CLICK;
				}
				else{	//没有在规定的时间内抬起
					mode=TCHSCR_ACTION_DOWN;
				}
			  	
              break;
			}
		}
		else{
			if(TCHSCR_IsPenNotDown){	//抬起
				mode=TCHSCR_ACTION_UP;
				*x=oldx;
				*y=oldy;
				return mode;
			}
			else{
				TchScr_GetScrXY(x, y);
				if(ABS(oldx-*x)>25 ||ABS( oldy-*y)>25){//有移动动作
					mode=TCHSCR_ACTION_MOVE;
					break;
				}
			}
		}
		//Delay(50);
		hudelay(10);
	}
    oldx=*x;
	oldy=*y;
	
	if(lable)
	{
	 *x=(*x-TchScr_Xmin)*LCDWIDTH/(TchScr_Xmax-TchScr_Xmin);
     *y=(*y-TchScr_Ymin)*LCDHEIGHT/(TchScr_Ymax-TchScr_Ymin);	
     }
	return mode;
}

void TchScr_Clibrate()
{
	U32 mode;
	int x,y;
	
	Uart_Printf(0,"please touch the screen\n");
   
    drawline(0,10,20,1,0xf818);  //左上角。
	drawline(10,0,20,0,0xf818);
    drawline(0,470,20,1,0xf818);  //坐下角
	drawline(10,460,20,0,0xf818);
	drawline(620,470,20,1,0xf818);  //右下角
	drawline(630,460,20,0,0xf818);
	drawline(620,10,20,1,0xf818); //右上角
	drawline(630,0,20,0,0xf818);
	
	for(;;){
		mode=TchScr_GetOSXY(&x, &y,0);   //不进行坐标转换
		switch(mode){
		case TCHSCR_ACTION_CLICK:	
		  	Uart_Printf(0,"Action=click:x=%d,\ty=%d\n",x,y);
			break;
		case TCHSCR_ACTION_DBCLICK:	
						
			Uart_Printf(0,"Action=double click:x=%d,\ty=%d\n",x,y);
			break;
		case TCHSCR_ACTION_DOWN:
		   				
			Uart_Printf(0,"Action=down:x=%d,\ty=%d\n",x,y);
			break;
		case TCHSCR_ACTION_UP:	
		     Uart_Printf(0,"Action=up:x=%d,\ty=%d\n",x,y);
			break;
		case TCHSCR_ACTION_MOVE:	
		  		
             Uart_Printf(0,"Action=move:x=%d,\ty=%d\n",x,y);
			break;
		}
		
		hudelay(1000);
	}
}


void getkey(int x,int y)
{

  if((x>100)&(x<210)&(y>310)&(y<410))
     
  {
   Uart_SendByte(0,'1');
  }
   if((x>210)&(x<330)&(y>310)&(y<410))
     
  {
   Uart_SendByte(0,'2');
  }
   if((x>330)&(x<455)&(y>310)&(y<410))
     
  {
   Uart_SendByte(0,'3');
  }
   if((x>455)&(x<580)&(y>310)&(y<410))
     
  {
   Uart_SendString(0,"F1");
    
  }
  
  if((x>100)&(x<210)&(y>234)&(y<320))
     
  {
   Uart_SendByte(0,'4');
  }
  if((x>215)&(x<325)&(y>234)&(y<320))
     
  {
   Uart_SendByte(0,'5');
  }
  if((x>325)&(x<455)&(y>234)&(y<320))
     
  {
   Uart_SendByte(0,'6');
  }
   
   if((x>455)&(x<580)&(y>234)&(y<320))
     
  {
   Uart_SendString(0,"F2");
  }
  if((x>100)&(x<210)&(y>144)&(y<234))
     
  {
   Uart_SendByte(0,'7');
  } 
    if((x>215)&(x<325)&(y>144)&(y<234))
     
  {
   Uart_SendByte(0,'8');
  } 
  
   if((x>325)&(x<455)&(y>144)&(y<234))
     
  {
   Uart_SendByte(0,'9');
  } 
   if((x>455)&(x<580)&(y>144)&(y<234))
     
  {
   Uart_SendString(0,"F3");
  } 
  if((x>100)&(x<210)&(y>65)&(y<155))
     
  {
   Uart_SendByte(0,'0');
  } 
  if((x>215)&(x<325)&(y>65)&(y<155))
     
  {
   Uart_SendByte(0,'*');
  }   
  if((x>325)&(x<455)&(y>65)&(y<155))
     
  {
   Uart_SendByte(0,'#');
  }  
   
   if((x>455)&(x<580)&(y>15)&(y<155))
     
  {
   Uart_SendString(0,"F4");
  } 
  }

void irq_TCHSCR(void)

{
   
   OSMboxPost(pevent,(void *)0);	   
}	

⌨️ 快捷键说明

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