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

📄 touch_screen.c

📁 Keil工程例子,NXP新出的芯片lpc2478的示例代码
💻 C
字号:
/***********************************************************************
 * File:   touch_screen.c
 * Rev:    1.0
 * Author: Chun Sing Chu
 * Date:   March 26 2007
 *
 * Description:
 *     This file contains driver support for the LCD panel touch screen.
 *
 * Revision History:
 * Rev 1.0 March 26 2007
 * Initial revision.
 * 
 **********************************************************************/
#include "LPC24xx.h"
#include "touch_screen.h"


INT32U x_values[num_samples]; // array to store x_samples
INT32U y_values[num_samples]; // array to store y_samples

/***********************************************************************
 *
 * Function: timer_delay
 *
 * Purpose: Start the timer until delay elapsed.
 *
 * Processing:
 *
 * Parameters: 	Delay
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void timer_delay(INT32U count)
{
    T0TCR = 0x00000002;           /* disable and reset the timer */
    T0CTCR = 0;                   /* timer mode */
    T0MR0 = count;                /* desired count */
    T0PR = 0;
    T0PC = 0;
    T0MCR = 7;                    /* reset timer , stop, and set flag on match */
    while (T0IR & 1);             /* wait for match flag */
    T0IR |= 1;                    /* clear the IR bit */
}

/***********************************************************************
 *
 * Function: config_pins_x
 *
 * Purpose: config_pins_x.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void config_pins_x (void)
{
    PINSEL1 &= ~(X_minus_mask);   /* X- is digital I/O */
    PINMODE1 &= ~(X_minus_mask);  /* clear the two bits */
    PINMODE1 |= X_minus_no_pull;  /* no pullup on X- */
   FIO0DIR |= X_minus;            /* X- is an output */
    FIO0CLR |= X_minus;            /* make X- low */

    PINSEL1 &= ~(Y_minus_mask);   /* Y- is digital I/O */
    PINMODE1 &= ~(Y_minus_mask);  /* clear the two bits */
    PINMODE1 |= Y_minus_no_pull;  /* no pullup on Y- */
    FIO0DIR &= ~(Y_minus);         /* Y- is an input */
    
    
    PINSEL1 &= ~(X_plus_mask);    /* X+ is digital I/O */
    PINMODE1 &= ~(X_plus_mask);   /* clear the two bits */
    PINMODE1 |= X_plus_no_pull;   /* no pullup on X+ */
    FIO0DIR |= X_plus;             /* X+ is an output */
    FIO0SET |= X_plus;             /* make X+ high */

    PINSEL1 &= ~(Y_plus_mask);    /* Y+ is digital I/O */
    PINSEL1 |= ADC_on_Y;          /* Y+ is an ADC pin */
    //PINMODE1 &= ~(Y_plus_mask);   /* clear the two bits */
    //PINMODE1 |= Y_plus_no_pull;   /* no pullup on Y+ */
}

/***********************************************************************
 *
 * Function: read_ch_x
 *
 * Purpose: read_ch_x.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void read_ch_x (void)
{
    INT32U i;
    config_pins_x();              /* configure pins for read x-dir */
  //  timer_delay(settling);        /* settling time for switching */
       i=2000;
    while(i--);
    AD0CR = 0x00200304;  
    //  AD0CR = 0x00200302;           /* Power up, PCLK/4, sel ADC0.1 */
    for (i=0; i < num_samples; i++)
    {
        AD0CR |= 0x01000000;      /* Start A/D conversion */
        while (AD0DR2 & 0x80000000);
    //    while (ADDR1 & 0x80000000); /* wait conversion completed */
       // x_values[i] = ((ADDR1 >> 6) & 0x3FF); /* store result */
         x_values[i] = ((AD0DR2 >> 6) & 0x3FF);
    }
}

