How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

In the previous article we looked at how to initialize the controller; in this one we'll figure out how to get the touch coordinates and calibrate the touch panel.
Before determining the touch coordinates, the touch itself has to be detected; for this we'll use the dedicated T_IRQ pin, which goes to logic zero when the panel is touched. It would be good to connect it to a pin responsible for external interrupts; by the way, the ATmega16 has 3 such pins, but mine are all already in use, so we'll connect it to any other pin configured as an input.
Now let's try to get the value of the X coordinate; the value of the Y coordinate is obtained by similar reasoning.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

The picture above shows that the X coordinate value can be obtained in two cases: in the first (A0=1, A1=0, A2=0), YP and YN serve as the reference voltages; in the second (A0=1, A1=1, A2=0), YP and XN; we'll implement the first. With this connection, the ADC works in ratiometric mode, which reduces measurement error. Also, to start the conversion, the start bit S must be set to one. Let's refresh our memory of the command format.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

So to get the X coordinate, we need to send 0x90 to the controller, and in response it will send us 12 bits of data. Since we can only receive 8 bits at a time, we'll receive twice and get the result using shift operations. Let's put all of the above into a function to which we'll pass pointers to the coordinate variables.

#define	chy 	0x90         //the coordinate axes of the display and the touchscreen are swapped (where the display has X, the touchscreen has Y)
#define	chx 	0xD0        //we'll take the display axes as the basis

void Get_Touch_xy( volatile uint16_t *x_kor,volatile uint16_t *y_kor)
{
		volatile uint16_t touch_x = 0;
		volatile uint16_t touch_y = 0;

		Spi_Master_Transmit(chx);  //send the X coordinate request

		Spi_Master_Transmit(0X00); //receive the upper 8 bits
		touch_x = SPDR;
		touch_x <<= 8;
		
		Spi_Master_Transmit(0X00); //receive the lower 4 bits
		touch_x |= SPDR;
		touch_x >>= 4;

		_delay_us(100);
		Spi_Master_Transmit(chy);  //send the Y coordinate request
		
		Spi_Master_Transmit(0X00);  //receive the upper 8 bits
		touch_y = SPDR;
		touch_y <<= 8;
		
		Spi_Master_Transmit(0X00);  ////receive the lower 4 bits
		touch_y |= SPDR;
		touch_y >>= 4;
		
		*x_kor = touch_x;
		*y_kor = touch_y; 
}

Now let's move on to calibration. The idea of calibration is that when you press the screen, a point is drawn where the press occurred. To do this we need to solve 3 problems. The first is to account for insensitive areas: ideally, if you draw the stylus from one side to the opposite one, the touch coordinates (data from the 12-bit ADC) should change from 0 to 4095; in reality they will change from roughly 300 to 3800. These are approximate figures, but the point is the same: there's an insensitive area near each side. Second, to draw a point on the display, its coordinates must be in the range 0 to 239 for X and 0 to 319 for Y, while since the ADC is 12-bit, the touch point coordinates will range from 0 to 4095. And third, as it turned out later, the coordinate origins of the display and the touch panel are in opposite corners, so if we don't want to press in one corner and have the point drawn in another, we need to take this into account.
For this task I wrote a simple function that lets us watch the touch point coordinates on the screen.

void Callibrate()
{
	volatile uint16_t touch_kor_x1= 0;
	volatile uint16_t touch_kor_y1= 0;
	uint8_t data_x [4] = {0} ;
	uint8_t data_y [4] = {0} ;
	
	if(!(PINC & (1<<PINIRQ)))
	{
		
		Draw_Simbol(10,50,green,0XFFFF, &simbols[0x38*8],3);//draw X
		Draw_Simbol(10,90,green,0XFFFF, &simbols[0x1D*8],3);//draw =
		Draw_Simbol(40,50,green,0XFFFF, &simbols[0x39*8],3);//draw Y
		Draw_Simbol(40,90,green,0XFFFF, &simbols[0x1D*8],3);//draw =

		Get_Touch_xy(&touch_kor_x1, &touch_kor_y1);//get the touch point coordinates

		itoa( touch_kor_x1,(char*) data_x, 10);//convert the X coordinate for display
		itoa( touch_kor_y1,(char*) data_y, 10);//convert the Y coordinate for display
		
		for (uint8_t k = 0; k < 4; k++)
		{       //output the X and Y values
			Draw_Simbol(40,130 + 8*k*5,green,0XFFFF, &simbols[(data_y[k]-0x20)*8],3);
			Draw_Simbol(10,130 + 8*k*5,green,0XFFFF, &simbols[(data_x[k]-0x20)*8],3);
			
		}
	}
}

