Wei Dongshan ARM Bare Metal and uboot Encyclopedia (1st Enhanced Edition) Study Notes 18-Lesson 018_ADC and Touch Screen_Sections 003~005_Resistive Touch Screen Principle and Programming

The principle of a resistive touch screen

  1. Basic principle: ADC measures the built-in resistance of the touch screen in real time. The position of the contact is different, the resistance measured by the ADC in real time is also different. Touch screen and LCD are two devices, and touch screen is a layer of film covering the LCD.
    Insert picture description here
    When the screen is clicked, it will be pressed.
    ①Measure the coordinates in the X direction: XP connects to 3.3V, XM connects to GND; YP and YM are not connected to power; measure the YP voltage to get the X coordinate.
    ②Measure Y coordinate: YP is connected to 3.3V, YM is connected to GND; XP, XM is not connected to power; Y coordinate can be obtained by measuring XP voltage.
  2. Touch screen use process:
    ①Press the touch screen to generate a touch interrupt ->
    ②In the touch interrupt program: start ADC (purpose: get XY coordinates) ->
    ③Wait for ADC completion, generate ADC interrupt ->
    ④In ADC interrupt program: ADC interrupt Read the XY coordinates in the middle ->
    ⑤Start the timer ->
    ⑥The timer interrupt occurs, and judge whether the touch screen is still pressed? ->
    ⑦Press: jump to ②; release: end the process
    Insert picture description here
    Insert picture description here

Two S3C2440 ADC & TOUCH SCREEN chip manual study

Insert picture description here
Touch screen interrupt processing flow:
Insert picture description here

Three programming process

  1. Initialize the ADC/TC interface:
  2. The touch screen is not pressed at the beginning, and the TS is set to "waiting for interrupt mode"
  3. Set interrupt control register: INTSUBMASK/INTMSK
  4. Press the touch screen to enter TS interrupt: ①Enter automatic acquisition mode; ②Start ADC
  5. ADC interrupt: ①Read data; ②Enter the "waiting interrupt mode" again; ③Start the timer
  6. Timer interrupt: ①If you release the touch, it ends; ②If it is still pressed, continue to execute. The
    program flow chart is as follows (not including timer interrupt):
    Insert picture description here

Four program code implementation process (only realize the touch screen press and release interrupt)

Code:

//总初始化函数
void touchscreen_init(void)
{
    
    
//1. 初始化ADC/TC接口;
	adc_ts_reg_init();
//2.设置中断控制寄存器:INTSUBMASK/INTMSK
	adc_ts_int_init();
//3. 开始触摸屏没有按下,设置TS处于“等待中断模式”
	enter_wait_pen_down_mode();
}
//4. 按下触摸屏,进入TS中断:①进入自动采集模式;②启动ADC
//5. ADC中断:①读数据;②再次进入“等待中断模式”;③启动定时器
//6. 定时器中断:①若松开触摸,结束;②若仍然按下,继续执行
//1 ADC/TC接口初始化函数
void adc_ts_reg_init(void)
{
    
    
	/* [15] : ECFLG,  1 = End of A/D conversion
	 * [14] : PRSCEN, 1 = A/D converter prescaler enable
	 * [13:6]: PRSCVL, adc clk = PCLK / (PRSCVL + 1)
	 * [5:3] : SEL_MUX, 000 = AIN 0
	 * [2]   : STDBM
	 * [0]   : 1 = A/D conversion starts and this bit is cleared after the startup.
	 */
	ADCCON = (1<<14) | (49<<6) | (0<<3);
	ADCDLY = 0xff;	
}

//2 ADC/TS中断初始化函数 
#define ADC_INT_BIT (10)
#define TC_INT_BIT  (9)
void adc_ts_int_init (void)
{
    
    
	/*为了避免误触发,清除中断标志位*/
	SUBSRCPND = (1<<TC_INT_BIT) | (1<<ADC_INT_BIT);
	/* 注册中断处理函数 */
	//中断总开关已经设置过了
	register_irq(31, AdcTsIntHandle);//31:中断号,	AdcTsIntHandle:中断处理函数
	/* 使能中断 */
	//中断子开关
	//ADC中断为INTSUBMSK寄存器bit10,
	//TC中断为INTSUBMSK寄存器bit9
	INTSUBMSK &= ~((1<<ADC_INT_BIT) | (1<<TC_INT_BIT));
}

//3 开始触摸屏没有按下,设置TS处于“等待中断模式”
//SS开关闭合,YM开关闭合,其余开关断开

/* ADCTSC's bits */
#define WAIT_PEN_DOWN    (0<<8)
#define WAIT_PEN_UP      (1<<8)

#define YM_ENABLE        (1<<7)
#define YM_DISABLE       (0<<7)

#define YP_ENABLE        (0<<6)
#define YP_DISABLE       (1<<6)

#define XM_ENABLE        (1<<5)
#define XM_DISABLE       (0<<5)

#define XP_ENABLE        (0<<4)
#define XP_DISABLE       (1<<4)

#define PULLUP_ENABLE    (0<<3)
#define PULLUP_DISABLE   (1<<3)

#define AUTO_PST         (1<<2)

#define WAIT_INT_MODE    (0b11)//等待中断
#define NO_OPR_MODE      (0)
void enter_wait_pen_down_mode(void)
{
    
    
	ADCTSC = WAIT_PEN_DOWN | PULLUP_ENABLE | YM_ENABLE | YP_DISABLE | XP_DISABLE |XM_DISABLE | WAIT_INT_MODE;
}

//4.1 中断服务函数
void AdcTsIntHandle(int irq)
{
    
    
	/* 如果是触摸屏中断 */
	if (SUBSRCPND & (1<<TC_INT_BIT))  
		Isr_Tc();

//	if (SUBSRCPND & (1<<ADC_INT_BIT))  /* ADC中断 */
//		Isr_Adc();

	/*清除中断*/
	SUBSRCPND = (1<<TC_INT_BIT) | (1<<ADC_INT_BIT);
}

//4.1.1 中断服务子函数-触摸屏中断处理函数
void Isr_Tc(void)
{
    
    
	//bit15=1表示up
	if (ADCDAT0 & (1<<15))
	{
    
    
		void enter_wait_pen_down_mode();
		printf("pen up\n\r");
	}
	//bit15=0表示down
	else	
	{
    
    
		/*需要检测触摸笔松开*/
		enter_wait_pen_up_mode();
		printf("pen down\n\r");
	}
}
void enter_wait_pen_up_mode(void)
{
    
    
	ADCTSC = WAIT_PEN_UP | PULLUP_ENABLE | YM_ENABLE | YP_DISABLE | XP_DISABLE | XM_DISABLE | WAIT_INT_MODE;
}

Guess you like

Origin blog.csdn.net/xiaoaojianghu09/article/details/104352839