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

📄 touch.h~

📁 这是电池检测仪的程序
💻 H~
字号:
//没有触摸动作时程序不执行,当有触摸时进行扫描采集数据,每5ms进行一次数据采集                                       
//占用资源:INT0和TIMER2    
//2004.08.09
//#include <mylib.h>
//#define BELL_ON()	{DDRB.7=1;PORTB.7=1;}
//#define BELL_OFF()  	{DDRB.7=1;PORTB.7=0;}

/*#define TX1_1()		{PORTE.3=0;DDRE.3=1;}//定义触摸屏四线端口接通状态
#define TX1_0()		{PORTE.3=1;DDRE.3=1;}   
#define TY1_1()		{PORTE.5=0;DDRE.5=1;}	
#define TY1_0()		{PORTE.5=1;DDRE.5=1;}    
#define TX0_1()		{PORTE.4=1;DDRE.4=1;}	
#define TX0_0()		{PORTE.4=0;DDRE.4=1;} 
#define TY0_1()		{PORTE.6=1;DDRE.6=1;}	
#define TY0_0()		{PORTE.6=0;DDRE.6=1;}     

#define TINT_IN()	{DDRE.7=0;}			//触摸屏中断口为输入方式    
#define TINT_INPUT	PINE.7
#define TADX_IN()	{DDRF|=1;PORTF&=0XFE;delay_us(50);DDRF&=0XFE;}//触摸屏ADC端口为输入方式  
#define TADY_IN()	{DDRF|=2;PORTF&=0XFD;delay_us(50);DDRF&=0XFD;}     */

 //unsigned int touch_xdata[20];		//触摸屏X轴数据
 //unsigned int touch_ydata[20];		//触摸屏Y轴数据    
 unsigned char touch_timer1;  		//触摸屏定时放电定时寄存器
 unsigned char touch_timer2;             //触摸屏接收数据超时寄存器
 //unsigned char touch_ok;                 //数据接收完成标志  
 //unsigned char touch_press;		//触摸屏状态,是否按下
 unsigned char touch_i;                  //数据接收指针    
unsigned int touch_x;  			//触摸屏X坐标值   
unsigned int touch_y;			//触摸屏Y坐标值    
unsigned char tk1; 
unsigned char tk2; 
unsigned char tk3; 
unsigned char tk4; 
unsigned char tk5; 
unsigned char tk6;  
unsigned char tk7; 
unsigned char tk8; 
unsigned char tk9; 
unsigned char tk10;      
extern eeprom unsigned char esd;

unsigned char bell_state;
unsigned char bell_timer;

extern unsigned char picture_number;

/*****************************************************************************
				触摸屏初始化
初始化触摸屏四线端口、初始化触摸屏中断口、初始化ADC端口
******************************************************************************/
/*void touch_init(void){
unsigned char i;
       
	
	TX1_0()						//初始化四线端口
	TX0_0()
	TY1_1()
	TY0_0()                 
	  
	TINT_IN()					//触摸屏中断断口为输入方式  					
	EICRB|=0xC0;  					//设置INT7为上升沿触发,清除中断标志,开中断   
	EIFR|=0x80;					
	EIMSK|=0x80;						
	TADX_IN()					//把触摸屏ADC端口设置为输入
	TADY_IN()  

}                       */

