TFT-LCD screen touch calibration

TFT-LCD screen touch calibration

touch screen classification

1. Resistive screen

The main part of the resistive screen is a resistive thin film screen that matches the surface of the display very well. A layer of hardened, smooth, scratch-resistant plastic on the outside is also coated on the inside with a transparent conductive layer separated by many small (less than one-thousandth of an inch) transparent spacers. Open insulation. When a finger touches the screen, the normally insulated two-layer conductive layer has a contact at the touch point. After the controller detects this connection, one of the conductive layers is connected to the 5V uniform voltage field in the direction of the У axis, and the other The conductive layer leads the voltage of the contact point to the control card for A/D conversion. After obtaining the voltage value and comparing it with 5V, the coordinates of the y-axis of the touch point can be obtained. Similarly, the coordinates of the x-axis can be obtained. This is what all resistive technology touch screens Common fundamental principles.

2. Capacitive screen

The capacitive screen works by using the current induction of the human body. When touching the capacitive screen, due to the electric field of the human body, the user's finger and the working surface form a coupling capacitance. Because the working surface is connected with a high-frequency signal, the finger absorbs a very small current, and the current flows from the four corners of the screen respectively. The current flows out of the electrodes, and theoretically the current flowing through the four electrodes is proportional to the distance from the finger to the four corners. The controller obtains the position through precise calculation of the four current ratios. It can achieve 99% accuracy and has a response speed of less than 3ms.

The difference between resistive screen and capacitive screen

1. The resistive screen needs to be lightly pressed when touching the mold, while the capacitive screen can be activated even with a slight finger touch.

2. The resistive screen can be touched by any object, while the capacitive screen is based on the working principle of human body heat induction, which can only be touched by the heat sensitive area of ​​fingers, and nails and stylus are invalid. Since the area of ​​the finger is much larger than that of the stylus, when touching a small icon or menu on a mobile phone with a capacitive screen, the touch accuracy cannot be as high as that of a resistive screen.

3. Capacitive screens can easily perform multi-point touch, and resistive screens generally cannot achieve multi-point touch.

4. The inside of the resistive screen is soft. Generally, 4 to 5 layers of ultra-thin tempered glass are mixed with fine carbon particles (which can only be seen under a microscope), and the touch screen is connected by pressing the upper and lower layers of carbon particles into contact with each other. The circuit produces a touch response, which is prone to scratches, breakage, and touch screen failure. The capacitive screen is made of single-layer thickened tempered glass, which is hard, durable and has a long service life.

5. The visibility of the resistive screen is slightly poor in the sun, while the capacitive screen is very good, and the visibility of writing in the sun is still very strong.

Touch chip XPT2046

XPT2046 is an ADC conversion chip that samples the point when the screen is touched, then converts it into a digital quantity, and then communicates with the STM32 microcontroller through the SPI interface

insert image description here

The basic working principle of the chip

XPT2046 is a typical successive approximation analog-to-digital converter (SAR ADC), which includes functions such as sample/hold, analog-to-digital conversion, and serial port data output. At the same time, the chip integrates a 2.5V internal reference voltage source and temperature detection circuit, and uses an external clock when working. XPT2046 can be powered by a single power supply, and the power supply voltage range is 2.7V ~ 5.5V. The reference voltage value directly determines the input range of the ADC. The reference voltage can use an internal reference voltage, or directly input a reference voltage within the range of 1V to VCC from the outside (the output impedance of the external reference voltage source is required to be low). X, Y, Z, VBAT, Temp and AUX analog signals enter the ADC after being selected by the on-chip control register, and the ADC can be configured as single-ended or differential mode. When VBAT, Temp and AUX are selected, it can be configured as a single-ended mode; when used as a touch screen application, it can be configured as a differential mode, which can effectively eliminate the measurement error caused by the parasitic resistance of the drive switch and external interference, and improve the conversion accuracy

Timing diagram

insert image description here

CS is the chip select signal, if it is low, it means the chip is selected

DCLK is a clock signal, generated by STM32

DIN is the input pin, connected to the MOSI pin of STM32, STM32 is the host, the host outputs, and the slave inputs; when the communication starts, STM32 needs to send a byte (8 bits) to set the chip, and 8 clocks are required here

BUSY is a busy signal. When DIN inputs a byte to set the chip, the BUSY line will be pulled high, indicating that the chip is setting or converting data. After the data conversion is completed, the BUSY line will be pulled low. It takes 1 clock here.

DOUT is an output pin, because it is a 12-bit ADC chip, so the converted data has 12 bits. Compared with the frequency of the clock line, you can first give 7 clocks to read 7-bit data, and then give 5 clocks to read out For the remaining 5 bits, give another 3 clocks, and the Zero Filled behind the data will be automatically filled with zeros

