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

📄 touch.c

📁 嵌入式触摸屏实验
💻 C
字号:
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\Touch.h"
#include "..\inc\def.h"

volatile int xPos = 320/2;
volatile int yPos = 240/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 sioTxStr;
volatile int touch_x[3];
volatile int touch_y[3];

volatile int ms_tick = 0;
volatile int peg_tick = 0;

U8 flag=0;
S16 Positionx0=0;//54;
S16 Positiony0=0;//40;
S16 Positionx1=320;//470;
S16 Positiony1=240;//395;

S16 fp0;
S16 fp1;

U8 Adjust(void);
void __irq Timer1Done(void);

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

    switch(sioTxStr)
    {
      case 0x00:
           sioTxStr = 0x01;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x01:
           sioTxStr = 0x02;
           touch_x[touch_check_count] = rSIODAT;
           touch_x[touch_check_count] <<= 8;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x02:
           sioTxStr = 0x03;
           touch_x[touch_check_count] += rSIODAT;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x03:
           sioTxStr = 0x04;
           rSIODAT = 0xd3;
           rSIOCON|=(1<<3);
           break;
      case 0x04:
           sioTxStr = 0x05;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x05:
           sioTxStr = 0x06;
           touch_y[touch_check_count] = rSIODAT;
           touch_y[touch_check_count] <<= 8;
           rSIODAT = 0x00;
           rSIOCON|=(1<<3);
           break;
      case 0x06:
           sioTxStr = 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
             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
             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;
             touch_x[1] = (touch_x[1] - Positionx0)*320/(Positionx1 - Positionx0); //校准

             touch_y[1] >>= 6;
             touch_y[1] = (touch_y[1]- Positiony0)*240/(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)Uart_Printf("%03d  %03d\n",xPos,yPos);
 else {if (!flag){
	   fp0=xPos;
	   fp1=yPos;
	   flag++;
	Uart_Printf("\n OK! 请点击右下方的黑色方块!");
		}
       else if(flag==1){
			if(abs(xPos-fp0)<20 | abs(yPos-fp1)<20)
				{Uart_Printf("\n错误,再来一次!");
				 return 0;}
			Positionx1=xPos;
	   		Positiony1=yPos;
			Positionx0=fp0;
			Positiony0=fp1;
			flag++;
			Uart_Printf("Ok!it's over!.\n");
			Uart_Printf("Positionx0=%d, Positiony0=%d, Positionx1=%d,Positiony1=%d\n\n", 
                                                        Positionx0,Positiony0,Positionx1,Positiony1);
			} 
	}
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
           sioTxStr = 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;
      }
   }
 
}



/****************************************
 *  PWM Timer1 Interrupt test           *
 ****************************************/
void Test_TimerInt(void)
{
    ms_tick = 0;

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

    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)Sio_Int;
    rINTMSK &= (~BIT_SIO);//enable sio intrupt 

    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
    
}
 

void __irq Timer1Done(void)
{
    rI_ISPC |= BIT_TIMER1;
    ms_tick ++;
    
    if(ms_tick == 50)
    {
       ms_tick = 0;
       peg_tick++;       
    }
    

    Touch_check();
}

⌨️ 快捷键说明

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