Connecting a Resistive Touch Panel with an XPT2046 Driver to STM32 and Calibrating It Using Four Points.
There are already several articles about resistive touch panels on this site: here you can read how they work, and here how to connect one to an AVR and calibrate it. So below I'll only talk about the subtleties of working with such a panel.
As you know, data exchange with the XPT2046 happens over SPI, so you just need to find a free one and connect to it. Usually, to see which pins are assigned to which SPI, I use the STM32CUBE program.
The display with the touch panel glued on is connected to my miniSTM32F103V board, and so far none of the SPIs are in use, but the connector intended for the display has the following unused pins: PA5, PA6, PA7, PB6, PB7.
And as it later became clear, some of them are intended precisely for connecting SPI devices; the remaining two pins can be used as shown in the picture below.

Now that the display is connected, we need to configure and enable the SPI module. Below is the initialization code of the SPI module for connecting to the XPT2046.
Let's also rewrite the remaining functions.
I'd like to draw attention to the function for getting the touch point coordinates: to improve accuracy it's advisable to take several measurements and then find the average, which is what was implemented. Also, if one of the coordinates is zero, the value of the other coordinate is reset to zero, and they are not used in the calculation.
Now let's move on to calibration. First I'd like to note that each coordinate is given by an equation of the form
And the point of calibration is to find the values of the coefficients K and B.
The proportionality coefficient K is found from the following reasoning: "If the distance between two points on the screen is 100 pixels and the same distance on the touch panel is 500 ADC counts, then K equals 5.
Knowing the coordinates of the points and the value of the coefficient K, you can find the value of the coefficient B from the formula above. Here is what the coefficient calculation actually looks like.
The code for the entire calibration procedure is shown below.
That's all; the archive contains the Keil uVision project. In the project settings, on the Target tab, set Xtal = 8MHz
ssd1963_xpt2046_example.rar
As you know, data exchange with the XPT2046 happens over SPI, so you just need to find a free one and connect to it. Usually, to see which pins are assigned to which SPI, I use the STM32CUBE program.
The display with the touch panel glued on is connected to my miniSTM32F103V board, and so far none of the SPIs are in use, but the connector intended for the display has the following unused pins: PA5, PA6, PA7, PB6, PB7.
And as it later became clear, some of them are intended precisely for connecting SPI devices; the remaining two pins can be used as shown in the picture below.