Control byte for DIN input

insert image description here

Start bit - the first bit, namely the S bit. The first bit of the control word must be 1, that is, S=1 . All inputs will be ignored until a start bit is detected on the XPT2046's DIN pin.

Address - the next 3 bits (A2, A1 and A0) select the current channel of the multiplexer, the touch screen driver and the reference source input; because this chip can not only collect the screen, but also collect the temperature and battery, so it is necessary to set channel, set to capture screen

insert image description here

According to the table in the manual, if you want to set the Y axis, A2, A1, and A0 are 0, 0, and 1 respectively; if you want to set the X axis, A2, A1, and A0 are 1, 0, and 1 respectively.

MODE - mode selection bit, used to set the resolution of the ADC. MODE=0, the next conversion will be 12-bit mode; MODE=1, the next conversion will be 8-bit mode; choose 12-bit mode, so this bit is 0

The SER/DFR bit controls the reference source mode, select single-ended mode (SER/DFR=1), or differential mode (SER/DFR=0). In X-coordinate, Y-coordinate, and touch pressure measurements, differential mode of operation is preferred for best performance; so this bit is 0

Note: The differential mode is only used for the measurement of X coordinate, Y coordinate and touch pressure, other measurements require single-ended mode

PD0 and PD1 - set the relationship between power down and internal reference voltage configuration. According to the table below, generally set to 00

insert image description here

In summary, the control byte is

Read X-axis coordinates - 11010000 - 0xD0

Read Y-axis coordinates - 10010000 - 0x90

CubeMx configuration

It should be noted that the input pin DIN of the XPT2046 chip latches data on the rising edge of the clock signal DCLK, which is mentioned in the manual

insert image description here

Because SPI has 4 modes, and mode 0 and mode 3 are used to collect data on the rising edge, so when configuring the SPI communicating with XPT2046, it must be configured as mode 0 or mode 3

insert image description here

XPT2046 chip and STM32 pin connection

XPT2046 STM32
CS SPI2_NSS
DCLK SPI2_SCK
FROM SPI2_MOSI
DOUBT SPI2_MISO

Because SPI2 is used, CubeMX chooses to initialize SPI2

insert image description here

configuration interface

Select full-duplex host mode

Select 256 for the frequency division factor of the baud rate, then the baud rate is 70.312KBits/s, and the communication rate can be set lower, because it takes time for XPT2046 to convert data after receiving the setting command, if the frequency division factor is the smallest, The communication rate is the fastest, but the communication will be unstable occasionally. Set the rate to a low point under the premise of ensuring stability, and generate the keil project after other problems are fine.

insert image description here

If the Baud Rate (baud rate) is not 70.312KBits/s, it is because the clock configuration of APB1 is set to 36M, and the frequency division of APB1 in the clock configuration is set to 4, then APB1 becomes 18M, and you can get 70.312KBits/s

tips: SPI2 is mounted on the APB1 bus

insert image description here

Touch Calibration Principle

Reasons for touch calibration:

1. The resolution of the 2.8-inch TFT screen is 240x320, while the ADC value of the touch screen is 0-4096. Since it changes linearly, there is a proportional relationship between the two. It is necessary to calculate the proportional factors xFactor and yFactor

Explanation: The screen display is 2.8 inches, the resolution is 240x320, the coordinate value of the resolution is manipulated for display, but the screen also has a touch function, and the touch function is also 2.8 inches (the same screen), which is read when touch The value is not the coordinate value of the resolution, but the ADC value. This ADC value is 0 ~ 4096 in the screen width and 0 ~ 4096 in the height, so there is a linear relationship between the ADC value and the screen resolution

insert image description here

ADC value when touched * scale factor xFactor = screen X-axis coordinates

ADC value when touched * scaling factor yFactor = screen Y-axis coordinates

2. The coordinate origin (0,0) of the TFT screen does not exactly correspond to the origin of the touch screen, there is a certain offset, and the offset xOffset and yOffset need to be calculated .

insert image description here

After calibration, calculate the TFT screen coordinates LCD_X and LCD_Y according to the scale factor and offset

LCD_X = (ADC_X * xFactor ) - xOffset

LCD_Y = (ADC_Y * yFactor ) - yOffset

The general idea of ​​​​carrying out screen calibration and calculating the proportional factors xFactor, yFactor and offsets xOffset and yOffset

1. First, take 4 corners and 1 center point on the screen. Assuming that the distance between the 4 points and the screen is 20, the coordinates of the 4 surrounding points are

(x1,y1)= (20,20)

(x2,y2)=(20,LCD_Height-20)

(x3,y3)=(LCD_Width-20,LCD_Height-20)

(x4,y4)=(LCD_Width-20,20)

