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

📄 tsplib.c

📁 在arm9实现了一个简单的触摸屏演示实验,可以实现了简单触摸功能.
💻 C
字号:
/*********************************************************************************************
* File name	: lcdlib.c
* Author	: Brian Zheng
* Descript	: 触摸屏库函数文件 
* History
*			Brian Zheng, Programming modify, May 28, 2008
*********************************************************************************************/
#include <string.h>
#include "2410lib.h"
#include "tsplib.h"
#include "lcdlib.h"

extern void isrADC_normal(void);
/*********************************************************************************************
* name:		tsp_init
* func:		tsp初始化 
* para:		无
* ret:		无
* modify:
* comment:		
*********************************************************************************************/
void tsp_init(void)
{
	rADCDLY = 5000;
	rGPGCON = 0xFF000000;	//设置外部传输端口,nYPON,YMON,nXPON,XMON
	rGPGUP	= 0xFFFF;		//上拉功能失效
	
	rADCTSC = (0<<8) | (1<<7) | (1<<6) | (0<<5) | (1<<4) | (0<<3) | (1<<2) | (0);
	//-----保留位----|--YMON--|--YPON--|--XMON--|--XPON--|上拉使能|自动转换模式|无操作|
    rADCCON  =  (1<<14) | (ADCPRS<<6) | (5<<3) | (0<<2) | (0<<1) | (0);
	//---预分频使能-----|--预分频值---|--AINT5-|正常模式|禁止读启动|转换器不工作|
	rADCTSC = (0<<8) | (1<<7) | (1<<6) | (0<<5) | (1<<4) | (0<<3) | (0<<2) | (3);
	//-----保留位----|--YMON--|--YPON--|--XMON--|--XPON--|上拉使能|自动转换模式|等待中断|
	delay(100);
	
	switch(nMode)
	{
		case 0:
			pISR_ADC = (unsigned)isrADC_normal;	
			uart_printf("TSP normal mode!\n");
			break;
		/*case 1:
			pISR_ADC = (unsigned)tsp_int_separate;
			uart_printf("TSP separate mode!\n");
			break;
		default 2:
			pISR_ADC = (unsigned)tsp_int_auto;
			uart_printf("TSP auto mode!\n");*/	
	}
		
	//pISR_ADC = (unsigned)tsp_int;			// 注册tsp中断处理函数
	rINTMSK &= ~(BIT_ADC);					// 清除相关的中断屏蔽位
	rINTSUBMSK = ~(BIT_SUB_TC);
	
	uart_printf("tsp inital over!\n");
}

