bsp_touchscr.c
来自「lpc2478+ucosII+ucgui源码」· C语言 代码 · 共 447 行 · 第 1/2 页
C
447 行
BSP_TouchScr_Dly_uS(BSP_TOUCH_SCR_SETUP_DLY_uS); /* Wait for I/O setup time */
adc_acc = 0;
for (i = 0; i < BSP_TOUCH_SCR_NBR_SAMPLES; i++) {
adc_acc += BSP_ADC_GetStatus(BSP_ADC_Y1);
BSP_TouchScr_Dly_uS(BSP_TOUCH_SCR_ADC_DLY_uS);
}
adc_acc /= BSP_TOUCH_SCR_NBR_SAMPLES;
p_status->TouchScrIsPressed = DEF_TRUE;
p_status->TouchScrX = (CPU_INT16U)adc_acc;
}
BSP_TouchScr_IO_Init(); /* Set the I/O to a common know state */
}
/*
*********************************************************************************************************
* BSP_TouchScr_MeasureY()
*
* Description : Measure the Y coordinate of the Touch Screen.
* Touch Screen Hardware on the LPC2478-SK
*
* (Y2) P0.21
* |
* +---------|----------+
* | | |
* | +==|==+ |
* | | | |
* | +===============+ |
* P0.22 (X2)<--|--| R1 |-|-->(X1) P0.24/AD[1]
* | +===============+ |
* | | R2 | |
* | | | |
* | +==|==+ |
* +=========|==========+
* |
* (Y1) P0.23/AD[0]
* (1) Set Y1 and Y2 as pull-up outputs and X2 as pull-down input and X1 as ADC channel 1.
* (2) If X2 is LOW the Touch screen is not being pressed go to step 5.
* (3) Measure ADC Channel 1 input BSP_TOUCH_SCR_NBR_SAMPLES times.
* (4) Average the result and return the data.
* (5) Set Y1, Y2, X1, X2 as inputs with pull-up resistor.
*
* Argument(s) : p_status Pointer to the BSP_TOUCH_SCR_STATUS structure.
*
* Return(s) : none.
*
* Caller(s) : Application
*
* Note(s) : none.
*********************************************************************************************************
*/
void BSP_TouchScr_MeasureY (BSP_TOUCH_SCR_STATUS *p_status)
{
CPU_INT32U adc_acc;
CPU_INT32U i;
IO0DIR |= BSP_TOUCH_SCR_IO0DIR_Y_GRP; /* Configure Y1 and Y2 as outputs */
IO0SET = BSP_TOUCH_SCR_IO0DIR_Y_GRP; /* Set Y1 and Y2 to '1' */
PINSEL1 |= BSP_TOUCH_SCR_PINSEL1_X1_ADC; /* Select X1 as ADC channel 1 */
PINMODE1 |= BSP_TOUCH_SCR_PINMODE1_X2_PULL_DOWN; /* Configure X2 with a Pull-Down resistor */
BSP_TouchScr_Dly_uS(BSP_TOUCH_SCR_SETUP_DLY_uS); /* Wait for I/O Setup time */
if (DEF_BIT_IS_CLR(IO0PIN, BSP_TOUCH_SCR_IO0DIR_X2)) { /* If X2 is LOW the Touch Screen is not being pressed */
p_status->TouchScrIsPressed = DEF_FALSE;
} else { /* If X2 is HIGH the Touch Screen is being pressed */
IO0CLR = BSP_TOUCH_SCR_IO0DIR_Y2; /* Reset Y2 to '0' so current will drain between Y1->Y2 */
BSP_TouchScr_Dly_uS(BSP_TOUCH_SCR_SETUP_DLY_uS); /* Wait for I/O setup time */
adc_acc = 0;
for (i = 0; i < BSP_TOUCH_SCR_NBR_SAMPLES; i++) {
adc_acc += BSP_ADC_GetStatus(BSP_ADC_X1);
BSP_TouchScr_Dly_uS(BSP_TOUCH_SCR_ADC_DLY_uS);
}
adc_acc /= BSP_TOUCH_SCR_NBR_SAMPLES; /* Average the ADC samples result */
p_status->TouchScrIsPressed = DEF_TRUE;
p_status->TouchScrY = (CPU_INT16U)adc_acc;
}
BSP_TouchScr_IO_Init(); /* Set the I/O to a common know state */
}
/*
*********************************************************************************************************
* BSP_TouchScr_Convert()
*
* Description : Convert the X and Y coordenates form ADC value to Pixels value.
* Touch Screen Hardware on the LPC2478-SK
*
* (Y2) P0.21
* |--- 320 (pixels) ---|
* +---------|----------+ -----
* | | | |
* | +==|==+ | |
* | | | | | (240 pixels)
* | +===============+ | |
* P0.22 (X2)<--|--| R1 |-|-->(X1) P0.24/AD[1]
* | +===============+ | |
* | | R2 | | |
* | | | | |
* | +==|==+ | |
* +=========|==========+ ----|
* |
* (Y1) P0.23/AD[0]
*
* Argument(s) : p_status Pointer to the BSP_TOUCH_SCR_STATUS structure.
*
* Return(s) : none.
*
* Caller(s) : Application
*
* Note(s) : none.
*********************************************************************************************************
*/
void BSP_TouchScr_Convert (BSP_TOUCH_SCR_STATUS *p_status)
{
CPU_INT32U x_pixels;
CPU_INT32U y_pixels;
x_pixels = (p_status->TouchScrX - BSP_TOUCH_SCR_MIN_X_ADC)
* (BSP_TOUCH_SCR_H_SIZE);
x_pixels /= (BSP_TOUCH_SCR_DELTA_X_ADC);
y_pixels = (BSP_TOUCH_SCR_MAX_Y_ADC - p_status->TouchScrY)
* BSP_TOUCH_SCR_V_SIZE;
y_pixels /= (BSP_TOUCH_SCR_DELTA_Y_ADC);
p_status->TouchScrX = x_pixels;
p_status->TouchScrY = y_pixels;
}
/*
*********************************************************************************************************
* BSP_TouchScr_Dly()
*
* Description : Delay for the specified number of micro seconds
*
* Argument(s) : us number of microseconds to delay.
*
* Return(s) : none.
*
* Caller(s) : BSP_TouchScr_MeasureX()
* BSP_TouchScr_MeasureY()
*
* Note(s) : none.
*********************************************************************************************************
*/
static void BSP_TouchScr_Dly_uS (CPU_INT32U us)
{
CPU_INT32U pclk_freq;
CPU_INT32U cpu_freq;
CPU_INT32U tmr_dly;
CPU_INT32U dflt_dly;
pclk_freq = BSP_CPU_PclkFreq(BSP_PCLK_TIMER3);
cpu_freq = BSP_CPU_ClkFreq();
tmr_dly = (pclk_freq / 1000000L) * us ;
dflt_dly = (cpu_freq / 1000000L) * BSP_TOUCH_SCR_DFLT_DLY_uS;
T3TCR = 2; /* Disable timer 0 and reset. */
T3PC = 0; /* Prescaler is set to no division. */
T3CCR = 0; /* Capture is disabled. */
T3EMR = 0; /* No external match output. */
T3TCR = 1; /* Enable timer 0 */
while (((CPU_INT32U)T3TC < tmr_dly) && /* If the Timer is stopped by the application the default... */
(CPU_INT32U)dflt_dly > 0 ) { /* delay counter is used. */
dflt_dly--;
}
}
/*
*********************************************************************************************************
* BSP_TouchScr_IO_Init()
*
* Description : Initializes the Touch Screen I/Os as inputs with Pull-Up resistor
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Caller(s) : BSP_TouchScr_Init()
* BSP_TouchScr_MeasureX()
* BSP_TouchScr_MeasureY()
*
* Note(s) : (1) In order to save power while a conversion is not required the Touch Screen I/O are set to
inputs with a pull-up resistor.
*********************************************************************************************************
*/
static void BSP_TouchScr_IO_Init (void)
{
IO0DIR &= ~(BSP_TOUCH_SCR_IO0DIR_Y_GRP
| BSP_TOUCH_SCR_IO0DIR_X_GRP);
PINSEL1 &= ~(BSP_TOUCH_SCR_PINSEL1_X1_MASK
| BSP_TOUCH_SCR_PINSEL1_X2_MASK
| BSP_TOUCH_SCR_PINSEL1_Y1_MASK
| BSP_TOUCH_SCR_PINSEL1_Y2_MASK);
PINMODE1 &= ~(BSP_TOUCH_SCR_PINMODE1_X1_MASK
| BSP_TOUCH_SCR_PINMODE1_X2_MASK
| BSP_TOUCH_SCR_PINMODE1_Y1_MASK
| BSP_TOUCH_SCR_PINMODE1_Y2_MASK);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?