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

📄 touch.c

📁 S3C44B0控制ADS7843专用四线电阻控制芯片的
💻 C
字号:
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\Touch.h"
#include "..\inc\def.h"
#include "..\inc\lcdlib.h"
#include "..\inc\glib.h"


volatile int xPos = LCD_XSIZE/2;
volatile int yPos = LCD_YSIZE/2 + 5;
volatile int Pos_change = 0;

volatile int Buttons = 0;
volatile int Buttons_change = 0;

volatile int touch_check = 0;
volatile int touch_check_delay = 0;
volatile int touch_check_count = 0;

volatile char TouchState;
volatile int touch_x[3];
volatile int touch_y[3];

U8 flag=0;
S16 Positionx0=0;//54;
S16 Positiony0=0;//40;
S16 Positionx1=LCD_XSIZE;//470;
S16 Positiony1=LCD_YSIZE;//395;

S16 fp0;
S16 fp1;
#define	ledon	*(volatile U8 *)(0x04080000)
U8 Adjust(void);
void __irq Timer1Done(void);

void __irq AD7843_Int(void)
{
    rI_ISPC |= BIT_SIO;

    switch(TouchState)
    {
      case 0x00:
           TouchState = 0x01;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x01:
           TouchState = 0x02;
           touch_x[touch_check_count] = rSIODAT;
           touch_x[touch_check_count] <<= 8;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x02:
           TouchState = 0x03;
           touch_x[touch_check_count] += rSIODAT;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x03:
           TouchState = 0x04;
           rSIODAT = 0xd3;
           rSIOCON|=(1<<3);
           break;
      case 0x04:
           TouchState = 0x05;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x05:
           TouchState = 0x06;
           touch_y[touch_check_count] = rSIODAT;
           touch_y[touch_check_count] <<= 8;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x06:
           TouchState = 0x07;
           touch_y[touch_check_count] += rSIODAT;

           rSIODAT = 0x98;
           rSIOCON|=(1<<3);
           touch_check_count++;

           if(touch_check_count == 3)
           {
             int touch_tmp_x,touch_tmp_y;

             touch_check_count = 0;

             //xPos  X坐标
             if(touch_x[0]>touch_x[1]) //高低字节交换
             {
               touch_tmp_x=touch_x[0];
               touch_x[0]=touch_x[1];
               touch_x[1]=touch_tmp_x;
             }

             if(touch_x[1]>touch_x[2])
             {
               touch_tmp_x=touch_x[1];
               touch_x[1]=touch_x[2];
               touch_x[2]=touch_tmp_x;
             }

             if(touch_x[0]>touch_x[1])
             {
               touch_tmp_x=touch_x[0];
               touch_x[0]=touch_x[1];
               touch_x[1]=touch_tmp_x;
             }


             //yPos   Y坐标
             if(touch_y[0]>touch_y[1])
             {
               touch_tmp_y=touch_y[0];
               touch_y[0]=touch_y[1];
               touch_y[1]=touch_tmp_y;
             }

             if(touch_y[1]>touch_y[2])
             {
               touch_tmp_y=touch_y[1];
               touch_y[1]=touch_y[2];
               touch_y[2]=touch_tmp_y;
             }

             if(touch_y[0]>touch_y[1])
             {
               touch_tmp_y=touch_y[0];
               touch_y[0]=touch_y[1];
               touch_y[1]=touch_tmp_y;
             }

             touch_x[1] >>= 6;  // 除以64
  //           touch_x[1] = (touch_x[1] - Positionx0)*LCD_XSIZE/(Positionx1 - Positionx0); //校准

             touch_y[1] >>= 6;
  //           touch_y[1] = (touch_y[1]- Positiony0)*LCD_YSIZE/(Positiony1 - Positiony0);

  //           if((xPos!=touch_x[1])||(yPos!=touch_y[1]))
   //          {
               xPos = touch_x[1];
               yPos = touch_y[1];
    //           Pos_change++;    //position changed, 处理程序 Pos_change--
    //         }

             if(!Buttons)    //Pressed button
             {
                Buttons = 0x81;
                Buttons_change++;
             }
           Adjust();

           }
           break;
      case 0x07:
           rPDATF |= 0x040; //cs = 1
           touch_check = 3;
           touch_check_delay = 20;
           break;
    }

}