/*******************************************************************************
INTO中断程序(触摸屏数据采集程序)
采用中断方式,在中断程序中进行ADC转换读取触摸屏数据
*******************************************************************************/
interrupt[9] int7_touch(void){ 			//触摸屏数据采集程序    
        touch_int();
	/*unsigned char i;  
        TADY_IN()
	TADX_IN()                         
 	if(touch_ok==1)return;			//数据未处理完成,不再接收数据                                              
                     	                				
	TX1_0()					//启动ADC转换Y轴数据   
	TX0_0()   	
	TY0_1() 
	TY1_1()     
	delay_us(400); 	
	for(i=0;i<15;i++){
		ADCSRA=0XA3; 
		ADMUX=0X41;	
		ADCSRA|=0X40; 
		while(!ADCSRA.4){;}
 		touch_ydata[i]=ADC&0X03FF;	
 	}
         						  
	TY1_0()					//启动ADC转换X轴数据   	
	TY0_0()  
	TX1_1();	
	TX0_1()	  
	delay_us(400);	
	for(i=0;i<15;i++){	
		ADCSRA=0XA3; 
		ADMUX=0X40;     	
		ADCSRA|=0X40;  
		while(!ADCSRA.4){;}
 		touch_xdata[i]=ADC&0X03FF;  
 	}
 		
	touch_ok=1;  
	touch_press=1;
	EIMSK&=0X7F;				//一次数据接收完成,关闭中断      
	TX1_0()							
	TX0_0()
	TY1_1()
	TY0_0()		
	TINT_IN()
	TADX_IN()
	TADY_IN()                                                 
 	EIFR|=0X80;      			//清除中断标志位
        TADY_IN()
        TADX_IN()                  */
}

         

/*****************************************************************************
                         	   触摸屏程序   
计算触摸屏坐标值
触摸屏端口100ms定时放电程序      
触摸屏一次按下只接收20个数据,等待放开后再接收下一组数据      
出口参数:
touch_x:X坐标
touch_y:Y坐标
*****************************************************************************/
/*void touch_data(void){         
    unsigned int temp[14]; 
    unsigned long int temp1;
    unsigned int i,p; 
	if(touch_ok==1){  
 		touch_ok=0;				
		for(i=0;i<9;i++){ 			//求x坐标数据
	 		temp[i]=touch_xdata[3+i];       //去掉头尾3个数据
		}    
		for(p=0;p<8;p++){
			i=0;     			//数据排序,
			while(i<8){
				if(temp[i]>temp[i+1]){
			   		temp1=temp[i+1];
					temp[i+1]=temp[i];   
			  		temp[i]=temp1;
		  		}	
				i++;		  		
	  		}   
		}   
		temp1=0;
		for(i=0;i<5;i++){			//去掉最高和最低4个值并求和
			temp1+=temp[i+2];
		} 
		touch_x=temp1/5;          		//计算平均值

		for(i=0;i<9;i++){ 			//求y坐标数据
	 		temp[i]=touch_ydata[3+i];       //去掉头尾3个数据
		}    
		for(p=0;p<8;p++){
			i=0;     			//数据排序,
			while(i<8){
				if(temp[i]>temp[i+1]){
			   		temp1=temp[i+1];
					temp[i+1]=temp[i];   
			  		temp[i]=temp1;
		  		}	
				i++;		  		
	  		}   
		}   
		temp1=0;
		for(i=0;i<5;i++){				//去掉最高和最低4个值并求和
			temp1+=temp[i+2];
		} 
		touch_y=temp1/5;          			//计算平均值	
				
	}
	
   	if(touch_press==1){    					//一次按下只接收20组数据,等待放开再接收下一组数据
		TINT_IN()
		if(!TINT_INPUT){
			delay_ms(10);
			if(!TINT_INPUT){  
				EIFR|=0X80;      			//清除中断标志位
		    		EIMSK|=0X80;
                		touch_press=0;
		 	} 
		}   
   	}
   	if(touch_timer1>100){      					//定时放电
	 	TADY_IN()
 		TADX_IN()
 	  	touch_timer1=0;
   	}  
 
}  
             */