Now that the display is connected, we need to configure and enable the SPI module. Below is the initialization code of the SPI module for connecting to the XPT2046.
Show / Hide text
//function that initializes SPI
void SPI_Touch_Config (void)
{
//Enable clocking for ports A, B and alternate functions
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN;
//pinout
//PA5 - SCL - Alternative func. push-pull - OUT
//PB7 - CS - GPIO - soft - OUT
//PA7 - MOSI - Alternative func. push-pull - OUT
//PA6 - MISO - Input floating / Input pull-up - IN
//PB6 - Interrupt - GPIO - IN
//configure port A: pins 5 and 7 - alternate function, 6 - leave as is
GPIOA->CRL &= ~(GPIO_CRL_CNF5_0 | GPIO_CRL_CNF7_0);
GPIOA->CRL |= (GPIO_CRL_CNF5_1 | GPIO_CRL_CNF7_1);
GPIOA->CRL |= (GPIO_CRL_MODE5_1 | GPIO_CRL_MODE7_1);
//configure port B: pin 7 - push-pull output
GPIOB->CRL &= ~(GPIO_CRL_CNF7_0 | GPIO_CRL_CNF6_0);
GPIOB->CRL |= GPIO_CRL_MODE7_1;
//6 - input with pull-up
GPIOB->CRL |= GPIO_CRL_CNF6_1;
GPIOB->ODR |= GPIO_ODR_ODR6;
//configure SPI1
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //Enable oscil SPI1
SPI1->CR1 |= SPI_CR1_BR; //Baud rate = Fpclk/256
SPI1->CR1 &= ~SPI_CR1_CPOL; //Polarity cls signal CPOL = 0;
SPI1->CR1 &= ~SPI_CR1_CPHA; //Phase cls signal CPHA = 0;
SPI1->CR1 &= ~SPI_CR1_DFF; //8 bit data
SPI1->CR1 &= ~SPI_CR1_LSBFIRST; //MSB will be first
SPI1->CR1 |= SPI_CR1_SSM; //Program mode NSS
SPI1->CR1 |= SPI_CR1_SSI; //same as the state when NSS is 1
SPI1->CR1 |= SPI_CR1_MSTR; //Mode Master
SPI1->CR1 |= SPI_CR1_SPE; //Enable SPI1
//--------------------------
SPI1->CR2 = 0x00000000; //No interrupt enable
}
Let's also rewrite the remaining functions.
Show / Hide text
void Init_XPT2046()
{
Spi_Master_Transmit(0X80);
Spi_Master_Transmit(0X00);
Spi_Master_Transmit(0X00);
delay_us(1000);
}
///////////////////////////////////
uint8_t Spi_Master_Transmit(uint8_t out_data)
{
int d_in = 0;
SPI1->DR = out_data;
while(!(SPI1->SR & SPI_SR_RXNE));
d_in = SPI1->DR;
return d_in;
}
/////////////////////////////////
uint16_t Get_Touch(uint8_t adress)
{
uint16_t data = 0;
CS_TOUCH_LOW;//activate the XPT2046
//send a request to get the coordinate we are interested in
Spi_Master_Transmit(adress);
//read the high byte
Spi_Master_Transmit(0X00);
data = SPI1->DR;
data <<= 8;
//read the low byte
Spi_Master_Transmit(0X00);
data |= SPI1->DR;
data >>= 3;
CS_TOUCH_HIGH;//deactivate the XPT2046
return data;
}
I'd like to draw attention to the function for getting the touch point coordinates: to improve accuracy it's advisable to take several measurements and then find the average, which is what was implemented. Also, if one of the coordinates is zero, the value of the other coordinate is reset to zero, and they are not used in the calculation.
Show / Hide text
///////////////////////////////
void Get_Touch_XY( volatile uint16_t *x_kor,volatile uint16_t *y_kor, uint8_t count_read, uint8_t calibration_flag)
{
uint16_t tmpx, tmpy, touch_x, touch_y = 0;
touch_x = Get_Touch(X);//read the X coordinate
delay_us(100);
touch_y = Get_Touch(Y);//read the Y coordinate
//read the coordinates a certain number of times and find the average each time
//if one of the coordinates is zero, also zero the other value and ignore it
for(uint8_t i = 0; i < count_read; i++)
{
tmpx = Get_Touch(X);
delay_us(100);
tmpy = Get_Touch(Y);
if (tmpx == 0) tmpy = 0;
else if (tmpy == 0) tmpx = 0;
else
{
touch_x = (touch_x + tmpx) / 2;
touch_y = (touch_y + tmpy) / 2;
}
}
//during calibration, return the values calculated above,
//otherwise calculate the coordinates using the coefficients obtained during calibration
if (!calibration_flag)
{
*x_kor = touch_x/ax + bx;
*y_kor = touch_y/ay + by;
}
else
{
*x_kor = touch_x;
*y_kor = touch_y;
}
}
Now let's move on to calibration. First I'd like to note that each coordinate is given by an equation of the form
Xdisplay = Xtouch panel/K + B
And the point of calibration is to find the values of the coefficients K and B.
The proportionality coefficient K is found from the following reasoning: "If the distance between two points on the screen is 100 pixels and the same distance on the touch panel is 500 ADC counts, then K equals 5.
Knowing the coordinates of the points and the value of the coefficient K, you can find the value of the coefficient B from the formula above. Here is what the coefficient calculation actually looks like.
//Calculating the coefficients.
//xPos - values obtained from the touchscreen, xCenter - the real coordinates of the points on the display
axc[0] = (float)(xPos[3] - xPos[0])/(xCenter[3] - xCenter[0]);
bxc[0] = xCenter[0] - xPos[0]/axc[0];
ayc[0] = (float)(yPos[3] - yPos[0])/(yCenter[3] - yCenter[0]);
byc[0] = yCenter[0] - yPos[0]/ayc[0];
The code for the entire calibration procedure is shown below.
Show / Hide text
//calibration function
void Touch_Calibrate ( void )
{
uint16_t x, y;
//draw a crosshair in the upper left corner
TFT_Clear_Screen (WHITE);
TFT_Draw_String( 50, 100, 0xFFE0, 0x0000, *font8x8, "Callibration", 3 );
TFT_Draw_Line ( 10, 10+25, 10+50, 10+25, 0x0000,1 );
TFT_Draw_Line ( 10+25, 10, 10+25, 10+50, 0x0000,1 );
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Press", 3 );
while (1)
{
//wait for a press
while ( !isTouch ( ) );
Get_Touch_XY ( &x, &y, 100, 1);//take 100 measurements
if (x < 4090 && y < 4090)
{
xPos[0] = x;
yPos[0] = y;
break;
}
}
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Off ", 3 );
//wait for release
while ( isTouch ( ) );
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, " ", 3 );
//draw a crosshair in the upper right corner
TFT_Clear_Screen (WHITE);
TFT_Draw_String( 50, 100, 0xFFE0, 0x0000, *font8x8, "Callibration", 3 );
TFT_Draw_Line ( TFT_PIXEL_WIDTH -10-50, 10+25, TFT_PIXEL_WIDTH -10-50+50, 10+25, 0x0000,1 );
TFT_Draw_Line ( TFT_PIXEL_WIDTH -10-25, 10, TFT_PIXEL_WIDTH -10-25, 10+50, 0x0000,1 );
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Press", 3 );
while (1)
{
//wait for a press
while ( !isTouch ( ) );
Get_Touch_XY ( &x, &y, 100, 1);//take 100 measurements
if (x < 4090 && y < 4090)
{
xPos[1] = x;
yPos[1] = y;
break;
}
}
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Off ", 3 );
//wait for release
while ( isTouch ( ) );
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, " ", 3 );
//draw a crosshair in the lower left corner
TFT_Clear_Screen (WHITE);
TFT_Draw_String( 50, 100, 0xFFE0, 0x0000, *font8x8, "Callibration", 3 );
TFT_Draw_Line ( 10, TFT_PIXEL_HEIGHT-10-25, 10+50, TFT_PIXEL_HEIGHT-10-25, 0x0000,1 ); // hor
TFT_Draw_Line ( 10+25, TFT_PIXEL_HEIGHT-10-50, 10+25, TFT_PIXEL_HEIGHT-10-50+50, 0x0000,1 ); // vert
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Press", 3 );
while (1)
{
// wait for a press
while ( !isTouch ( ) );
Get_Touch_XY ( &x, &y, 100, 1);//take 100 measurements
if (x < 4090 && y < 4090)
{
xPos[2] = x;
yPos[2] = y;
break;
}
}
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Off ", 3 );
// wait for release
while ( isTouch ( ) );
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, " ", 3 );
//draw a crosshair in the lower right corner
TFT_Clear_Screen (WHITE);
TFT_Draw_String( 50, 100, 0xFFE0, 0x0000, *font8x8, "Callibration", 3 );
TFT_Draw_Line ( TFT_PIXEL_WIDTH -10-50, TFT_PIXEL_HEIGHT-10-25, TFT_PIXEL_WIDTH -10-50+50, TFT_PIXEL_HEIGHT-10-25, 0x0000,1 ); // hor
TFT_Draw_Line ( TFT_PIXEL_WIDTH -10-25, TFT_PIXEL_HEIGHT-10-50, TFT_PIXEL_WIDTH -10-25, TFT_PIXEL_HEIGHT-10-50+50, 0x0000,1 ); // vert
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Press", 3 );
while (1)
{
// wait for a press
while ( !isTouch ( ) );
Get_Touch_XY ( &x, &y, 100, 1);
if (x < 4090 && y < 4090)
{
xPos[3] = x;
yPos[3] = y;
break;
}
}
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, "Off ", 3 );
//wait for release
while ( isTouch ( ) );
TFT_Draw_String( 50, 120, 0xFFE0, 0x0000, *font8x8, " ", 3 );
//Calculating the coefficients.
//xPos - values obtained from the touchscreen, xCenter - the real coordinates of the points on the display
axc[0] = (float)(xPos[3] - xPos[0])/(xCenter[3] - xCenter[0]);
bxc[0] = xCenter[0] - xPos[0]/axc[0];
ayc[0] = (float)(yPos[3] - yPos[0])/(yCenter[3] - yCenter[0]);
byc[0] = yCenter[0] - yPos[0]/ayc[0];
/*axc[1] = (float)(xPos[2] - xPos[1])/(xCenter[2] - xCenter[1]);
bxc[1] = xCenter[1] - xPos[1]/axc[1];
ayc[1] = (float)(yPos[2] - yPos[1])/(yCenter[2] - yCenter[1]);
byc[1] = yCenter[1] - yPos[1]/ayc[1];*/
// Save the coefficients
Touch_Set_Coef ( axc[0], bxc[0], ayc[0], byc[0] );
TFT_Clear_Screen (WHITE);
TFT_Draw_String( 50, 100, 0xFFE0, 0x0000, *font8x8, "Callibration_End", 3 );
delay_ms (1000); // 1 sec
}
That's all; the archive contains the Keil uVision project. In the project settings, on the Target tab, set Xtal = 8MHz
ssd1963_xpt2046_example.rar