📄 touch.c
字号:
/************************************************************************************
优龙科技
www.ucdragon.cn
By -- yj;
V1.0
*************************************************************************************/
#include "LPC177x_8x.h" /* LPC17xx definitions */
#include "lpc177x_8x_adc.h"
#include "includes.h"
#include "touch.h"
#include "GUI_X.h"
#include "LCDConf.h"
unsigned char tmp=0;
unsigned short x_vaule = 0, y_vaule = 0; /* Globals used for display */
/*----------------------------------------------------------------------------
initialize TOUCH Pins
*----------------------------------------------------------------------------*/
void touch_init(void){
ADC_Init(LPC_ADC, 400000);
LPC_IOCON -> P0_12 = IO_Pull_up; //P0.12 set gpio pullup
LPC_GPIO0 -> DIR &= ~(0x1<<XR); //P0.12 input
LPC_IOCON -> P0_23 = IO_No_UD;
LPC_GPIO0 -> DIR &= ~(0x1<<XL); //P0.23 input
LPC_IOCON -> P0_24 = IO_Pull_up; //P0.24 set gpio pullup
LPC_GPIO0 -> DIR |= (0x1<<YD); //P0.24 output dir
LPC_GPIO0 -> CLR |= (0x1<<YD); //P0.24 output 0
LPC_IOCON -> P0_25 = IO_Pull_up; //P0.25 set gpio pullup
LPC_GPIO0 -> DIR |= (0x1<<YU); //P0.25 output dir
LPC_GPIO0 -> CLR |= (0x1<<YU); //P0.25 output 0
GUI_X_Delay(10);
}
/*----------------------------------------------------------------------------
config the X Pins for touch
*----------------------------------------------------------------------------*/
void config_pins_x(void){
LPC_IOCON -> P0_25 = IO_No_UD; //P0.25 set gpio no pullup and no pulldown
LPC_GPIO0 -> DIR &= ~(0x1<<YU); //P0.25 input
LPC_IOCON -> P0_24 = ADC_In; //P0.24 set gpio pullup
LPC_IOCON -> P0_23 = IO_Pull_up; //P0.23 set gpio pullup
LPC_GPIO0 -> DIR |= (0x1<<XL); //P0.23 output dir
LPC_GPIO0 -> CLR |= (0x1<<XL); //P0.23 output 0
LPC_IOCON -> P0_12 = IO_Pull_up; //P0.12 set gpio pullup
LPC_GPIO0 -> DIR |= (0x1<<XR); //P0.12 output dir
LPC_GPIO0 -> SET |= (0x1<<XR); //P0.12 output 1
GUI_X_Delay(10);
}
/*----------------------------------------------------------------------------
config the Y Pins for touch
*----------------------------------------------------------------------------*/
void config_pins_y(void){
LPC_IOCON -> P0_12 = IO_No_UD; //P0.12 set gpio no pullup and no pulldown
LPC_GPIO0 -> DIR &= ~(0x1<<XR); //P0.12 input
LPC_IOCON -> P0_23 = ADC_In; //P0.23 set gpio pullup
LPC_IOCON -> P0_24 = IO_Pull_up; //P0.24 set gpio pullup
LPC_GPIO0 -> DIR |= (0x1<<YD); //P0.24 output dir
LPC_GPIO0 -> SET |= (0x1<<YD); //P0.24 output 1
LPC_IOCON -> P0_25 = IO_Pull_up; //P0.25 set gpio pullup
LPC_GPIO0 -> DIR |= (0x1<<YU); //P0.25 output dir
LPC_GPIO0 -> CLR |= (0x1<<YU); //P0.25 output 0
GUI_X_Delay(10);
}
/*----------------------------------------------------------------------------
read the x position
*----------------------------------------------------------------------------*/
unsigned short read_ch_x(void){
uint16_t X_coor,adc_value_x[NUM_samples]=0;
uint32_t i,num;
config_pins_x();
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_1, ENABLE);
for(num=0;num<NUM_samples;num++){
ADC_StartCmd(LPC_ADC, ADC_START_NOW);
//Wait conversion complete
while (!(ADC_ChannelGetStatus(LPC_ADC, ADC_CHANNEL_1, ADC_DATA_DONE)));
for(i=0;i<=100;i++);
adc_value_x[num] = ADC_ChannelGetData(LPC_ADC, ADC_CHANNEL_1);
}
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_1, DISABLE);
X_coor = (adc_value_x[2]+ adc_value_x[3])*LCD_XSIZE/(2*4096) ;
return X_coor;
}
/*----------------------------------------------------------------------------
read the y position
*----------------------------------------------------------------------------*/
unsigned short read_ch_y(void){
uint16_t Y_coor,adc_value_y[NUM_samples]=0;
uint32_t i,num;
config_pins_y();
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_0, ENABLE);
for(num=0;num<NUM_samples;num++){
ADC_StartCmd(LPC_ADC, ADC_START_NOW);
//Wait conversion complete
while (!(ADC_ChannelGetStatus(LPC_ADC, ADC_CHANNEL_0, ADC_DATA_DONE)));
for(i=0;i<=100;i++); //延时,重要!
adc_value_y[num] = ADC_ChannelGetData(LPC_ADC, ADC_CHANNEL_0);
}
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_0, DISABLE);
Y_coor = (adc_value_y[2]+ adc_value_y[3])*LCD_YSIZE/(2*4096) ;
return Y_coor;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -