```cpp
/***************************************************
This is our GFX example for the Adafruit ILI9488 Breakout and Shield
----> http://www.adafruit.com/products/1651
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <SPI.h>
#include <ILI9488_t3.h>
#include <ILI9488_t3_font_ComicSansMS.h>
#include <XPT2046_Touchscreen.h>
#define TEENSY64
// Define the SPI clock frequency, this affects the graphics rendering speed. Too
// fast and the TFT driver will not keep up and display corruption appears.
// With an ILI9341 display 40MHz works OK, 80MHz sometimes fails
// With a ST7735 display more than 27MHz may not work (spurious pixels and lines)
// With an ILI9163 display 27 MHz works OK.
// #define SPI_FREQUENCY 1000000// different spi frequencys did not help
// #define SPI_FREQUENCY 5000000
// #define SPI_FREQUENCY 10000000
// #define SPI_FREQUENCY 20000000
//#define SPI_FREQUENCY 27000000
// #define SPI_FREQUENCY 40000000
// #define SPI_FREQUENCY 55000000 // STM32 SPI1 only (SPI2 maximum is 27MHz)
// #define SPI_FREQUENCY 80000000
// Optional reduced SPI frequency for reading TFT
//#define SPI_READ_FREQUENCY 20000000
// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
//#define SPI_TOUCH_FREQUENCY 2500000
// For the Adafruit shield, these are the default.
#if defined(__MK66FX1M0__) && !defined(TEENSY64)// needs this
//#define TFT_RST 8
//#define TFT_DC 9
//#define TFT_CS 10
ILI9488_t3 tft = ILI9488_t3(&SPI, TFT_CS, TFT_DC, TFT_RST);
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__)// needs this
// On Teensy 4 beta with Paul's breakout out:
// Using pins (MOSI, MISO, SCK which are labeled on Audio board breakout location
// which are not in the Normal processor positions
// Also DC=10(CS), CS=9(BCLK) and RST 23(MCLK)
#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
ILI9488_t3 tft = ILI9488_t3(&SPI, TFT_CS, TFT_DC, TFT_RST);
//#elif defined(TEENSY64)
//#define TFT_RST 255
//#define TFT_DC 20
//#define TFT_CS 21
//#define TFT_SCK 14
//#define TFT_MISO 39
//#define TFT_MOSI 28
//ILI9488_t3 tft = ILI9488_t3(&SPI, TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO);
#else
#error "This example App will only work with Teensy 3.6 or Teensy 4."
#endif
// If using the breakout, change pins as desired
//Adafruit_ILI9488 tft = Adafruit_ILI9488(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
#define CS_PIN 20// for touch screen
//#define SCLK_PIN 8 //8 was 2// Must be 8 for Hardware SPI XIAO
//#define MOSI_PIN 10 //10 was 3// Must be 10 for hardware SPI XIAO// Pin 9 is MISO
// MOSI=11, MISO=12, SCK=13 on others
//XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN 2// for touch screen
//XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling
void setup() {
Serial.begin(9600);
ts.begin();
ts.setRotation(1);
tft.begin();
tft.setRotation(1);// was 3
tft.fillScreen(ILI9488_BLACK);
while (!Serial) ;// serial monitor must be activated
//tft.useFrameBuffer(true); // this makes it get stuck
tft.enableScroll();
//tft.setScrollTextArea(0,282,479,42);// if you want the color bars back.
tft.setScrollTextArea(0,302,479,22);// if last number is higher than 22 there will be color bars.
//tft.useFrameBuffer(true); // this makes it get stuck
tft.setScrollBackgroundColor(ILI9488_BLACK);
tft.setCursor(280, 200);
tft.setFont(ComicSansMS_12);
tft.setTextSize(2);
tft.print("Fixed text");
tft.setTextColor(ILI9488_YELLOW);
tft.setCursor(0, 0);
for(int i=0;i<14;i++){// lines 0 to 13 fit on normal screen
tft.print(" this is line ");
tft.println(i);
delay(500);
}// for
tft.setCursor(0,304);// set cursor for scroll area
for(int i=14;i<29;i++){
tft.print(" this is line ");
tft.println(i);
delay(500);
}// for
for(int i=0;i<20;i++){
tft.print(" this is line ");
tft.println(i);
delay(500);
}// for
tft.fillRoundRect(320 ,0 , 160, 54, 10, ILI9488_PURPLE);// push button
tft.drawRoundRect(320 ,0 , 160, 54, 10, ILI9488_YELLOW);
tft.setCursor(330, 20);
tft.setTextColor(ILI9488_WHITE);
tft.println(" Touch ");
tft.setCursor(0, 304);// set cursor for scroll area // all new text will print on the bottom line as long as there is a tft.println() // no need to clear line.
tft.setTextColor(ILI9488_YELLOW);
}// setup
void loop(void) {
if (ts.touched()) {
TS_Point p = ts.getPoint();// read touch screen
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
delay(30);
Serial.println();
if((p.x > 235) && (p.x < 1440)) {// number ranges adjusted for this touch screen
if ((p.y > 3300) && (p.y <= 3885)) {// number ranges adjusted for this touch screen
Serial.println("Button pushed");
tft.print("Button pushed: ");
tft.print("Pressure = ");
tft.print(p.z);
tft.print(", x = ");
tft.print(p.x);
tft.print(", y = ");
tft.println(p.y);// no need to clear line as long as there is a tft.println()
delay(30);// makes it less flickery
}//p.y
}//p.x
}//touch
}//loop
```