Page 1 of 1

Arduino example: XPT2046 touch and ILI9341 tft.

Posted: Sun Nov 26, 2017 6:05 pm
by Cellie
Simple etch-a-sketch for ESP32 and a SPI ILI9341 tft board with XPT2046 touch controller from ebay etc.

Uses the Adafruit ILI9341 library as the ILI9341_t3 does not compile for me.

Code: Select all

/* Map XPT2046 input to ILI9341 320x240 raster */
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h> /* https://github.com/PaulStoffregen/XPT2046_Touchscreen */

#define TFT_DC             27
#define _sclk              25
#define _mosi              32
#define _miso              39
#define TFT_CS              4
#define TFT_RST            12
#define TFT_BACKLIGHT_PIN   2 /* -via transistor- */
#define TOUCH_CS_PIN       33
#define TOUCH_IRQ_PIN      35

#define TFT_NORMAL          1
#define TFT_UPSIDEDOWN      3

const uint8_t TFT_ORIENTATION = TFT_NORMAL;

Adafruit_ILI9341 tft = Adafruit_ILI9341( TFT_CS, TFT_DC, TFT_RST );

XPT2046_Touchscreen touch( TOUCH_CS_PIN, TOUCH_IRQ_PIN );

void setup() {
  Serial.begin( 115200 );
  SPI.begin( _sclk, _miso, _mosi );
  SPI.setFrequency( 60000000 );

  tft.begin( 10000000, SPI );

  tft.setRotation( TFT_ORIENTATION );
  tft.fillScreen( ILI9341_BLACK );
  tft.setTextColor( ILI9341_GREEN, ILI9341_BLACK );

  pinMode( TFT_BACKLIGHT_PIN, OUTPUT );
  digitalWrite( TFT_BACKLIGHT_PIN, HIGH );

  touch.begin();
  Serial.println( "Touch screen ready." );
}


TS_Point rawLocation;

void loop() {
    while ( touch.touched() )
    {
      rawLocation = touch.getPoint();
      /*
        tft.setCursor( 100, 150 );
        tft.print( "X = " );
        tft.print( rawLocation.x );
        tft.setCursor(100, 180);
        tft.print( "Y = " );
        tft.print( rawLocation.y );

        Serial.print("x = ");
        Serial.print(rawLocation.x);
        Serial.print(", y = ");
        Serial.print(rawLocation.y);
        Serial.print(", z = ");
        Serial.println(rawLocation.z);
      */
      if ( TFT_ORIENTATION == TFT_UPSIDEDOWN )
      {
        tft.drawPixel( map( rawLocation.x, 340, 3900, 0, 320 ),
                       map( rawLocation.y, 200, 3850, 0, 240 ),
                       ILI9341_GREEN );
      }
      if ( TFT_ORIENTATION == TFT_NORMAL )
      {
        tft.drawPixel( map( rawLocation.x, 340, 3900, 320, 0 ),
                       map( rawLocation.y, 200, 3850, 240, 0 ),
                       ILI9341_GREEN );
      }
    }
  //tft.fillScreen(ILI9341_BLACK);
}

Re: Arduino example: XPT2046 touch and ILI9341 tft.

Posted: Sun May 17, 2020 10:16 pm
by henrik04
The problem I saw ( ESP32 and ILI9341 + SDCARD + XPT2046 Touch Screen) is that the XPT2046 library does not let you specify which SPI hardware you want to use. :?
I mean VSPI or HSPI.
Three SPI channel exists on these LCD boards.
Two MOSI/MISO SCK will be common on the ESP32. (With different CS lines obviously)

How do you resolve that situatiuon :?:

Thank you in advance.
Henri

Re: Arduino example: XPT2046 touch and ILI9341 tft.

Posted: Sun Apr 18, 2021 3:09 pm
by rmetzner49
Since HSPI is used on most Dev Boards for EEPROM access, I should think you would want to stay off of that one with any other business.
In the Arduino world, VSPI is the default channel the Compiler uses. SPI devices generally don't have a problem sharing channels unless one device (e.g. RA8875) doesn't adhere to the SPI rules.