1. Environment introduction

The microcontroller adopts:STM32F103ZET6

Programming software:cone5

Programming language:C language

Programming style:Register development.

Target chip:XPT2046—Standard SPI interface timing

2. Introduction of XPT2046 chip

2.1 Function

XPT2046 is a 12-bit ADC chip, which can be used as an ordinary ADC chip, but is generally used on resistive touch screens to facilitate positioning of touch screen coordinates.

Figure 1: XPT2046 Internal Schematic

Figure 2: Resistive touch screen—The 4 lines drawn are connected to YN\XN\YP\XP of XPT2046

(XPT2046 supports pen interrupt output – low level is active, this pin can be configured to the interrupt pin of the microcontroller, or polling to determine the state of this pin to determine whether the touch screen has been pressed)

You can buy a touch screen + an XPT2046 alone to make a sketchpad, touch buttons (just use a piece of paper to draw a model below), and many other gadgets.

Figure 3: LCD screen with resistive touch screen (which layer of film is used for touch)

2.2 Features

1. The working voltage range is 2.2V~5.25V

3. Built-in 2.5V reference voltage source
4. Power supply voltage measurement (0V~6)
5. Built-in temperature measurement function
6. Touch pressure measurement
7. Adopt SPI 3-wire control communication interface
8. With automatic power-down function
9. Package: QFN-16, TSSOP-16 and VFBGA-48 are fully compatible with TSC2046, AK4182A
10. The XPT2046 consumes only 750 µW at 125KHz slew rate and 2.7V. XPT2046 11. With its low power consumption and high speed, it is widely used in small handheld devices powered by batteries, such as PDAs, mobile phones, etc.
12. XPT2046 is available in TSSOP-16, QFN-16 and VFBGA packages
formula, the temperature range is – 40 ~ + 85 ℃.

2.3 Working principle

XPT2046 is a typical successive approximation analog-to-digital converter (SAR ADC), which includes sample/hold, analog-to-digital conversion, serial port data output and other functions. At the same time, the chip integrates a 2.5V internal reference voltage source, 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 the internal reference voltage, or directly input the reference voltage in 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 go through on-chip
After the control register is selected and entered into the ADC, the ADC can be configured asSingle-ended or differential mode. When VBAT, Temp and AUX are selected, it can be configured as single-ended mode; when used as a touch screen application, it can be configured as 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.

Typical applications:

single-ended operation

When SER/DFR is set to high level, XPT2046 works in single-ended mode. The application principle of single-ended mode is shown in the figure below.
The single-ended mode is simple. After the sampling process is completed, the drive switch can be turned off during the conversion process to reduce power consumption. However, the disadvantage of this mode is that the accuracy is directly limited by the accuracy of the reference voltage source. At the same time, due to the on-resistance of the internal drive switch, the voltage divider between the on-resistance and the touch screen resistance will also bring measurement errors.

(A2 A1 A0 in the picture, and the SER/DFR mentioned above are the configuration commands of XPT2046, the specific usage will be mentioned later)

Differential working mode

When SER/DFR is set low, the XPT2046 operates in differential mode.
The advantage of the differential mode is that the inputs of +REF and -REF are directly connected to YP and YN respectively, which can eliminate the coordinate measurement error caused by the on-resistance of the drive switch.
The disadvantage is that the drive switch needs to be turned on during sampling or conversion, which increases power consumption compared to single-ended mode.
If the power consumption is not considered, the differential working mode is currently selected.

(A2 A1 A0 in the picture, and the SER/DFR mentioned above are the configuration commands of XPT2046, the specific usage will be mentioned later)

2.3 Introduction to the time sequence of XPT2046 collecting and converting data once

The XPT2046 data interface is a serial interface, the communication between the processor and the converter requires 8 clock cycles, and synchronous serial interfaces such as SPI, SSI and Microwire can be used. A complete conversion requires 24 serial synchronization clocks (DCLKs) to complete.