U8 Adjust(void)
{
	if(flag==2)
            {  
               if(yPos>100 && yPos<280) 
                   {
                      if (xPos>0 && xPos<95)
                          {
                             Uart_Printf("您按的数字为1\r");//串口输出            
                             ledon=0x14;  //数码管显示1  		                 
                           }
                      else if (xPos>95 && xPos<190)
                          { 
                             Uart_Printf("您按的数字为2\r");
                             ledon=0xb3;//数码管显示2 	                       
                          }
                      else if (xPos>190 && xPos<285)
                          {
                             Uart_Printf("您按的数字为3\r");
                             ledon=0xb6;	    
                           }
                      else if (xPos>285 && xPos<380)
                           {
                              Uart_Printf("您按的数字为4\r");
                               ledon=0xd4;	    
                            }
                      else if (xPos>380&& xPos<475)
                           {
                             Uart_Printf("您按的数字为5\r");
                             ledon=0xe6;	    
                            }
                     }
                else if (yPos>280 && yPos<470)
                    { 
                        if (xPos>0 && xPos<95)
                          {
                             Uart_Printf("您按的数字为6\r");            
                             ledon=0xe7;   		                 
                           }
                      else if (xPos>95 && xPos<190)
                          { 
                             Uart_Printf("您按的数字为7\r");
                             ledon=0x34;	                       
                          }
                      else if (xPos>190 && xPos<285)
                          {
                             Uart_Printf("您按的数字为8\r");
                             ledon=0xf7;	//    
                           }
                      else if (xPos>285 && xPos<380)
                           {
                              Uart_Printf("您按的数字为9\r");
                               ledon=0xf6;	    
                            }
                      else if (xPos>380&& xPos<475)
                           {
                             Uart_Printf("您按的数字为0\r");
                             ledon=0x77;	    
                            }

                     }

            }
 	else if (!flag)
       	   {
	     flag++;
           }
 	else if(flag==1)
     	   {
             flag++;
	     Uart_Printf("Ok!\n");
	   } 
   return 1;
}

void Touch_check(void) //被Rtc_Tick调用
{
   if((rPDATG & 0x02) == 0)
   {
      if(touch_check == 0)
      {
       	 touch_check = 1;
       	 touch_check_delay = 10;
      }
      else if(touch_check == 1)
      {
        	 if(touch_check_delay)
        	 {
           		touch_check_delay--;
        	 }
         	else
         	{
         	  	touch_check = 2;
           		rPDATF &= 0x1bf; //cs = 0
          	 	TouchState = 0x00;
          	 	rSIODAT = 0x93;
           		rSIOCON|=(1<<3);
       	 }
      }
      else if(touch_check ==3)
      {
         	touch_check = 1;
        	 touch_check_delay = 10;
      }
   }
   else
   {
      	if(touch_check == 1)
     	 {
        		 touch_check = 0;
        		 if(Buttons)
        		 {
         			  Buttons = 0;
          			 Buttons_change++;
       		  }
       		 touch_check_count = 0;
     	 }
     	 else if(touch_check == 3)
     	 {
       		 touch_check = 0;
       		 Buttons = 0;
       		 Buttons_change++;
       		 touch_check_count = 0;
     	 }
   }
 
}


 

void __irq TouchTimerDone(void)
{
    rI_ISPC |= BIT_TIMER1;   
    Touch_check();
}


/****************************************

 ****************************************/
void Test_Touch(void)
{
int i,j;

    rINTMSK =0x03ffffff;    
    rINTMSK &= (~BIT_TIMER1);
    pISR_TIMER1=(int)TouchTimerDone;

    rTCFG0 |= 0x0f;    //dead zone=0,pre2=0,pre1=0,pre0=0xf

    rTCFG1 &= 0xfffff0f; 
    rTCFG1 |= 0x10;	//all interrupt,mux1=1/4
    
    rTCNTB1 = 5000;	//(1/(60MHz/15/4))*1000=1ms

    rTCON &= 0xffff0ff;
    rTCON |= 0x0000200;	//update T5/T4/T3/T2/T1/T0
    
    rTCON &= 0xffff0ff;
    rTCON |= 0x0000900;	//T5/T4/T3/T2/T1=auto reload,T0=one shot,all_start

    rPCONG &= 0xfff3; //EINT1 
    rPUPG &= 0xfffd;  //pull up enable

    pISR_SIO=(unsigned)AD7843_Int;//中断地址
    rINTMSK &= (~BIT_SIO);//enable sio intrupt 使能SIO中断

    rPDATF |= 0x040; //cs = 0 pf6=1, enable ads7843
    rPCONF=(rPCONF&0x3ff)+(3<<19)+(3<<16)+(01<<13)+(3<<10);
						//pf8 sioclk,pf7 siorxd,pf6 output,pf5 siotxd
    rPUPF &=0x1bf;//pull up pf6
    rSBRDR=0x40;
    rIVTCNT=0;

    rSIOCON=1|(0<<2)|(0<<4)|(1<<5)|(0<<6);//rxd and txd,and sio intrupt mode
   	
    Uart_Printf("请点击触摸屏,观察相应坐标显示变化情况:\n");
    while(1);
}
 



⌨️ 快捷键说明

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