/*********************************************************************************************
* name:		tsp_int
* func:		tsp中断处理函数
* para:		无
* ret:		无
* modify:
* comment:		
*********************************************************************************************/
void tsp_int(void)
{
	int i;
	UINT32T szPos[40];		
	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");
		rADCTSC&=0xff;							// Set stylus down interrupt
	}
	else
	{*/
		uart_printf(" Stylus Down!!\n");
		szPos[30] = g_nPosX;
		szPos[34] = g_nPosY;
				
		// <X-Position Read>
		//Hi-Z,AIN5,GND,Ext vlt,Pullup Dis,Normal,X-position
		rADCTSC = (0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(0<<3)|(0<<2)|(1);
		//adc input ain5
		rADCCON = (1<<14)|(39<<6)|(5<<3)|(0<<2)|(1<<1)|(0);
		rADCDAT0;
		delay(10);
		for(i = 0; i<nSampleNo; i++)
		{
			while(!(0x8000 & rADCCON));			// Check ECFLG
			szPos[i] = (0x3ff & rADCDAT0);
			g_nPosX += szPos[i];
		}
		g_nPosX = g_nPosX/nSampleNo;
		uart_printf(" X-Posion[AIN5] is %04d\n", g_nPosX);

		// <Y-Position Read>
		//GND,Ext vlt,Hi-Z,AIN7,Pullup Dis,Normal,Y-position
		rADCTSC = (0<<8)|(1<<7)|(0<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(2);
		//adc input ain7
		rADCCON = (1<<14)|(39<<6)|(7<<3)|(0<<2)|(1<<1)|(0);
		rADCDAT1;
		delay(10);		
		for(i = 0; i<nSampleNo; i++)
		{
			while(!(0x8000 & rADCCON));			// Check ECFLG
			szPos[i] = (0x3ff & rADCDAT1);
			g_nPosY += szPos[i];
			
		}
		g_nPosY = g_nPosY/nSampleNo;
		uart_printf(" Y-Posion[AIN7] is %04d\n", g_nPosY);
		
		//GND,AIN,Hi-z,AIN,Pullup En,Normal,Waiting mode
    	rADCTSC=(1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);
	//}
		
	rSUBSRCPND |= BIT_SUB_TC;		
	rINTSUBMSK = ~(BIT_SUB_TC);			// Unmask sub interrupt (TC)     
	ClearPending(BIT_ADC);
	
	g_nIntSem = 0;
}

/*********************************************************************************************
* name:		tsp_int_normal
* func:		tsp正常模式下的中断处理函数 
* para:		无
* ret:		无
* modify:
* comment:		
*********************************************************************************************/
void tsp_int_normal(void)
{
	UINT32T	tmp,i;
	//INT32T	dX,dY;
	rINTSUBMSK  |=  (BIT_SUB_ADC | BIT_SUB_TC);// 屏蔽子中断(ADC和TC)
	
	 // 判断中断类别	
	if( rADCTSC & 0x100)						//探针离开屏幕时产生的中断
	{
		uart_printf(" Stylus Up!!\n");
		rADCTSC&=0xff;						
	}
	else										//探针与屏幕接触时产生的中断
	{
		uart_printf(" Stylus Down!!\n");
		
		// X坐标读取
		rADCTSC = (0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(1);
		//YM=Hi-Z,YP=AIN[5],XM=GND,XP=VCC,上拉功能失效,正常模式,X坐标转换
		rADCCON = (1<<14)|(49<<6)|(5<<3)|(0<<2)|(1<<1)|(0);
		//rADCCON = (0<<14)|(5<<3)|(0<<2)|(1<<1)|(0);
		//预分频功能失效,AIN[5]输入,正常模式,读启动,转换器不工作
		rADCDAT0;
		delay(10);
		
		for(i = 0; i<nSampleNo; i++)
		{
			while(!(0x8000 & rADCCON));			// 确认AD转换已经结束
			tmp = (0x3ff & rADCDAT0);
			g_nPosX += tmp;
		}
		g_nPosX = g_nPosX/nSampleNo;
		uart_printf(" X-Posion[AIN5] is %04d\n", g_nPosX);
			
		//Y坐标读取
		rADCTSC = (0<<8)|(1<<7)|(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(2);
		//YM=GND,YP=VCC,XM=AIN[7],XP=Hi-Z,上拉功能失效,正常模式,Y坐标转换
		rADCCON = (1<<14)|(49<<6)|(7<<3)|(0<<2)|(1<<1)|(0);
		//rADCCON = (0<<14)|(7<<3)|(0<<2)|(1<<1)|(0);
		//预分频功能失效,AIN[7]输入,正常模式,读启动,转换器不工作
		rADCDAT1;
		delay(10);
		
		for(i = 0; i<nSampleNo; i++)
		{
			while(!(0x8000 & rADCCON));			// 确认AD转换已经结束
			tmp = (0x3ff & rADCDAT1);
			g_nPosY += tmp;
		}
		g_nPosY = g_nPosY/nSampleNo;
		uart_printf(" Y-Posion[AIN7] is %04d\n", g_nPosY);
		
		rADCTSC=(1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);
		//YM=GND,YP=AIN[5],XM=AIN[7],XP=Hi-Z,上拉功能使能,正常模式,等待中断发生
	}
	
	rSUBSRCPND |= BIT_SUB_TC;		
	rINTSUBMSK = ~(BIT_SUB_TC);			// 开中断    
	ClearPending(BIT_ADC);
	
	g_nIntSem = 0;
/*	
	dX = g_nPosX;						//显示触摸点
	dY = g_nPosY;
	tsp_coordinate_convert(dX, dY);
	pixel_display(dX,dY,RED);
	draw_rectangle_width(50, 50, 150, 150, RED, 10);*/
}

/*********************************************************************************************
* name:		tsp_display_init
* func:		tsp显示初始化 
* para:		无
* ret:		无
* modify:
* comment:		
*********************************************************************************************/
void tsp_display_init(void)
{
	lcd_init();	//初始化显示器
	LCD_D_ON;
	
	//draw_rectangle_width(g_nXmin,g_nYmin,g_nXmax,g_nYmax,RED,2);
	draw_rectangle_width(g_dXmin,g_dYmin,g_dXmax,g_dYmax,RED,2);
	
	g_nIntSem = 1;
	while(g_nIntSem ==1);					// 获得第一个对角点的坐标	
	g_nXmin = g_nPosX;
	//g_nYmin = g_nPosY;
	g_nYmax = g_nPosY;
	g_nIntSem = 1;
	while(g_nIntSem ==1);		

	g_nIntSem = 1;
	while(g_nIntSem ==1);						
	g_nXmax = g_nPosX;
	//g_nYmax = g_nPosY;
	g_nYmin = g_nPosY;
	g_nIntSem = 1;
	while(g_nIntSem ==1);					// 获得第一个对角点的坐标	
	
	uart_printf("tsp diaplay initial!\n");
}

/*********************************************************************************************
* name:		tsp_coordinate_convert
* func:		tsp捕获坐标转换 
* para:		无
* ret:		无
* modify:
* comment:		
*********************************************************************************************/
void tsp_coordinate_convert(INT32T *X, INT32T *Y)
{
	(*X) = ((*X)-g_nXmin)*(g_dXmax-g_dXmin)/(g_nXmax-g_nXmin);//+g_dXmin;
	//(*Y) = ((*Y)-g_nYmin)*(g_dYmax-g_dYmin)/(g_nYmax-g_nYmin)+g_dYmin;
	(*Y) = (g_nYmax-(*Y))*(g_dYmax-g_dYmin)/(g_nYmax-g_nYmin);//+g_dYmin;
	//(*X) = (g_nXmax-(*X))*320/(g_nXmax-g_nXmin);
	//(*Y) = (g_nYmax-(*Y))*240/(g_nYmax-g_nYmin);
	//(*Y) = 320 - (*Y);
}
	
void tsp_test(void)
{
	INT32T	X,Y;
	
	tsp_init();
	tsp_display_init();
	
	//draw_rectangle_width(30, 30, 100, 100, RED, 10);
	while(1)
	{
		g_nIntSem = 1;
		while(g_nIntSem ==1);				// 等待中断信号产生
		X = g_nPosX;						//显示触摸点
		Y = g_nPosY;
		g_nIntSem = 1;
		while(g_nIntSem ==1);
		tsp_coordinate_convert(&X, &Y);
		//pixel_display(X,Y,RED);
		draw_rectangle_width(X, Y, X+10, Y+10, RED, 5);
	}
	//pixel_display(g_nPosX, g_nPosY, RED);
	//draw_rectangle_width(50, 50, 150, 150, RED, 10);
	rINTSUBMSK |= BIT_SUB_TC;				//关闭中断
	rINTMSK |= BIT_ADC;
	uart_printf(" end.\n");
}

⌨️ 快捷键说明

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