The first 8 clocks are used to input control bytes through the DIN pin. When the converter has obtained enough information about the next conversion, it then sets the input multiplexer and reference source input according to the obtained information, and enters sampling mode, which will start the touch panel driver if needed. After 3 multi-clock cycles, the control byte is set and the converter enters the conversion state. At this time, the input sample-and-hold device enters the hold state, and the touch panel driver stops working (single-ended operation mode).

The next 12 clock cycles will complete the actual analog-to-digital conversion. If it is a metric ratio conversion method (SER/DFR ——=0), the driver will always work during the conversion process, and the 13th clock will output the last bit of the conversion result. The remaining 3 multi-clock cycles are used to complete the last byte ignored by the converter (DOUT deasserted).

The timing diagram is as follows:

Control command bytes in the timing diagram:

Bit 7 (MSB) bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 Bit 0 (LSB)
S A2 A1 A0 MODE SER/DFR PD1 PD0

The meaning of each bit of the control byte is as follows:

bit name Function description
7 S start bit. If it is 1, it means that a new control byte is coming, if it is 0, it ignores the data on the PIN pin.
6-4 A2-A0 Channel select bits. This has been described above
3 MODE 12-bit/8-bit conversion resolution selection bits. For 1 select 8-bit for conversion resolution, for 0 for 12-bit resolution
2 SER/DFR Single-ended input mode/differential input mode selection bit. When it is 1, it is a single-ended input mode, and when it is 0, it is a differential input mode.
1-0 PD1-PD0 Low power mode selection bits. If 11, the device is always powered; if 00, the device is low between transitions
power mode

Note: Differential mode is only used for X-coordinate, Y-coordinate and touch pressure measurement, other measurements require single-ended mode.

According to the introduction of the above table, you can get two commands for selecting 12-bit resolution and measuring X and Y coordinates in differential mode: 0xD0 and 0x90

XPT2046 also has other modes, which can measure temperature, pen interrupt switch (on by default), 16 clock cycle conversion, 15 clock cycle conversion, these will not be introduced. According to the previous introduction, the function of measuring XY coordinates on the touch screen has been satisfied.

2.4 SPI timing introduction

The XPT2046 here supports the standard 3-wire SPI interface. The introduction of the SPI timing has been introduced in the previous article.

Refer here:STM32 entry development: introduction to SPI bus, reading and writing W25Q64 (FLASH) (hardware + analog timing) – DS Xiaolongge's column – CSDN blog – w25q64

2.5 Conversion of physical coordinates to screen coordinates

To use the touch screen on the LCD screen normally, it is definitely necessary to convert the original X and Y values ​​collected into the screen coordinates of the LCD screen before use.

There are many ways to convert, here is the simplestAngle factorCalculation method conversion.

For example, the LCD screen I use is 3.5 inches and the resolution is 320*480.

1. Get the coordinate XY limit values ​​of the upper left corner and lower right corner of the touch screen
x=3831,y=3934
x=155,y=168

2. Convert coordinate values
x coordinate: 3831~155 –> 3676~0
y coordinate: 3934~168 –> 3766~0

3. Calculate the slope
Slope of x coordinate: 3676/320=11.4875
Slope of y coordinate: 3766/480=7.84583

4. Get the actual pixel coordinates
x coordinate: 320-(the current X analog value collected in real time-155)/11.4875
Y coordinate: 480-(current Y analog value collected in real time-168)/7.84583

The reason for the subtraction here: Because the X and Y values ​​collected by the touch screen I used for testing and the screen coordinate values ​​of the LCD screen are reversed.

3. Sample code

Using SPI analog timing drive, other platforms can be transplanted.

3.1 xpt2046.c

