Skip to content

Commit 114861b

Browse files
committed
Touch screen library for STM32F1, based on XPT2046 touch controller.
The XPT2046 touch controller can be found on the new 2.4" ILI9341 SPI LCDs.
1 parent 3452a04 commit 114861b

7 files changed

Lines changed: 354 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Description
2+
---
3+
4+
A simple **XPT2046** Touch Screen library for STM32 micro-controllers.
5+
6+
* The first example sketch (**TouchTest.ino**) checks if the touch screen has been pressed and prints the X,Y coordinates on the Serial port 1.
7+
* The second example sketch (**TouchButtons.ino**) creates some virtual buttons defined by the user.
8+
9+
Copyright (c) 03 December 2015 by **Vassilis Serasidis**
10+
11+
Home: http://www.serasidis.gr , https://github.com/Serasidis
12+
13+
The library is written for STM32duino (http://stm32duino.com)
14+
15+
```
16+
//Create 4*2=8 virtual buttons
17+
#define LINES 2 //Divide the touch screen into 2 lines
18+
#define COLUMNS 4 //Divide the touch screen into 4 columns
19+
20+
```
21+
22+
23+
Screenshot of the Serial port 1 (TouchButtons.ino)
24+
```
25+
-------------------------------------------------
26+
XPT2046 touch screen buttons
27+
Copyright (c) 02 Dec 2015 by Vassilis Serasidis
28+
Home: http://www.serasidis.gr
29+
-------------------------------------------------
30+
Button: 1
31+
X: 1097
32+
Y: 800
33+
34+
Button: 2
35+
X: 3455
36+
Y: 617
37+
38+
Button: 3
39+
X: 684
40+
Y: 1483
41+
42+
Button: 4
43+
X: 3412
44+
Y: 1198
45+
46+
Button: 5
47+
X: 860
48+
Y: 2395
49+
50+
Button: 6
51+
X: 3353
52+
Y: 2355
53+
54+
```
55+
56+
PIN Connections between XPT2046 and STM32F103
57+
----
58+
| XPT2046 | STM32F103 |
59+
|:------:|:-----:|
60+
|T_DO|PA6|
61+
|T_DIN|PA7|
62+
|T_CS|PA3|
63+
|T_CLK|PA5|
64+
65+
Selecting the SPI port number and Chip Select pin
66+
----
67+
Can be used other SPI port than the default SPI1 port. Just select the SPI port you want by replacing the following line into the sketch:
68+
69+
```
70+
SPIClass mySPI(1); //Create an SPI instance on SPI1 port.
71+
//SPIClass mySPI(2); //Create an SPI instance on SPI2 port.
72+
```
73+
74+
The Chip select pin can be defined by the user also.
75+
76+
```
77+
#define CS_PIN PA3 // The pin PA3 has been chosen as Chip Select pin.
78+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* XPT2046 touch screen buttons.
3+
*
4+
* It divides the touch screen into COLUMNS * LINES areas (4*2=8 buttons) and creates virtual buttons.
5+
* if the touch screen area is pressed and prints on Serial Port 1 the X,Y coordinates.
6+
*
7+
* Copyright (c) 02 Dec 2015 by Vassilis Serasidis
8+
* Home: http://www.serasidis.gr
9+
* email: avrsite@yahoo.gr
10+
*
11+
* The sketch example has been created for using it with STM32Duino (http://www.stm32duino.com)
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*/
21+
22+
#include "XPT2046_touch.h"
23+
#include <SPI.h>;
24+
25+
#define CS_PIN PA3 // Chip Select pin
26+
#define LINES 2
27+
#define COLUMNS 4
28+
29+
SPIClass mySPI(1); //Create an SPI instance on SPI1 port.
30+
XPT2046_touch ts(CS_PIN, mySPI); // Chip Select pin, SPI port
31+
32+
uint16_t xy[2];
33+
34+
void setup() {
35+
Serial1.begin(9600);
36+
Serial1.println("-------------------------------------------------");
37+
Serial1.println("XPT2046 touch screen buttons");
38+
Serial1.println("Copyright (c) 02 Dec 2015 by Vassilis Serasidis");
39+
Serial1.println("Home: http://www.serasidis.gr");
40+
Serial1.println("-------------------------------------------------");
41+
ts.begin(); //Begin TouchScreen.
42+
ts.setButtonsNumber(COLUMNS, LINES); //Divide the Touch screen area into 4 columns and 2 lines and make them act as buttons.
43+
}
44+
45+
void loop() {
46+
if(ts.read_XY(xy)){ //If the touch screen is preesed, read the X,Y coordinates and print them on Serial port.
47+
uint8_t buttonNumber = ts.getButtonNumber();
48+
if(buttonNumber > 0){
49+
Serial1.print("Button: ");
50+
Serial1.println(buttonNumber);
51+
52+
Serial1.print("X: ");
53+
Serial1.println(xy[0]); //Print X value
54+
Serial1.print("Y: ");
55+
Serial1.println(xy[1]); //Print Y value
56+
Serial1.println();
57+
}
58+
delay(500);
59+
}
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* XPT2046 Touch Screen Controller example.
3+
*
4+
* It checks if the touch screen area is pressed and prints on Serial Port 1 the X,Y coordinates.
5+
*
6+
* Copyright (c) 02 Dec 2015 by Vassilis Serasidis
7+
* Home: http://www.serasidis.gr
8+
* email: avrsite@yahoo.gr
9+
*
10+
* The sketch example has been created for using it with STM32Duino (http://www.stm32duino.com)
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
* THE SOFTWARE.
19+
*/
20+
21+
#include "XPT2046_touch.h"
22+
#include <SPI.h>;
23+
24+
#define CS_PIN PA3 // Chip Select pin
25+
26+
SPIClass mySPI(1); //Create an SPI instance on SPI1 port.
27+
XPT2046_touch ts(CS_PIN, mySPI); // Chip Select pin, SPI port
28+
29+
uint16_t xy[2];
30+
31+
void setup() {
32+
Serial1.begin(9600);
33+
Serial1.println("-------------------------------------------------");
34+
Serial1.println("XPT2046 example sketch");
35+
Serial1.println("Copyright (c) 02 Dec 2015 by Vassilis Serasidis");
36+
Serial1.println("Home: http://www.serasidis.gr");
37+
Serial1.println("-------------------------------------------------");
38+
ts.begin(); //Begin TouchScreen.
39+
40+
}
41+
42+
void loop() {
43+
if(ts.read_XY(xy)){ //If the touch screen is preesed, read the X,Y coordinates and print them on Serial port.
44+
Serial1.print("X: ");
45+
Serial1.println(xy[0]); //Print X value
46+
Serial1.print("Y: ");
47+
Serial1.println(xy[1]); //Print Y value
48+
Serial1.println();
49+
delay(500);
50+
}
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#######################################
2+
# Syntax Coloring Map For XPT2046_touch
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
XPT2046_touch KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
setButtonsNumber KEYWORD2
17+
getButtonNumber KEYWORD2
18+
read_XY KEYWORD2
19+
20+
#######################################
21+
# Instances (KEYWORD2)
22+
#######################################
23+
24+
25+
#######################################
26+
# Constants (LITERAL1)
27+
#######################################
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=Serasidis_XPT2046_touch
2+
version=1.0
3+
author=Vasillis Serasidis.
4+
email=avrsite@yahoo.gr
5+
sentence=A simple XPT2046 Touch screen driver
6+
paragraph=A simple XPT2046 Touch screen driver
7+
url=http://wwww.serasidis.gr
8+
architectures=STM32F1
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
*
3+
*
4+
*/
5+
6+
7+
8+
#include "Arduino.h"
9+
#include "XPT2046_touch.h"
10+
11+
12+
/****************************************************************************/
13+
XPT2046_touch::XPT2046_touch(uint8_t _cs_pin, SPIClass _spiChan) : cs_pin(_cs_pin), my_SPI(_spiChan){
14+
}
15+
16+
17+
/****************************************************************************/
18+
19+
void XPT2046_touch::begin(){
20+
pinMode(cs_pin, OUTPUT);
21+
digitalWrite(cs_pin, HIGH);
22+
my_SPI.begin();
23+
}
24+
25+
/****************************************************************************/
26+
27+
boolean XPT2046_touch::read_XY(uint16_t *xy){
28+
int z1, z2, tmpH, tmpL;
29+
digitalWrite(cs_pin, LOW);
30+
31+
//Check if touch screen is pressed.
32+
SPI.transfer(B10110011); // Z1
33+
delay(10);
34+
tmpH = (my_SPI.transfer(0) << 5);
35+
tmpL = (my_SPI.transfer(0) >> 3);
36+
z1 = tmpH | tmpL;
37+
38+
SPI.transfer(B11000011); // Z2
39+
delay(10);
40+
tmpH = (my_SPI.transfer(0) << 5);
41+
tmpL = (my_SPI.transfer(0) >> 3);
42+
z2 = tmpH | tmpL;
43+
44+
if((z2 - z1) < Z_THRESHOLD){ //If the touch screen is pressed, read the X,Y coordinates from XPT2046.
45+
my_SPI.transfer(B11010011); // X
46+
delay(10);
47+
tmpH = (my_SPI.transfer(0) << 5);
48+
tmpL = (my_SPI.transfer(0) >> 3);
49+
xy[0] = tmpH | tmpL;
50+
51+
my_SPI.transfer(B10010011); // Y
52+
delay(10);
53+
tmpH = (my_SPI.transfer(0) << 5);
54+
tmpL = (my_SPI.transfer(0) >> 3);
55+
xy[1] = tmpH | tmpL;
56+
digitalWrite(cs_pin, HIGH);
57+
return true;
58+
}
59+
digitalWrite(cs_pin, HIGH);
60+
return false;
61+
}
62+
63+
/****************************************************************************/
64+
void XPT2046_touch::setButtonsNumber(byte columnButtons, byte rowButtons ){
65+
_rowButtons = rowButtons;
66+
_columnButtons = columnButtons;
67+
}
68+
69+
/****************************************************************************/
70+
uint8_t XPT2046_touch::getButtonNumber(){
71+
uint16_t xy[2];
72+
uint8_t tmp, buttonNum;
73+
int div;
74+
75+
if(read_XY(xy)){
76+
77+
div = (X_MAX + X_MIN) / (_columnButtons + 1);
78+
buttonNum = ((xy[1] / div));
79+
80+
div = (Y_MAX + Y_MIN) / _rowButtons;
81+
tmp = ((xy[0] / div));
82+
83+
return ((buttonNum * _rowButtons) + tmp + 1); //Return the button number.
84+
}
85+
86+
return 0; //Touch screen is not pressed.
87+
}
88+
/****************************************************************************/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
*
3+
*
4+
*
5+
*
6+
*/
7+
8+
9+
#ifndef XPT2046_touch_h
10+
#define XPT2046_touch_h
11+
#include <Arduino.h>
12+
#include <SPI.h>
13+
14+
#define Z_THRESHOLD 3000
15+
16+
// Pre-defined touch screen calibration for using the 2.4" ILI9341 LCD
17+
#define X_MIN 830
18+
#define X_MAX 3800
19+
#define Y_MIN 550
20+
#define Y_MAX 3550
21+
22+
/**
23+
*
24+
*/
25+
26+
class XPT2046_touch{
27+
private:
28+
uint8_t cs_pin;
29+
SPIClass my_SPI;
30+
uint8_t _rowButtons = 1;
31+
uint8_t _columnButtons = 1;
32+
33+
public:
34+
XPT2046_touch(uint8_t _cs_pin, SPIClass _spiChan); //Contructor.
35+
void begin();
36+
void setButtonsNumber(byte rowButtons, byte columnButtons);
37+
uint8_t getButtonNumber();
38+
boolean read_XY(uint16_t *xy);
39+
};
40+
41+
#endif
42+

0 commit comments

Comments
 (0)