Menu

XPT2046 ili9341 328p

2020-10-18
2020-11-05
  • stan cartwright

    stan cartwright - 2020-10-18

    I tried for lots to get ili9341 touch to work and eventually it worked using software spi.
    I tried the demo from latest test gcb and it works with hspi on a few same displays.
    The line #define XPT2046_ReadSamples 15 "seems" new but it works well!!
    It has to be set to GLCDRotate Portrait for the touch x,y to match the screen
    although the code looks like it's supposed to take care of that.
    I am happy this is finally working.
    This is the demo I used

    I had to swap these lines for it to work ie wireing.

        #define XPT2046_DI       DIGITAL_12          ' Data in | MISO
        #define XPT2046_DO       DIGITAL_11          ' Data out | MOSI
    
     

    Last edit: stan cartwright 2020-10-18
  • stan cartwright

    stan cartwright - 2020-10-20

    Evan, here is a problem
    Using a device with 3.3V logic output and 5V logic input..then is ili9341 cs-touch line need pulling up?
    via 5V voltage divider to raise 3.3V to 5V logic.
    the XPT2046 demo display works but not the touch
    I tried trello but probably messed up question.

     
    • Anobium

      Anobium - 2020-10-21

      If this is LGT related, until we release to the general folks, use Trello

       
  • stan cartwright

    stan cartwright - 2020-11-04

    This mega328 demo works very well.
    It has the line #define XPT2046_ReadSamples 15 which made the hspi work...did it?
    anyway works and accurate.
    And a video how good it is https://www.youtube.com/watch?v=WJa3ZFUI43E
    new batteries for my camera :)

    '''A demonstration program for GCGB and GCB.
    '''--------------------------------------------------------------------------------------------------------------------------------
    '''This program is a simple GLCD demonstration of the ILI9341 GLCD with the XPT2046 Touch capabilities.
    '''It is a nice graphical LCD, suitable for a lot of various projects.
    '''This program shows the calibration of the touch with the specific GLCD.
    '''The GLCD is connected to the microprocessor as shown in the hardware section of this code.
    ''':
    '''This has been tested using the hardware and software SPI option.
    ''':
    '''Note: for the correct operation of this display you MUST connect via a CD4050, see http://forum.arduino.cc/index.php?topic=181679.90
    ''':
    '''@author
    '''@licence GPL
    '''@version 1.1
    '''@date
    '''********************************************************************************
    
        'Chip Settings.
        #chip mega328p, 16
        #option explicit
    
        #include <glcd.h>
        #include <xpt2046.h>
        #include <uno_mega328p.h >
    
        '******************************************************************************************************
        'Setup the GLCD
    
        #define GLCD_TYPE GLCD_TYPE_ILI9341
    
        'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
        #define GLCD_DC       DIGITAL_8           ' Data command line
        #define GLCD_CS       DIGITAL_10          ' Chip select line
        #define GLCD_RESET    DIGITAL_9           ' Reset line
    
        #define GLCD_DI       DIGITAL_12          ' Data in | MISO    - Not used therefore not really required
        #define GLCD_DO       DIGITAL_11          ' Data out | MOSI
        #define GLCD_SCK      DIGITAL_13          ' Clock Line
        #define ILI9341_HardwareSPI               ' remove/comment out if you want to use software SPI.
        'Optionally, you can define the HWSPIMode by using #define HWSPIMode masterfast where #define HWSPIMode is masterslow|master|masterfast
        'This will overwrite the default in the GLCD library.  This is so users do not have to change the library.
        'the default is "masterfast"
        'The following example is commented out intentionally
            '#define HWSPIMode masterfast
    
        '******************************************************************************************************
        'Setup the XPT2046
    
        'Typical calibration range for Hardware SPI when using XPT2046
        'for 320 * 240 screeen... may be different for others
        #define XPT2046XMIN   15
        #define XPT2046YMIN   14
        #define XPT2046XMAX   110
        #define XPT2046YMAX   113
        #define XPT2046_ReadSamples 15
    
        'Typical Calibration range for Software SPI when using XPT2046
    '    #define XPT2046XMIN   30
    '    #define XPT2046YMIN   25
    '    #define XPT2046XMAX   210
    '    #define XPT2046YMAX   225
    
    
        #define XPT2046_DI       DIGITAL_12          ' Data in | MISO
        #define XPT2046_DO       DIGITAL_11          ' Data out | MOSI
        #define XPT2046_SCK      DIGITAL_13          ' Clock Line
        #define XPT2046_CS       DIGITAL_2           ' Chip select line
        #define XPT2046_IRQ      ANALOG_5
        #define XPT2046_HardwareSPI                  ' remove/comment out if you want to use software SPI - must use same method as GLCD
        'Optionally, you can define the HWSPIMode by using #define HWSPIMode masterfast where #define HWSPIMode is masterslow|master|masterfast
        'This will overwrite the default in the XPT2046 library.  This is so users do not have to change the library.
        'the default is "masterfast"
        'The following example is commented out intentionally
            '#define HWSPIMode masterfast
    
    
    
        'Set the initial calibration - shown here to show the method.
        'If you rotate the screen then you will have to call the method to reset these parameters.
        SetCalibation_XPT2046 ( XPT2046XMIN, XPT2046XMAX, XPT2046YMIN, XPT2046YMAX, GLCD_WIDTH, GLCD_HEIGHT  )
    
        '******************************************************************************************************
        'Main program
        GLCDRotate Portrait
        GLCDCLS TFT_NAVY
        SetCalibation_XPT2046 ( XPT2046XMIN, XPT2046XMAX, XPT2046YMIN, XPT2046YMAX, GLCD_WIDTH, GLCD_HEIGHT )
        ScreenLayout
    
        dim Xconverge, Yconverge as Integer
        dim convergeCounter as byte
        do
    
          Xconverge = 0
          Yconverge = 0
          convergeCounter = 1
    
          filledbox Current_GLCD_WIDTH, 0, Current_GLCD_WIDTH-10, 10, TFT_RED
          'always wait for touch event
          wait while isTouched_XPT2046
          filledbox Current_GLCD_WIDTH, 0, Current_GLCD_WIDTH-10, 10, TFT_YELLOW
          do
              repeat 5
                  'get the values -
                  GetXY_XPT2046( XTouchPoint_XPT2046, YTouchPoint_XPT2046 )
              end Repeat
              'test for settling by testing for convergance
              if ABS( ( Xconverge - XTouchPoint_XPT2046 ) ) > 1  or  ABS( ( Yconverge - YTouchPoint_XPT2046 ) ) > 1  then
                  Xconverge = XTouchPoint_XPT2046
                  Yconverge = YTouchPoint_XPT2046
                  convergeCounter++
              Else
                  'converged
                  exit do
              end if
          Loop
          'Flip the data to handle for the screen rotation
          YTouchPoint_XPT2046 = Current_GLCD_HEIGHT - YTouchPoint_XPT2046
          filledbox Current_GLCD_WIDTH, 0, Current_GLCD_WIDTH-10, 10, TFT_GREEN
            if XTouchPoint_XPT2046 > Current_GLCD_WIDTH-59 and YTouchPoint_XPT2046 > Current_GLCD_HEIGHT-20  then
              if XTouchPoint_XPT2046 < Current_GLCD_WIDTH and  YTouchPoint_XPT2046 < Current_GLCD_HEIGHT then
                'clear
                ScreenLayout
              end if
          else
    
              FilledCircle XTouchPoint_XPT2046, YTouchPoint_XPT2046, 3, TFT_LIGHTGREY
    
              'Show the raw data
              GLCDPrint ( 24, 56, pad(str(XTouchPoint_XPT2046raw),3 ) )
              GLCDPrint ( 24, 72, pad(str(YTouchPoint_XPT2046raw),3 ) )
              GLCDPrint ( 24, 90, pad(str(convergeCounter ) ,3 ) )
    
              'Automatically adjust the values to min and max
              'Not needed in a real program use SetCalibation_XPT2046( XPT2046_Xmin, XPT2046_Xmax, XPT2046_Ymin, XPT2046_Ymax )
              if XTouchPoint_XPT2046raw < XPT2046_Xmin then XPT2046_Xmin = XTouchPoint_XPT2046raw
              if YTouchPoint_XPT2046raw < XPT2046_Ymin then XPT2046_Ymin = YTouchPoint_XPT2046raw
              if XTouchPoint_XPT2046raw > XPT2046_Xmax then XPT2046_Xmax = XTouchPoint_XPT2046raw
              if YTouchPoint_XPT2046raw  > XPT2046_Ymax then XPT2046_Ymax = YTouchPoint_XPT2046raw
    
              'show the values
              GLCDPrint ( 102, 56, pad(str(XPT2046_Xmin),3 ) )
              GLCDPrint ( 102, 72, pad(str(XPT2046_Ymin),3 ) )
              GLCDPrint ( 162, 56, pad(str(XPT2046_Xmax),3 ) )
              GLCDPrint ( 162, 72, pad(str(XPT2046_Ymax),3 ) )
    
              GLCDPrint ( 24, 230, pad(str(XTouchPoint_XPT2046),5 ))
              GLCDPrint ( 24, 246, pad(str(YTouchPoint_XPT2046),5 ))
    
              'do not hammer the touch
              wait 20 ms
    
          end if
    
        loop
    
    
    sub ScreenLayout
    
            #define GLCD_OLED_FONT
            GLCDfntDefaultsize = 2
            GLCDFontWidth = 5
            'Layout the screen
            GLCDCLS
            GLCDPrint ( 2, 3, "XPT2046 demonstration 2019")
            line 0, 34, Current_GLCD_WIDTH, 34
            line 0, 128, Current_GLCD_WIDTH, 128
            line 0, 220, Current_GLCD_WIDTH, 220
            GLCDPrint ( 2, 25, "Dynamic Calibration")
            GLCDPrint ( 22, 40, "Raw    Min   Max")
            GLCDPrint ( 2, 56, "X:")
            GLCDPrint ( 2, 72, "Y:")
            GLCDPrint ( 2, 90, "C:")
    
            GLCDPrint ( 2, 122, "Current Calibration")
            GLCDPrint ( 2, 146, "X:")
            GLCDPrint ( 2, 162, "Y:")
            GLCDPrint ( 102, 146, pad(str(XPT2046_Xmin),3 ) )
            GLCDPrint ( 102, 162, pad(str(XPT2046_Ymin),3 ) )
            GLCDPrint ( 162, 146, pad(str(XPT2046_Xmax),3 ) )
            GLCDPrint ( 162, 162, pad(str(XPT2046_Ymax),3 ) )
            GLCDPrint ( 2, 214, "Scaled")
            GLCDPrint ( 2, 230, "X:")
            GLCDPrint ( 2, 246, "Y:")
    
            GLCDPrint ( 120, 214, "GLCD Size")
            GLCDPrint ( 120, 230, "X:" + Str( Current_GLCD_WIDTH ) )
            GLCDPrint ( 120, 246, "Y:" + Str( Current_GLCD_HEIGHT ) )
    
    
            GLCDfntDefaultsize = 1
            GLCDPrint ( 2, Current_GLCD_HEIGHT-17, "v0.9c")
            GLCDfntDefaultsize = 2
    
            filledbox Current_GLCD_WIDTH, 0, Current_GLCD_WIDTH-10, 10, TFT_RED
    
            Dim oldGLCDBackground as word
            oldGLCDBackground = GLCDBackground
                GLCDBackground = TFT_WHITE
                GLCDPrint ( Current_GLCD_WIDTH-58, Current_GLCD_HEIGHT-18, "Clear", TFT_BLACK )
            GLCDBackground = oldGLCDBackground
    
            ' ------ Version Control - optional
            #include "GCBVersionNumber.cnt"
            'Include the GCBVersionNumber.cnt to increment versionString and create the build time string called GCBBuildTimeStr.
            ' versionString a string is created automatically.
            ' GCBBuildTimeStr is a string that is also created automatically.
            ' use "GCBVersionNumber.cnt" as this will create a local copy of the versionString tracker.
            ' if you use <GCBVersionNumber.cnt> this is a system wide versionString tracker.
    
            dim versionString as string * 40
            versionString = "Build: "+GCBBuildStr
            versionString = versionString + "@"+GCBBuildTimeStr
            GLCDfntDefaultsize = 1
            GLCDPrint ( 2, Current_GLCD_HEIGHT-10, versionString)
            GLCDfntDefaultsize = 2
            ' ------ End of Version Control - optional
    End Sub
    
     
  • stan cartwright

    stan cartwright - 2020-11-05

    The code looks complicated
    may be take 4 x,y samples and the difference between top y and bottom y is y centre and same for most x - least x as centre x.
    are raw values worth trying to use?
    what's GLCDPrint ( 102, 56, pad(str(XPT2046_Xmin),3 ) ) ?
    looks new...it's probably in help

     
    • Anobium

      Anobium - 2020-11-05

      The GLCDPrint is simple a Left padding of spaces to ensure any prior string is erased, as is 999 to 1 with pad this becomes "999" to "1 " {trailing spaces erase the last two chars "99"

       

Log in to post a comment.