xpt2046 touchscreen control with the ILI9341 TFT & Arduino
Posted: Wed Feb 12, 2020 4:51 pm
Hi Guys
I'm trying to use the STM32 "blue pill" board with a TFT screen with touch controlled by XPT2046. Its a fairly standard looking 2.8" module available in the usual places.
I've got the TFT connected to SPI1 and the touch to SPI2 - I am trying to follow the information here:
https://github.com/PaulStoffregen/XPT2046_Touchscreen
The code gets so far and then seems to halt - I've added comments over the working and non working point in the code - I'm a bit stuck now! I've filled the code with Serial1.print statements to debug and here's the output of Serial1:

I'm not "looping" in my main loop and either:
Mark
G0MGX
I'm trying to use the STM32 "blue pill" board with a TFT screen with touch controlled by XPT2046. Its a fairly standard looking 2.8" module available in the usual places.
I've got the TFT connected to SPI1 and the touch to SPI2 - I am trying to follow the information here:
https://github.com/PaulStoffregen/XPT2046_Touchscreen
The code gets so far and then seems to halt - I've added comments over the working and non working point in the code - I'm a bit stuck now! I've filled the code with Serial1.print statements to debug and here's the output of Serial1:
I'm not "looping" in my main loop and either:
- The SPI has dissapeared up its backside
- I've screwed up the Serial comms somehow
- Something else I cant think of is happening
Mark
G0MGX
Code: Select all
#include "SPI.h"
#include ".\Adafruit_GFX_AS.h"
#include ".\Adafruit_ILI9341_STM.h"
#include <XPT2046_Touchscreen.h>
#define TFT_CS PA0
#define TFT_DC PA2
#define TFT_RST PA1
// touch screen is on SPI2
#define T_CS PA8
#define TIRQ_PIN PA15
Adafruit_ILI9341_STM tft = Adafruit_ILI9341_STM(TFT_CS, TFT_DC, TFT_RST); // Use hardware SPI
XPT2046_Touchscreen ts(T_CS, TIRQ_PIN);
int nFrames = 100; // higher number, slower full-cycle annimation
void setup() {
Serial1.begin(9600);
Serial1.println("Starting");
Serial1.println("Start TFT Gubbins");
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setCursor(120, 50);
tft.setTextSize(5);
tft.print("Test");
tft.setCursor(80, 120);
tft.print("Touch");
Serial1.println("End TFT Gubbins");
delay(2000);
Serial1.println("Start Clear TFT");
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
Serial1.println("End Clear TFT");
// any call to ts breaks the SPI interface. Is ts on SPI2?
Serial1.println("Start ts initialise");
ts.begin();
ts.setRotation(1);
Serial1.println("End ts initialise");
}
boolean wastouched = true;
void loop()
{
Serial1.println("Loop Start");
boolean istouched;
TS_Point p;
istouched = ts.touched();
Serial1.println("After ps.touched call");
Serial1.print("istouched is ");
Serial1.println(istouched);
if (istouched)
{
Serial1.println("inside if(istouched)");
p = ts.getPoint();
Serial1.println("after getPoint call");
Serial1.print("wastouched is ");
Serial1.println(wastouched);
if (!wastouched)
{
Serial1.println("Inside if (!wastouched)");
Serial1.println("printing touch");
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(60, 80);
tft.print("Touch");
}
Serial1.println("After if (!wastouched)");
tft.setTextColor(ILI9341_GREEN);
Serial1.println("After green text");
tft.setTextSize(5);
Serial1.println("After text size");
tft.setCursor(100, 150);
Serial1.println("After set cursor"); // this happens
tft.print("x = ");
Serial1.println("After print x="); // this doesnt happen
tft.print(p.x);
tft.setCursor(100, 180);
tft.print("y = ");
tft.print(p.y);
Serial1.print(", x = ");
Serial1.print(p.x);
Serial1.print(", y = ");
Serial1.println(p.y);
}
else
{
if (wastouched)
{
Serial1.println("printing no touch");
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setCursor(120, 50);
tft.print("No");
tft.setCursor(80, 120);
tft.print("Touch");
}
}
wastouched = istouched;
delay(100);
}