Professional Documents
Culture Documents
The document following this cover page is marked as “Cypress” document as this is the
company that originally developed the product. Please note that Infineon will continue
to offer the product to new and existing customers as part of the Infineon product
portfolio.
www.infineon.com
AN2376
PSoC® 1 – Interface to Four-Wire Resistive Touchscreen
Author: Svyatoslav Paliy, Andrij Bilynskyy
Associated Project: Yes
Associated Part Family: CY8C24x94
Software Version: PSoC ® Designer™ 5.4 CP1
Related Application Notes: None
Abstract
AN2376 shows how PSoC 1 can be used to control a resistive touchscreen, and read (x,y) positions of single
touches as well as touch pressure. It describes the essential mathematics in detail, and includes a method for
calibrating a touchscreen to a display. A project is included that passes touchscreen readings to a PC through a USB
HID interface.
Contents Introduction
Introduction .......................................................................1
Touchscreen interfaces are effective in many information
Construction of Resistive Touchscreens ...........................2 appliances, in personal digital assistants (PDAs), and as
Resistive Touchscreen Measurement Method ..................2 generic pointing devices for instrumentation and control
PSoC Implementation .......................................................5 applications. This application note describes resistive types
Power Consumption (Pen Interrupt) ..................................7 of touchscreens. Their construction is simple, their cost is
Touchscreen Calibration ...................................................8 low, and their operation is well understood by users. The
only concern is that the resistive layers can be damaged by
Appendix A: Hardware Setup (Not Actual Size) ..............11 very sharp objects. This document considers the basic
Appendix B: Board Bill of Materials .................................12 principles of how resistive touchscreens work and how to
Appendix C: Touchscreen Controller PC Test Application13 best convert these analog inputs into usable digital data
Appendix D: Three-Points Calibration Procedure ............14 using a PSoC.
Appendix E: Capacitive Touchscreens as Alternative to When developing more complex touch-assisted projects;
Resistive Touchscreens ..................................................16 there is often a need for additional peripheral units, such as
About the Author .............................................................17 operational and instrument amplifiers, filters, timers, digital
About the Author .............................................................17 logic circuits, AD and DA convertors. As a general rule,
Document History ............................................................18 implementation of these extra peripherals brings in
additional difficulties: space for new components, additional
attention during production of a printed circuit board, power
consumption increase. All of these factors can significantly
affect the project price and development cycle.
PSoC has made many engineers’ dreams come true; that is,
to have all their project needs covered in one chip.
RY+ RY+
YM YN YN
RY- RY-
Conductive Bar XN XN
(Silver Ink) Rx- Rtouch Rx-
XM
XP Rx+ Rx+
XP
Untouched Touched
XP
Analog hi-Z
Vcc
YP Touch YP Touch
Screen Screen
Ry+ ADC Ry+ ADC
Rx+ Vref=VCC Vref=VCC
XN Rx+
XN
GND
XP Rx- XP Rx- Analog hi-Z
Rtouch Rtouch
YN YN
Ry- Ry-
Analog hi-Z
Analog hi-Z
Current (when touched) Vcc Current (when touched) Vcc
YP Touch YP Touch
Screen Screen
Ry+ ADC Ry+ ADC
Rx+ Vref=VCC Rx+ GND Vref=VCC
XN XN
GND
XP Rx- XP Rx-
Rtouch Rtouch
YN YN
Ry- Ry-
Analog
Analog hi-Z hi-Z
c) Touch pressure Z1 d) Touch pressure Z2
PSoC PSoC
Measurement Measurement
Now it’s possible to develop a mathematical equation to By analogy from Figure 3b:
calculate the cross-layer resistance, from our ADC results X,
Y, Z1, Z2 (see Figure 3). Y Ry − Ry −
= =
From Figure 3a, you can write: ADmax R y − + R y + R y _ plate
X V V Equation 2
= in = in = Where,
ADmax V ref Vcc
iR x − Rx− Ry_plate = Ry-+Ry+ – Y-plate resistant
= =
i (R x − + R x + ) R x − + R x + From Figure 3c:
Rx− Z1 Rx −
=
R x _ plate ADmax Rx − + Rtouch + R y +
Equation 1
Equation 3
Where,
And from Figure 3d:
You can then obtain the cross-layer resistance that represents touch pressure, from Equations 1, 3, and 4:
X Z2
Rtouch = Rx _ plate ⋅ ADC _ resolution
− 1
2 Z1
Equation 5
And the other cross-layer resistance, which also represents touch pressure, from Equations 1, 2, and 3:
X 2 ADC _ resolution Y
Rtouch = Rx _ plate ⋅ ADC _ resolution − 1 − Ry _ plate 1 − ADC _ resolution
2 Z1 2
Equation 6
Equation 5 requires a known X-plate resistance, Figure 4. General Touchscreen Parameter Measurement
measurements of X-position (X), and two additional Routine Flowchart
cross-panel measurements (Z1 and Z2) of the touchscreen
Equation 6 requires known X- and Y-plate resistance values
but only allows measurement of Z1. For calculation touch Start
pressure, you can use one of those equations. In the real
projects, you must have three touchscreen parameters such
as X-position, Y-position, and pressure. To calculate touch Configure port to
pressure, you can use either of these equations. Equation 6 measure touch Configure port for
requires three ADC measurements while Equation 5 pressure Y - position
requires four. Using Equation 6 is faster, but its arithmetic is Measurement
more complicated.
Measure Z1 and Z 2
A flowchart for the touchscreen parameter measurement Measure Y
routine is shown in Figure 4.
Configure port for
X - position Configure port for
measurement touch detection
and ensure that
touch screen is still
Measure X and touched
calculate touch
pressure, P
Return Parameters
PSoC Implementation
The PSoC internal structure is very simple (see Figure 5). Pins P0[1], P0[3], P0[5], and P0[7] are used for the touch panel
interface. Signals from the input pins are switched through the analog column input multiplexer to the PGA, and then to the
delta-sigma ADC.
The PSoC-based schematic is also very simple (see Figure 6). The touchscreen is directly connected to the PSoC device
ports. Resistors R1 and R2 are used as pull downs to terminate the PSoC inputs when the plates are not driven.
YP
XM
+5V
XP
R1
1M
56
55
54
53
52
51
50
49
48
47
46
45
44
43
U1
Touch Scren YM
Vdd
P2[5]
P2[7]
P0[1]
P0[3]
P0[5]
P0[7]
Vss
P0[6]
P0[4]
P0[2]
P0[0]
P2[6]
P2[4]
1 42
2 P2[3] P2[2] 41
R2 3 P2[1] P2[0] 40
1M 4 P4[7] P4[6] 39
5 P4[5] P4[4] 38
6 P4[3] P4[2] 37
7 P4[1] P4[0] 36
8 P3[7] CY 8C24794 P3[6] 35
9 P3[5] P3[4] 34
10 P3[3] P3[2] 33
11 P3[1] P3[0] 32
12 P5[7] P5[6] 31
13 P5[5] P5[4] 30
14 P5[3] P5[2] 29
P5[1] P5[0]
P1[7]
P1[5]
P1[3]
P1[1]
P7[7]
P7[0]
P1[0]
P1[2]
P1[4]
P1[6]
Vdd
Vss
D+
D-
15
16
17
18
19
20
21
22
23
24
25
26
27
28
R3 R4
24R 24R
+5V
J1
1
Vdd 2
-D 3 C1
+D 4 0.1u
Gnd
USB
To debug this project, measured data is transferred to the PC through the USB interface. The USB HID device class is used to
simplify data transfer to the PC and to avoid writing a separate USB driver for the PC. The PSoC device sends a data packet
to the PC that contains four 16-bit parameters. Table 1 shows the data packet structure. A simple PC application has been
developed for evaluation (Appendix C).
Table 1. Data Packet Structure
1 0 2 X touch coordinate
2 2 2 Y touch coordinate
3 4 2 Z1
4 6 2 Z2
Analog hi-Z
YP
Touch
Screen
XP
XN
Digital In
Interrupt on
Rtouch falling edge
YN
Strong “0”
PSoC
Touchscreen Calibration
In most cases, the touchscreen is mounted over an LCD or Figure 9 shows some misaligned touchscreen and display
another display. If this is the case, the data measured by the coordinates. This figure also shows that it is possible to
touchscreen must be translated into true screen describe each point on the display as PD = [XD,YD] and
coordinates. each point on the touchscreen as PT = [XT,YT].
The calibration algorithm proposed in this application note Three error factors are presented:
allows eliminating scaling factors and mechanical
misalignment of the touchscreen. The challenge for the Rotation of the touchscreen coordinates relative to the
calibration algorithm is to translate the set of coordinates display coordinates.
reported by the touchscreen into a set of coordinates that
accurately represents a point on the display. Linear shift of the coordinates.
[X D , YD ] = f ([X T , YT ]) A scaling factor.
Equation 7
dX
LCD LCD LCD
Scale KY
Touch Touch Touch
(XD,YD)=(Rcos(α-dα),Rsin(α-dα))
(XD,YD)=(XT+dX,YT+dY) (XT,YT)=(Rcos(α),Rsin(α)) (XD,YD)=(XT*KX,YT*KY)
α-dα 1
1
C
α dα
dY 11
Scale KX
C
Linear shift error Rotation of the touch Scaling error
screen error
The LCD coordinate XD can be expressed as:
X D = K x R cos(α − dα ) + dX
= K x R cos α cos dα + K x sin α sin dα + dX
= K x X T cos dα + K xYT sin dα + dX
= α x X T + β xYT + dX
Equation 8
YD = K y R sin (α − dα ) + dY
= K y R sin α cos dα − K y cos α sin dα + dY
= K yYT cos dα − K y X T sin dα + dY
= α y X T + β yYT + dY
Equation 9
Where,
αx = Kxcos(dα), βx = Kxsin(dα), αy = –Kysin(dα), and βy = Kycos(dα).
From Equations 8 and 9, when there are three independent points (not on one line), you can get the coefficients αx, αy, βx, βy,
dX, and dY. Assuming that (XD1, YD1), (XD2, YD2), and (XD3, YD3) are three independent points selected on the LCD, and
(XT1, YT1), (XT2, YT2), and (XT3, YT3) are the corresponding points on the touchscreen, Equations 8 and 9 can be used to write
Equation 10:
X D1 = α x X T 1 + β xYT 1 + dX
X D 2 = α x X T 2 + β xYT 2 + dX
X D 3 = α x X T 3 + β xYT 3 + dX
YD1 = α y X T 1 + β yYT 1 + dY
YD 2 = α y X T 2 + β yYT 2 + dY
YD 3 = α y X T 3 + β yYT 3 + dY
Equation 10
Or in matrix form:
X D1 αx YD1 α y X T 1 YT 1 1
X D 2 = A × β x and YD 2 = A × β y Where, A = X T 2 YT 2 1
X dX Y dY
D3 D3 X T 3 YT 3 1
αx X D1 α y YD1
β x = A × X D 2 and β y = A × YD 2
−1 −1
dX X dY Y
D3 D3
Equation 11
Where,
A-1 is the inverse of matrix A. The three points—(XD1, YD1), (XD2, YD2), and (XD3, YD3)—are selected on the display surface, and
the elements in matrix A are measured from the touchscreen during calibration.
The 3-points calibration’s equations, in a non-matrix form, and source code for it, optimized for 8-bit cores, are described in
Appendix D.
For best results, the first sample point must be located at the distance of approximately 10% of the screen size in the upper-
left corner. The second and third points must be at a distance of approximately 10% of the screen size from the screen edges
in the center of each side (see Figure 10). For the calibration process, points 1, 2, and 3 must be touched in an ascending
order.
Calibration must be performed before use for each device that contains a touchscreen. There is no need to perform calibration
every time the device is powered on. However, it is prudent to save the calibration parameters in the energy-independent
memory. Every time the device is used after calibration, it reads these coefficients and uses them to calculate the true screen
coordinates.
In many applications, users may use more than three points in their calibration routines to average or filter the noisy readings
from the touchscreen controller. For calibration with more than three points:
X D1 YD1 X T 1 YT 1 1
X D2 YD 2 X T 2 YT 2 1
. αx . α y . . .
= A × β x and = A × β y where A= Equation 12
. dX . dY . . .
. . . .
.
X Y X Y 1
DN DN TN TN
To resolve Equation 12, both sides can be multiplied by A’s pseudo-inverse matrix, (AT×A)–1×AT, where AT is A’s transpose
matrix.
X D1 YD1
X D1 YD1
αx . α y .
β
x = A(
T
× A
−1
)
× AT
× and β
y = A(
T
× A
−1
)
× AT
× Equation 13
dX . dY .
. .
X Y
DN DN
( X D1 − X D 3 )(YT 2 − YT 3 ) − ( X D 2 − X D 3 )(YT 1 − YT 3 )
αx =
( X T 1 − X T 3 )(YT 2 − YT 3 ) − ( X T 2 − X T 3)(YT 1 − YT 3 )
( X − X T 3 )(YD 2 − YD 3 ) − ( X D 2 − X D 3 )(YT 1 − YT 3 )
β x = T1
( X T 1 − X T 3 )(YT 2 − YT 3 ) − ( X T 2 − X T 3)(YT 1 − YT 3 )
YT 1 ( X T 3 X D 2 − X T 2 X D 3 ) + YT 2 ( X T 1 X D 3 − X T 3 X D1 ) + YT 3 ( X T 2 X D1 − X T 1 X D 2 )
dX =
( X T 1 − X T 3 )(YT 2 − YT 3 ) − ( X T 2 − X T 3 )(YT 1 − YT 3 )
(YD1 − YD 3 )(YT 2 − YT 3 ) − (YD 2 − YD 3 )(YT 1 − YT 3 )
αy =
( X T 1 − X T 3 )(YT 2 − YT 3 ) − ( X T 2 − X T 3)(YT 1 − YT 3 )
( X − X T 3 )(YD 2 − YD 3 ) − (YD1 − YD 3 )( X T 2 − X T 3 )
β y = T1
( X T 1 − X T 3 )(YT 2 − YT 3 ) − ( X T 2 − X T 3)(YT 1 − YT 3 )
Y ( X Y − X T 2YD 3 ) + YT 2 ( X T 1YD 3 − X T 3YD1 ) + YT 3 ( X T 2YD1 − X T 1YD 2 )
dY = T 1 T 3 D 2
( X T 1 − X T 3 )(YT 2 − YT 3 ) − ( X T 2 − X T 3 )(YT 1 − YT 3 )
The right sides of the all expressions contain the same denominator. This can be very useful for integer arithmetic, isolating
the denominator and marking it as a C vector:
This method is optimal for 8-bit cores. The following code demonstrates it.
Document History
Document Title: PSoC® 1 – Interface to Four-wire Resistive Touchscreen - AN2376
Document Number: 001-15228
In March of 2007, Cypress recataloged all of its Application Notes using a new documentation number and revision code. This new documentation
number and revision code (001-xxxxx, beginning with rev. **), located in the footer of the document, will be used in all subsequent revisions.
PSoC is a registered trademark of Cypress Semiconductor Corp. All other trademarks or registered trademarks referenced herein are the property of
their respective owners.
© Cypress Semiconductor Corporation, 2007-2014. The information contained herein is subject to change without notice. Cypress Semiconductor
Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any
license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or
safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as
critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The
inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies
Cypress against all charges.
This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide
patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a
personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative
works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress
integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source
Code except as specified above is prohibited without the express written permission of Cypress.
Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the
right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or
use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a
malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems
application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges.
Use may be limited by and subject to the applicable Cypress software license agreement.