#include "xpt2046_touch.h"struct XPT2046_TOUCH xpt2046_touch;/*function: Initialize hardware connection: T_MOSI--PF9T_MISO--PB2T_SCK---PB1T_PEN---PF10T_CS----PF11*/void XPT2046_TouchInit(void){ /* 1. Clock initialization*/ RCC->APB2ENR|=1<3; pb="" rcc-="">APB2ENR|=1<7; pf="" 2.="" Initialize gpio port*/="" gpiob-="">CRL&=0xFFFFF00F; GPIOB->CRL|=0x00000830; GPIOF->CRH&=0xFFFF000F; GPIOF->CRH|=0x00003830; /*3. Pull-up*/ GPIOB->ODR|=0x3<1 ; gpiof-="">ODR|=0x7<9; }="" Function:="" spi bottom layer writes a byte="" */="" void="" xpt2046_spi_writeonebyte(u8="" cmd) ="" {="" u8="" i;="" for(i="">9;><8;i++) {="" xpt2046_sck="0;" low level write="" if( cmd&0x80)xpt2046_mosi="1;" else="" xpt2046_mosi="0;">8;i++)><=1; xpt2046_sck="1;" High level read, ensure data line stability="" }="" }="" Function:="" Read 2 bytes="" Description:="" Read 16-bit data, the lowest 4-bit data is invalid, and the valid data is the upper 12-bit="" */="" u16 ="" xpt2046_readdata(u8="" cmd)="" {="" u16="" data;="" u8="" i;="" xpt2046_cs="0;" select xpt2046="" xpt2046_mosi=" 0;" xpt2046_sck="0;" xpt2046_spi_writeonebyte(cmd);="" delayus(8);="" 0.008ms="" , wait for xpt2046 conversion to complete. ="" Eliminate busy signal="" xpt2046_sck="0;" delayus(1);="" xpt2046_sck="1;" Continuously read 16-bit data="" for(i="">=1;> <16;i++) {="" xpt2046_sck="0;" inform xpt2046 that the host needs data="" xpt2046_sck="1;">16;i++)><=1; if(xpt2046_miso)data|="0x01; " }="" data="">>=4; //Discard the lowest 4 bits XPT2046_CS=1; //Uncheck return data;}/*XPT2046 command: 10010000 : Test Y coordinates 0x9011010000 : Test X coordinates 0xD0 return value: 0 means no coordinates are read, 1 means the current coordinates are read // 1. Get the XY limit values ​​of the upper left and lower right corners x=3831, y=3934 x=155, y=168// 2. Convert the coordinate value x coordinate: 3831~155 --> 3676~0 y coordinate: 3934~168 --> 3766~0//3. Calculate the slope of the slope of the x coordinate: 3676/320=11.4875 The slope of the y coordinate: 3766/480=7.84583//4. Get the actual pixel coordinate x coordinate: 320-(analog-155)/11.4875 y-coordinate: 480-(analog-168)/7.84583*/u8 XPT2046_ReadXY(void){ if( XPT2046_PEN==0) //Determine whether the touch screen is pressed { /*1. Get physical coordinates*/ xpt2046_touch.x0=XPT2046_ReadData(0xD0); xpt2046_touch.y0=XPT2046_ReadData(0x90); /*2. Get pixel coordinates*/ xpt2046_touch .x=320-(xpt2046_touch.x0-155)/11.4875; xpt2046_touch.y=480-(xpt2046_touch.y0-168)/7.84583; return 1; } return 0;}=1;>1;>7;>3 ;>

3.2 xpt2046.h

#ifndef XPT2046_TOUCH_H#define XPT2046_TOUCH_H#include "stm32f10x.h"#include "sys.h"#include "delay.h"//touch screen pin definition#define XPT2046_MOSI PFout(9)#define XPT2046_MISO PBin(2)#define XPT2046_SCK PBout(1)#define XPT2046_CS PFout(11)#define XPT2046_PEN PFin(10)//Function declaration void XPT2046_TouchInit(void);void XPT2046_SPI_WriteOneByte(u8 cmd);u8 XPT2046_ReadXY(void);//structure struct to store touch screen information XPT2046_TOUCH{ u16 x0; //physical coordinates x u16 y0; //physical coordinates y u16 x; //pixel coordinates x u16 y; //pixel coordinates y};extern struct XPT2046_TOUCH xpt2046_touch;#endif

Leave a Reply

Your email address will not be published.