/***********************************************************************
 *
 * Function: config_pins_y
 *
 * Purpose: config_pins_y.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void config_pins_y(void)
{
    PINSEL1 &= ~(X_minus_mask);   /* X- is digital I/O */
    PINMODE1 &= ~(X_minus_mask);  /* clear the two bits */
    PINMODE1 |= X_minus_no_pull;  /* no pullup on X- */
    FIO0DIR &= ~(X_minus);         /* X- is an input */
    
    PINSEL1 &= ~(Y_minus_mask);   /* Y- is digital I/O */
    PINMODE1 &= ~(Y_minus_mask);  /* clear the two bits */
    PINMODE1 |= Y_minus_no_pull;  /* no pullup on Y- */
    FIO0DIR |= Y_minus;            /* Y- is an output */
    FIO0CLR |= Y_minus;            /* make Y- low */

    PINSEL1 &= ~(X_plus_mask);    /* X+ is digital I/O */
    PINSEL1 |= ADC_on_X;          /* X+ is an ADC pin */
    
    PINMODE1 &= ~(X_plus_mask);   /* clear the two bits */
    PINMODE1 |= X_plus_no_pull;   /* no pullup on X+ */
    
    PINSEL1 &= ~(Y_plus_mask);    /* Y+ is digital I/O */
    PINMODE1 &= ~(Y_plus_mask);   /* clear the two bits */
    PINMODE1 |= Y_plus_no_pull;   /* no pullup on Y+ */
    FIO0DIR |= Y_plus;             /* Y+ is an output */
    FIO0SET |= Y_plus;             /* make Y+ high */
}

/***********************************************************************
 *
 * Function: read_ch_y
 *
 * Purpose: read_ch_y.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void read_ch_y(void)
{
    INT32U i;
    config_pins_y();              /* configure pins for read y-dir */
  // timer_delay(settling); /* settling time for switching */
    i=2000;
    while(i--);
    AD0CR = 0x00200302;  
  //  AD0CR = 0x00200301;           /* Power up, PCLK/4, sel ADC0.0 */
    for (i=0; i < num_samples; i++)
    {
        AD0CR |= 0x01000000;      /* Start A/D conversion */
       while (AD0DR1 & 0x80000000); /* wait until completed */
       y_values[i] = ((AD0DR1 >> 6) & 0x3FF); /* store result */
     //   while (ADDR0 & 0x80000000); /* wait until completed */
       // y_values[i] = ((ADDR0 >> 6) & 0x3FF); /* store result */
    }
}

/***********************************************************************
 *
 * Function: config_pins_touch
 *
 * Purpose: config_pins_touch.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void config_pins_touch(void)
{
    PINSEL1 &= ~(X_minus_mask);   /* X- is digital I/O */
    PINMODE1 &= ~(X_minus_mask);  /* clear the two bits */
    PINMODE1 |= X_minus_no_pull;  /* no pullup on X- */
    FIO0DIR &= ~(X_minus);          /* X- is an input */

    PINSEL1 &= ~(Y_minus_mask);   /* Y- is digital I/O */
    PINMODE1 &= ~(Y_minus_mask);  /* clear the two bits */
    PINMODE1 |= Y_minus_no_pull;  /* no pullup on Y- */
    FIO0DIR |= Y_minus;            /* Y- is an output */
    FIO0CLR |= Y_minus;            /* make Y- low */

    PINSEL1 &= ~(X_plus_mask);    /* X+ is digital I/O */
    PINMODE1 &= ~(X_plus_mask);   /* pullup on X+ */
    FIO0DIR &= ~(X_plus);          /* X+ is an input */
    
    PINSEL1 &= ~(Y_plus_mask);    /* Y+ is digital I/O */
    PINMODE1 &= ~(Y_plus_mask);   /* clear the two bits */
    PINMODE1 |= Y_plus_no_pull;   /* no pullup on Y+ */
    FIO0DIR &= ~(Y_plus);          /* Y+ is an input */
}

/***********************************************************************
 *
 * Function: touch_detect
 *
 * Purpose: touch_detect.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: true if touch is detected
 *
 **********************************************************************/
INT32U touch_detect (void)
{   INT32U m;
    config_pins_touch();          /* configure pins for touch detection */
   //timer_delay(settling);        /* settling time for switching */
    for(m=0;m<10000;m++);
    return ((IOPIN0 & X_plus) ^ X_plus); /* return true if touch is detected */
}

/***********************************************************************
 *
 * Function: touch_init
 *
 * Purpose: touch_init.
 *
 * Processing:
 *
 * Parameters: 	None
 *
 * Outputs: None
 *
 * Returns: None
 *
 **********************************************************************/
void touch_init(void)
{
    PCONP |= (1 << 12);           /* Enable power to AD block */
	SCS = SCS|1;
    touch_detect();               /* setup for touch detection */
   // install_irq(17, (void *)gpio1_handler, 1);
    IO0_INT_EN_F = X_plus;           /* enable falling edge X-plus interrupt */
}

⌨️ 快捷键说明

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