News:

Let's find out together what makes a PIC Tick!

Main Menu

Touch XPT2046

Started by Ivano, Nov 19, 2022, 04:44 PM

Previous topic - Next topic

Ivano

Good morning,
does anyone have experience with XPT2046 touch present on a 2.8 inch display with ili9341?
I have read the datasheet several times.
I did some experiment but I didn't quite understand if I have to send the registry value multiple times and with different parameters and which ones to locate the raw data.
I don't understand if I have to read x and y separately or in one reading, is there a particular procedure?

Craig

Hi Ivano I worked on the XTP2046 years ago and I found this video Very Helpful In understanding it's workings
https://www.youtube.com/watch?v=QvKkK6dcW2E
Regards
Craig

Craig

Ivano I found this in my project folders which Les has done on the ADS7486 Chip which is the same as the XTP2046 so this will help you understand.


'================================================================
'
' 4-Wire Resistive Touchscreen Demo
' Mounted on a 128x64 KS0108 Graphical LCD.
' The program allows drawing directly on the LCD with a stylus.
'
' Upon first power up, the screen requires calibration.
' Touch the Top-Left and Bottom-Right corners of the screen and it's ready to go.
' The calibration data is stored in eeprom, thus eliminating the calibration upon every start-up
'
' The 4-Wire Touchscreen is connected directly to AN0, AN1, AN2, and AN3 of the PICmicro
' With 10K Ohm pull-down resistors on each ADC input.
'
' Touchscreen Cable Configuration:-
'
'    X1 ---------\
'                 \
'    Y1 ---------\ \------O To RA0 (AN0)
'  From           \-------O To RA1 (AN1)
'  Touchscreen    /-------O To RA2 (AN2)
'    X2 ---------/ /------O To RA3 (AN3)
'                 /
'    Y2 ---------/
'
    Device = 18F452
    Declare Xtal = 8
Reminders = Off
    Declare PLL_Req = True                          ' We're now operating at 32Mhz from an 8Mhz crystal
    Declare Optimiser_Level = 3                     ' Maximum optimisation
    Declare Dead_Code_Remove = On                   ' Tighten the code even further
'
' Samsung KS0108 Graphic LCD setup
'
    Declare LCD_DTPort = PORTD
    Declare LCD_RSPin = PORTC.1
    Declare LCD_ENPin = PORTE.0
    Declare LCD_RWPin = PORTC.0
    Declare LCD_CS1Pin = PORTE.1
    Declare LCD_CS2Pin = PORTE.2
    Declare LCD_Type = Graphic
    Declare Internal_Font = On
    Declare Font_Addr = 0
'
' Setup for the ADC
'
    Declare Adin_Res = 10                           ' Set the resolution to 10
    Declare Adin_Tad = 8_FOSC                       ' Fosc:8
    Declare Adin_Stime = 10                         ' Allow 10us for charge time

    Dim TouchFlag As Bit                            ' Used to indicate the stylus has been lifted off the screen

$define True 1
$define False 0
'--------------------------------------------------------------------------------------

    Include "TouchScreen.Inc"                       ' Load the touchscreen macros and subroutines into memory

'--------------------------------------------------------------------------------------
' The Main Program Starts Here
'
Main:
    DelayMS 100                                     ' Wait for the LCD to stabilise
    Cls                                             ' Clear the LCD
'
' Check if calibration is required
'
    If ERead Calibrated_Data = 0 Then               ' Has calibration already taken place?
        GoSub Calibrate                             ' No. So calibrate the Touchscreen
    Else                                            ' Otherwise...
        wXminimum = ERead wXminimum_Data            ' \
        wYminimum = ERead wYminimum_Data            '  \
        wYmaximum = ERead wYmaximum_Data            '    Read the calibration data from eeprom
        bXrange =   ERead bXrange_Data              '  /
        wYrange =   ERead wYrange_Data              ' /
    EndIf
'
' Start the drawing routine
'
    TouchFlag = False
    While                                           ' Create an infinite loop
        If LookForStylus() = True Then              ' Is the stylus touching the screen?
            If TouchFlag = False Then               ' Yes. So is this the first time?
                'Line 0, Xlocation, Ylocation, Xlocation, Ylocation
                TouchFlag = True                    ' Yes so invert the flag
            Else                                    ' Otherwise...
                Line 1, bXprevious, wYprevious, bXlocation, wYlocation
            EndIf
            bXprevious = bXlocation                 ' Keep a record of the X position
            wYprevious = wYlocation                 ' Keep a record of the Y position
        Else                                        ' Otherwise...
            TouchFlag = False                       ' Invert the Stylus lift-off flag
        EndIf
        DelayMS 50
'
' Look for the Stylus over the Text "Cal" or "Cls"
'
        If HotSpot(0,58,21,63) = True Then          ' If "Cal" found, calibrate the Touchscreen again
            Print At 7,1,"Cal"
            DelayMS 300
            Print At 7,1,Inverse 1,"Cal", Inverse 0
            GoSub Calibrate
        Else If HotSpot(100,58,123,63) = True Then  ' If "Cls" found, clear the LCD
            Cls
            Print At 7,0,Inverse 1," Cal             ", Inverse 0, "Cls", Inverse 1," "
            While LookForStylus() = True
            Wend
        EndIf
        Print At 7,0,Inverse 1," Cal             Cls ", Inverse 0
    Wend

'----------------------------------------------------------------------------------------------
' Calibrate the Touchscreen
Calibrate:
    Cls                                         ' Clear screen
    Print At 1,2, "Calibrate Screen",_
          At 3,6, "  Touch",_
          At 3,6, " Top Left",_
          At 4,5, "Bottom Right"
    CalibrateScreen()                           ' Go and calibrate the screen
    Cls
    Return
'----------------------------------------------------------------------------------------------
' Load the graphic LCD font into code memory

    Include "Font.inc"

'================================================================================================


Ivano

Hi Craig,
this info is interesting. I have clearer ideas now, I'll do some tests.
In the second post I saw that there is the include "TouchScreen.Inc" file which is the one that manages the touch chip, even if the chip is not xpt2046, is it possible to download it somewhere? Thank you

Craig

Hi Ivano

It is in your PDS Folder on the C:\ Drive under "Includes"

Regards
Craig

Ivano

Thanks so much Craig,
I thought it was a file not included in pds and I didn't even look at it, stupid.
I have yet to look at that file, and I've done some x and y screen reading experiments with hardware spi and it's easier than I thought. The video in the link was helpful to me.
Thanks again
Ivan