The itoa function converts a numeric value into its string equivalent with the specified base.
char *itoa(int num, char *str, int radix)
int num - the numeric value we want to convert
char *str - a pointer to the result, of type char
int radix - the base of the number system for the output string
If we pass the value 1711 and choose the decimal system, the digit 1 is converted into the corresponding ASCII character, whose code is 49, the digit 7 into the character with code 55, and so on.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

Now let's flash the controller and see what we get.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

The reader can't see it, so you'll just have to trust me: first, the coordinate origins of the display and the touch panel are in opposite corners; second, for some reason the result came out 11-bit instead of 12-bit. Let's connect a logic analyzer and see what's going on.
Channel D0 is T_CLK, channel D1 is T_DO; I disabled the rest so they wouldn't get in the way.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

Now let's look in the debugger at what the controller sends.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

The data the controller sends fully matches what we see on the oscillogram. The whole oscillogram didn't fit, but it shows that the data should be read on the rising edge starting from the second clock, which is exactly what the datasheet says; I had missed that.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

So in the code above we need to change the line touch_y >>= 4; to touch_y >>= 3;
Let's load the program into the controller and see whether our reasoning was correct.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

Great, the readings became twelve-bit. Now we need to run the stylus along each side, and the value shouldn't change. That is, if we run along the X side, Y should change but X shouldn't. If the value changes unexpectedly, increase the distance from the side you're running along. We need to find such a value for each side and write them down on a sheet of paper. In the end we'll have 4 values: Xmax, Xmin, Ymax, Ymin. Next, we take these values and calculate the dimensions of the working area.
Ywork = Ymax - Ymin
Xwork = Xmax - Xmin
I got Ymax = 3900, Ymin = 400, Ywork = 3500, Xmax = 3900, Xmin = 300, Xwork = 3600. Let's reason: 240 points on the display along X correspond to 3600 points on the touch panel, i.e. 1 to 15, and 320 points along Y correspond to 3550 points on the touch panel, i.e. 1 to 11.09. It's desirable for the ratios to be whole numbers; otherwise, since we're working with integers, rounding will occur and the fractional part will cause an error. So pressing anywhere within the resulting 15 by 11 square on the touch panel should draw the same point on the display.
To display the press point along X, subtract Xmin from the obtained coordinate and divide by 15; the same for Y.

                touch_x -= 300;
		touch_x =  touch_x/15;
		touch_y -= 350;
		touch_y =  touch_y/11;

If you now try to draw points on touch, they'll appear mirrored about the center of the screen, since the coordinate origins are in opposite corners. So for X you need to subtract the result from 240, and for Y subtract the result from 320. In the end, the function we use to get the coordinates will look like this.

#define	chy 	   0x90
#define	chx 	   0xD0

#define    Xmax	3900
#define    Xmin	400

#define    Ymax	3900
#define    Ymin	300

//#define calibr
void Get_Touch_xy( volatile uint16_t *x_kor,volatile uint16_t *y_kor)
{
		
		volatile uint16_t touch_x = 0;
		volatile uint16_t touch_y = 0;
		Spi_Master_Transmit(chx);
		
		Spi_Master_Transmit(0X00);
		touch_x = SPDR;
		touch_x <<= 8;
		
		Spi_Master_Transmit(0X00);
		touch_x |= SPDR;
		touch_x >>= 3;
		
		_delay_us(100);
		Spi_Master_Transmit(chy);
		
		Spi_Master_Transmit(0X00);
		touch_y = SPDR;
		touch_y <<= 8;
		
		Spi_Master_Transmit(0X00);
		touch_y |= SPDR;
		touch_y >>= 3;
	
		#ifndef calibr
		touch_x -= 300;
		touch_x = 240 - touch_x/((Xmax-Xmin)/240);
		touch_y -= 350;
		touch_y = 320 - touch_y/((Ymax-Ymin)/320);  
		#endif
		
		*x_kor = touch_x;
		*y_kor = touch_y; 
}

For calibration, call the calibration function in an infinite loop and uncomment the line #define calibr.
Sometimes, at the moment the stylus is lifted from the display, the touch coordinates change to values that don't correspond to the press point, but in that case the result is always the same: X = -410 and Y = -20. So it's easy to take this into account when processing the press point.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

The overall wiring diagram is shown below.
How to Get Touch Point Coordinates and Calibrate a Resistive Touch Panel, Using the XPT2046 as an Example.

That's all; in the next article we'll convert an image into an array in order to display it.
The project for Atmel Studio 6.2 is in the archive xpt2046_spi__ssd1289_i8080_example.rar