/*********************************************************************************
								触摸屏按键产生程序
根据触摸位置及当前状态产生相应按键值
出口参数:tk_data
*********************************************************************************/


 	  
void touch_key(void){ 
 float x,y; 
 unsigned int i,j;
	
	if((touch_x==0)&(touch_y==0))return;					//没有数据不处理
	x=(float)touch_x*0.78125;  						//把触摸屏坐标转换为LCD坐标
 	y=480-(float)touch_y*0.46875;	
 	  
 	switch(picture_number){							//在不同界面下产生不同的按键
 		case 1:  							//界面1
 		   	if(touch_comp(x,y,240,200,528,264)==1)tk1=1;		//控制按钮
 			break;         
 		case 2:		 						//界面2					
 		   	if(touch_comp(x,y,696,76,776,124)==1)tk2=1;		//退出按钮 
 		   	else if(touch_comp(x,y,600,192,776,240)==1)tk2=2;	//数据曲线按钮  
 		   	else if(touch_comp(x,y,600,248,776,296)==1)tk2=3;	//信息查询按钮 
 		   	else if(touch_comp(x,y,600,304,776,352)==1)tk2=4;	//当前故障按钮
 		   	else if(touch_comp(x,y,600,360,776,408)==1)tk2=5;	//历史故障按钮
 		   	else if(touch_comp(x,y,600,416,776,464)==1)tk2=6;	//系统设置按钮			   	 			
 			break;
 		case 3:       							//界面3   
 		    				
 		   	if(touch_comp(x,y,696,76,776,124)==1)tk3=1;		//退出按钮 
 		   	else if(touch_comp(x,y,600,192,776,240)==1)tk3=2;	//数据曲线按钮  
 		   	else if(touch_comp(x,y,600,248,776,296)==1)tk3=3;	//信息查询按钮 
 		   	else if(touch_comp(x,y,600,304,776,352)==1)tk3=4;	//当前故障按钮
 		   	else if(touch_comp(x,y,600,360,776,408)==1)tk3=5;	//历史故障按钮
 		   	else if(touch_comp(x,y,600,416,776,464)==1)tk3=6;	//系统设置按钮
 		   	
 		   	else if(touch_comp(x,y,40,136,232,216)==1)tk3=7;	//单体电压按钮 
 		   	else if(touch_comp(x,y,312,136,504,216)==1)tk3=8;	//单体内阻按钮
 		   	else if(touch_comp(x,y,40,232,232,312)==1)tk3=9;	//统计参数按钮
 		   	else if(touch_comp(x,y,312,232,504,312)==1)tk3=10;	//容量计量按钮	
 		   				   	 			
 			break;
 		case 4:								//界面4   
 			  					
 		   	if(touch_comp(x,y,696,76,776,124)==1)tk4=1;		//退出按钮 
 		   	else if(touch_comp(x,y,600,192,776,240)==1)tk4=2;	//系统设置按钮  
 		   	else if(touch_comp(x,y,600,248,776,296)==1)tk4=3;	//信息查询按钮 
 		   	else if(touch_comp(x,y,600,304,776,352)==1)tk4=4;	//当前故障按钮
 		   	else if(touch_comp(x,y,600,360,776,408)==1)tk4=5;	//历史故障按钮
 		   	else if(touch_comp(x,y,600,416,776,464)==1)tk4=6;	//系统设置按钮	
 		   	else if(touch_comp(x,y,600,76,680,124)==1)tk4=7;	//上页按钮  
 		   	else if(touch_comp(x,y,600,132,680,180)==1)tk4=8;	//下页按钮		   	 			
 			break;
 		   	
 		case 5://界面5 
 			//if(touch_comp(x,y,600,76,680,124)==1)tk5=1;		//返回按钮    					
 		   	if(touch_comp(x,y,696,76,776,124)==1)tk5=2;		//退出按钮 
 		   	else if(touch_comp(x,y,600,192,776,240)==1)tk5=3;	//数据曲线按钮  
 		   	else if(touch_comp(x,y,600,248,776,296)==1)tk5=4;	//信息查询按钮 
 		   	else if(touch_comp(x,y,600,304,776,352)==1)tk5=5;	//当前故障按钮
 		   	else if(touch_comp(x,y,600,360,776,408)==1)tk5=6;	//历史故障按钮
 		   	else if(touch_comp(x,y,600,416,776,464)==1)tk5=7;	//系统设置按钮			   	 			
 			break;                         
   			
		case 6:								//界面6
			if(touch_comp(x,y,592,76,672,124)==1)tk6=1;		//确认按钮    					
 		   	else if(touch_comp(x,y,688,76,768,124)==1)tk6=2;	//退出按钮 
  		   	
  		   	else{
  		   		for(j=0;j<4;j++){
 		     			for(i=0;i<3;i++){
 		            			if(touch_comp(x,y,600+i*64,192+j*64,648+i*64,240+j*64)==1)tk6=3+i+3*j;	
 		                        }
 		                }
 		       }
 		       break;                  
 		     
 		case 7:	   							//界面7
  		   	if(touch_comp(x,y,592,76,672,124)==1)tk7=1;		//确认按钮    					
 		   	else if(touch_comp(x,y,688,76,768,124)==1)tk7=2;	//退出按钮 
  		   	else if(touch_comp(x,y,56,140,200,188)==1)tk7=3;	//电池类型
  		   	else if(touch_comp(x,y,56,196,200,244)==1)tk7=4;	//电池节数
  		   	else if(touch_comp(x,y,56,244,232,284)==1)tk7=5;	//单体过压值
  		   	else if(touch_comp(x,y,56,292,232,340)==1)tk7=6;	//单体欠压值
  		   	else if(touch_comp(x,y,56,348,232,396)==1)tk7=7;	//差压报警值
  		   	
  		   	else {
  		   		for(j=0;j<4;j++){
 		     			for(i=0;i<3;i++){
 		            			if(touch_comp(x,y,600+i*64,192+j*64,648+i*64,240+j*64)==1)tk7=8+i+j*3;
 		             			
 		       			}
 		         	}
 		        }               
 		        break;              
 		case 8:								//界面8
  		   	if(touch_comp(x,y,592,76,672,124)==1)tk8=1;		//确认按钮    					
 		   	else if(touch_comp(x,y,688,76,768,124)==1)tk8=2;	//退出按钮 
  		   	else if(touch_comp(x,y,56,140,216,188)==1)tk8=3;	//通信地址
  		   	else if(touch_comp(x,y,56,196,216,244)==1)tk8=4;	//通信速率
  		   	else if(touch_comp(x,y,56,284,216,332)==1)tk8=5;	//通信协议
  		   	else{
  		   		for(j=0;j<4;j++){
 		     			for(i=0;i<3;i++){
 		            			if(touch_comp(x,y,600+i*64,192+j*64,648+i*64,240+j*64)==1)tk8=6+i+j*3;
 		            			
 		                        }
 		             	}
 		        }
 		        break;
 		case 9: 
 			if(touch_comp(x,y,592,76,672,124)==1)tk9=1;		//确认按钮    					
 		   	else if(touch_comp(x,y,688,76,768,124)==1)tk9=2;		//退出按钮 
  		   	else if(touch_comp(x,y,56,148,280,196)==1)tk9=3;	//系统密码
  		   	else if(touch_comp(x,y,56,212,152,260)==1)tk9=4;	//日期
  		   	else if(touch_comp(x,y,56,276,152,324)==1)tk9=5;	//时间
  		   	else{
  		   		for(j=0;j<4;j++){
 		     			for(i=0;i<3;i++){
 		            			if(touch_comp(x,y,600+i*64,192+j*64,648+i*64,240+j*64)==1)tk9=6+i+j*3;
 		             			
 		             		}
 		             	}
 		        }
 		         break;
 		case 10: 							//界面10
 			if(touch_comp(x,y,40,136,232,216)==1)tk10=1;		//电池参数按钮    					
 		   	else if(touch_comp(x,y,312,136,504,216)==1)tk10=2;	//后台通信按钮 
  		   	else if(touch_comp(x,y,40,232,232,312)==1)tk10=3;	//密码时间按钮 
  		   	else if(touch_comp(x,y,688,76,768,124)==1)tk10=4;					
   		   	break;
 		default:break;      
 	} 
	touch_x=0;  								//数据处理完毕,清理数据
	touch_y=0;
}
           
/******************************************************************************


******************************************************************************/
/*void bell(void){

	if(bell_state==1){
 		BELL_ON()
		bell_timer=0;
		bell_state=2;
	}
	else if(bell_state==2){
	    if(bell_timer>50){
	    	bell_state=0;
 	    	BELL_OFF()
	    }
	}

}  */

/*****************************************************************************
 								触摸屏处理程序
触摸屏数据采集,过滤及转换成按键程序
*****************************************************************************/
void touch(void){

	touch_data();  					//触摸屏数据处理程序
  	touch_key();                                    //触摸屏按键产生程序   
  	bell();

}

⌨️ 快捷键说明

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