How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

As you know, touch screens today come in 2 types: resistive and capacitive. Their main difference for the user is that a resistive screen responds to pressure, while a capacitive one responds to touch. Let's look at how each of them is built.
A resistive screen is a glass liquid crystal display covered with a flexible membrane. A resistive layer is applied to the facing sides, and the space between the planes is separated by a dielectric. Electrodes are attached along the edges of the plates. When pressed, the screen and membrane come into contact at the point of touch, forming an ordinary voltage divider. The voltage from the lower arm of the divider goes to an ADC, which in turn converts it into a coordinate. Now it becomes clear why such a screen responds specifically to pressure and why you can press it with any hard object: a stylus or a pencil.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

Resistive touch screens have:
  • high durability;
  • low cost;

A capacitive touch screen is a glass panel coated with a layer of transparent resistive material. An electrode is installed in each corner of the panel, applying an alternating voltage to the layer. So when you touch it with a finger, since the human body has some capacitance, charge leaks from the panel. The amount of leakage depends on the distance from the touch point to the electrodes in the corners of the screen, which makes it possible to determine the touch point.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

Capacitive touch screens have:
  • low durability, since they use glass;
  • high cost;

The last factor is what explains why the module with a TFT display that I ordered on AliExpress has a resistive screen.
Also, multi-touch isn't possible on a resistive or a simple capacitive screen; it can be done on a projected capacitive display, which has a grid of electrodes on the back of the screen.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

Now let's move on to the XPT2046 controller. First, let's look at which controller pins the manufacturer brought out to the header.

Touch Screen controller pins:
T_CS (Chip Select) – activates/deactivates the touch controller.
T_CLK (Clock) – the clock line.
T_DIN (Data Input) – the command input line.
T_DO (Data Output) – the data output line.
T_IRQ (Interrupt Flag) – the interrupt request line.

The pin names make it clear that the controller is controlled via SPI, plus an interrupt request pin. When the display is touched, a logic zero appears on this pin. It's important to note that the maximum SPI clock frequency at which the XPT2046 can work must not exceed 2 MHz, and the ADC sample rate will then be 125 KHz.
Now let's figure out how to get the touch point coordinates. In fact, the XPT2046 can provide not only coordinates but also touch pressure, temperature and supply voltage, but we'll only deal with coordinates. I should also say that there is only one physical ADC in the XPT2046, and all the quantities listed above are measured using a multiplexer.
First we need to turn on the ADC.
The command format for the XPT2046 looks like this.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

PD1 - PD0 are responsible for the power modes and for connecting the reference voltage. We want to turn on the ADC and take measurements in differential input mode, so we choose the mode marked in the picture below.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

SER/DFR determines the ADC input type, single-ended or differential. We need differential, so we set it to zero.
MODE determines the resolution of the next conversion, 8 or 12 bits. Let's reason: since the TFT display has 320 points along the Y axis, 8-bit resolution, which gives coordinates from 0 to 255, won't suit us. We set MODE to zero.
A2-A0 configure the multiplexer, the touch driver connection and the reference voltage source, but since we only want to turn on the ADC, we send zeros.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

S – the start bit indicating the beginning of the control byte. The controller responds to a command only when the start bit is set, so we set it to one.
To sum up: we need to send the controller the number 0x80, and in response it will send us two bytes whose contents don't interest us. Having done this, we have every right to assume that the ADC has started and we can get data. Since SPI is a synchronous interface, for the controller to send us a response we must send 0x00 as well, preferably twice, since we don't know what resolution the conversion will be and how many bits it will send back.
Our SPI will be hardware; later we may redo it in software when we connect the SD card, since it also connects via SPI.
Below is the code for sending a byte over SPI

uint8_t Spi_Master_Transmit(uint8_t out_data)
{
      // Start transmission
     SPDR = out_data;
      // Wait for transmission to complete
      while(!(SPSR & (1<<SPIF))) ;
      return SPDR; //return the received byte
}

To initialize SPI we'll need 4 registers:
SPE – enables/disables the SPI module. If the bit is set to 1, the SPI module is enabled.
MSTR – determines the microcontroller's operating mode. If the bit is set to 1, the microcontroller works in Master mode. If the bit is cleared, in Slave mode.
SPR1 and SPR0 – determine the clock frequency of the SPI module.
How a Resistive Touch Screen Works, Using the XPT2046 Controller as an Example.

We set the maximum division factor to avoid errors from too high an exchange speed; it can be changed later. The XPT2046 initialization code for the Atmega16 in master mode looks like this.

        SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);
        Spi_Master_Transmit(0X80);
	Spi_Master_Transmit(0X00);
	Spi_Master_Transmit(0X00);

That's all; in the next article we'll look at how to get the touch point coordinates and calibrate the touch panel.