Wei Dongshan ARM Bare Metal and uboot Encyclopedia (1st Enhanced Edition) Study Notes 18-Lesson 018_ADC and Touch Screen_Sections 009~012_Touch Screen Programming_Comparison Principle

Reference article: Five-point correction algorithm for STM32f103 resistive touch screen

A calibration principle

  1. As shown in the figure below, if the bottom is LCD, the top is touch screen film (TS). Assuming that the resolution is 480*272, then where the LCD coordinates are (0,0), the point coordinates corresponding to TS are (x1', y1'). Where LCD coordinates are (479,271), the point corresponding to TS is (x2', y2').
    So, if the coordinates of the touch point are (x', y'), what about the point coordinates of the corresponding LCD?
    Insert picture description here

  2. To simplify, let’s look at the coordinates of the x-axis first:
    Insert picture description here
    from the above figure we can get an equation,

(x' - x1')/(x2' - x1') = (x - 0)/(479 - 0)
 x = (479 - 0)*(x' - x1')/(x2' - x1')
  1. But in the above picture, we have chosen the boundary points (0,0) and (479,271). Generally, we are not accurate at this kind of point, so we choose the general point below.
    Insert picture description here
(x' - x1')/(x2' - x1') = (x - x1)/(x2 - x1)
x = (x2 - x1)*(x' - x1')/(x2' - x1') + x1

假设 k  = (x2 - x1)*(x' - x1'),
所以
x = k * (x' - x1') + x1
X轴方向:
Kx = (s1 + s2) / (s1' + s2') = 2s / (s1' + s2')     -----------   (假设的s1 = s2)
Y轴方向:
Ky = (d1 + d2) / (d1' + d2') = 2d / (d1' + d2')   ----------   (假设d1 = d2)

Insert picture description here
Select the coordinates of the TS center point as (xc', yc') and the coordinates of the LCD center point as (xc, yc),
then the coordinates of the corresponding point on the LCD can be obtained from the point clicked by the TS.

x = (x' - xc')*Kx + xc
y = (y' - yc')*Ky + yc

2. Code writing for calibration and line drawing

Code design steps:

1. 在A点显示十字架-“+”------------------------------------------------->fb_disp_cross(int x,int y)
2. 客户点击十字架-“+”
3. 记录触摸屏坐标-------------------------------------------------------->ts_read_raw()
4. 在B点/C点/D点/E点重复步骤1.~3.
5. 根据这些数据确定上述公式的各个参数-------------------------->ts_calibrate()
6. 后续即可根据以上公式由触点坐标转换出LCD的坐标-------->ts_read()

The main function execution flow:

main.c->
touchscreen_test()->
ts_calibrate()->
get_calibrate_point_data()//A点B点C点D点E点绘制“+”并读取触摸屏点值->
fb_disp_cross()//绘制“+”->
ts_read_raw()//等待点击并回读位置数据。注:此时已经获得所有点的触摸屏坐标和LCD坐标->

is_ts_xy_swap()//判断触摸屏XY坐标是否反->
swap_xy()//如果反需要调换->
XXXX//计算各个校准参数->

//实际应用调准参数进行计算
while (1)
{
    
    
	ts_read(&x, &y)
	{
    
    
		ts_read_raw(&ts_x, ts_y);//读取触摸屏原始坐标
		if (g_ts_xy_swap)
		{
    
    
			swap_xy(&ts_x, &ts_y);//调换触摸屏原始坐标
		}
		/* 使用公式计算 */
		*lcd_x = g_kx * (ts_x - g_ts_xc) + g_lcd_xc;
		*lcd_y = g_ky * (ts_y - g_ts_yc) + g_lcd_yc;
	}
	fb_put_pixel(x, y, 0xff00);
}

Interrupt execution:

等待触摸屏中断->
进入触摸屏中断函数->
启动自动测量模式,启动ADC中断->
ADC中断程序执行,读取触摸屏的点位->
置位标志位g_ts_data_valid提示主程序继续执行

Three debugging process-discover and solve problems

  1. Problem: When the touch screen is pressed, the low-level timer interrupt has been executed to start the ADC, and the ADC generates a new interrupt to generate touch screen data; the old data is directly lost; and the touch screen data from the moment of pressing to releasing the touch screen is very poor Stability causes problems with calibration data generation.
    Solution: Accumulate multiple times to average. In the ADC interrupt, the conversion result is reported and the data valid bit is set after 16 sampling points are required.
  2. Problem: When the A point "+" is displayed, the touch screen is pressed; when the touch screen is released, the touch release interrupt of the touch screen first triggers the call to the report_ts_xy(0, 0, 0);underlying ADC converter conversion completion interrupt. Later, it is detected that the touch screen has been released at this time. Call again report_ts_xy(0, 0, 0);.
    The interval between two calls can be up to 10ms: the AD conversion completion interrupt has just been triggered (at this time, the timer interrupt is started again) -> touch screen release -> touch release interrupt trigger call report_ts_xy(0, 0, 0);-> timer interrupt start enable AD conversion completion interrupt -> The AD conversion is completed and the interrupt is triggered to call again report_ts_xy(0, 0, 0);.
    When the last AD conversion is completed and the interrupt is completed, the program has started to execute the "+" that shows point B. Therefore, this report is considered to be the report of B point touch screen data.
    Solution: Set the threshold condition before the program reads the current touch screen coordinate point: the key must be pressed to continue downward execution, thereby intercepting the false alarm of the interruption of the last AD conversion completion in the problem description.
  3. Problem: When the calculated LCD coordinate value after calibration needs to be judged illegally, if it exceeds the screen resolution, it needs to return an exception.
  4. Refer to the tslib library on the web.

Four continue to improve

Problem: a. Click on the touch screen and there will be two dots; b. Long-press the dots on the LCD will become larger and larger;
Reasons:
1. Calibration is very important: the coordinate values ​​converted by ADC are unstable and need to be calibrated many times Sampling and averaging (not only averaging in ADC interrupt);
2. In the timer interrupt service program: ADCDAT0'BIT 15 can be used to judge the status of the touch pen only in the "waiting interrupt mode"; when waiting for trigger 16 During the second AD interruption, the automatic measurement mode has been entered, so when entering the timing interruption, you should exit directly.
Insert picture description here
3. When starting AD, you should not enter the waiting interrupt mode, it will affect the data (the touch screen control switches are all off at this time)

Refer to tslib:
a. Use a matrix for calibration, which is more adaptable;
b. Use multiple methods to eliminate errors:
take the average of multiple measurements;
judge the distance between adjacent points, if the sudden change is large, there may be an error value
c .…

Guess you like

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