(x5,y5)=(LCD_Width/2,LCD_Height/2)

Among them, positions 1 - 4 are used to calculate the scale factor and offset, and position 5 is used to verify whether the touch calibration is correct

insert image description here

2. Display 5 cross cursors in turn on the screen. Their coordinates are the coordinates of the 4 corners and 1 center point above. Wait for the user to touch, and then obtain the ADC coordinate values ​​​​of the 5 positions in sequence, which are respectively recorded as ADC_x1 to ADC_x5 , ADC_y1 to ADC_y5

insert image description here

3. In the previous step, by touching 5 points, their ADC values ​​were obtained, but because the touch has errors, if these ADC values ​​are used directly, the final calculated proportional factor and offset are not very accurate , so it is necessary to integrate 4 touch points into two diagonal points to reduce touch errors

Description: The coordinates to be calculated are the upper left corner and the lower right corner, which are calculated by the coordinates of the surrounding 4 touch points

X coordinate of upper left corner = (X coordinate of touch point 0 + X coordinate of touch point 1) / 2

Y coordinate of upper left corner = (Y coordinate of touch point 0 + Y coordinate of touch point 3) / 2

X coordinate of lower right corner = (X coordinate of touch point 2 + X coordinate of touch point 3) / 2

Y coordinate of lower right corner = (Y coordinate of touch point 1 + Y coordinate of touch point 2) / 2

In this way, the (x, y) coordinates of the upper left corner and the (x, y) coordinates of the lower right corner are obtained.

4. Take the average value of the coordinates of the upper left corner and the lower right corner obtained in the previous step, and compare it with the coordinates of the middle touch point. If it exceeds a certain range, it is considered that the touch calibration has failed, and if it does not exceed, it is considered successful.

5. According to the diagonal coordinates and screen resolution, calculate the calculation scale factors xFactor, yFactor and offset xOffset and yOffset

6. Store the calibration parameters in the external Flash

part of the program

Use SPI2 to read the ADC value of the touch screen. When the parameter _ucXPT2046_CMD is 0xD0, it reads the X-axis coordinates, and when it is 0x90, it reads the Y-axis coordinates.

/**
* @name   Touch_Read_ADC
* @brief  读取触摸屏的ADC值
* @param  _ucXPT2046_CMD:触摸IC命令
* @retval None
*/
static uint16_t Touch_Read_ADC(uint8_t _ucXPT2046_CMD)
{
    
    
  uint8_t i,j;
  uint16_t usValue_Buf[TOUCH_READ_TIMERS],usTemp;
  uint32_t uiSumValue = 0;

  //通过SPI接口循环读取TOUCH_READ_TIMES次数的ADC值(5次)
  for(i=0;i<TOUCH_READ_TIMERS;i++)
  {
    
    
    //选择触摸芯片,CS输出低电平
    CLR_SPI_TOUCH_CS;

    /* 
    在差分模式下,XPT2046转换需要24个时钟,8个时钟输入命令,延时一会, 
    之后1个时钟去除忙信号,接着输出12位转换结果,剩下3个时钟是忽略位
    */

    //发送控制命令
    SPI_Touch_WriteByte(_ucXPT2046_CMD);
    //延时一会,等待ADC转换
    for(j=0;j<100;j++);
    //读取数据
    usValue_Buf[i] = SPI_Touch_ReadByte();    //获取前面7位(一个周期),其中最高位无效
    usValue_Buf[i] &= (~BIT7);                //最高位清零
    usValue_Buf[i] <<= 8;                     //左移至最高字节
    usValue_Buf[i] += SPI_Touch_ReadByte();   //获取后面5个字节,其中低3位无效
    usValue_Buf[i] >>= 3;                     //右移3位,得到12位有效数据
    //禁用触摸芯片,CS输出高电平
    SET_SPI_TOUCH_CS;
  }

  //采样值从大到小排序,冒泡排序
  for(i=0;i<(TOUCH_READ_TIMERS-1);i++)          //趟数:4次
  {
    
    
    for(j=i+1;j<TOUCH_READ_TIMERS;j++)        //比较次数
    {
    
    
      if(usValue_Buf[i]<usValue_Buf[j])
      {
    
    
        usTemp = usValue_Buf[i];
        usValue_Buf[i] = usValue_Buf[j];
        usValue_Buf[j] = usTemp;
      }
    }
  }

  //去掉最大最小值,求和
  for(i=1;i<(TOUCH_READ_TIMERS-1);i++)
  {
    
    
    uiSumValue += usValue_Buf[i];
  }

  //返回平均值
  return uiSumValue/(TOUCH_READ_TIMERS-2);
}

Guess you like

Origin blog.csdn.net/weixin_46251230/article/details/128429173