Menu

xpt2046 touch

2021-08-15
2021-09-12
1 2 > >> (Page 1 of 2)
  • stan cartwright

    stan cartwright - 2021-08-15

    I can see the include file but it looks initially hard. How to slow it down?
    It looks all software.

    '    XPT2046 routines for the GCBASIC compiler
    '    Copyright (C) 2018-2020 Evan R. Venn
    
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
    '    v0.9e - more samples
    '    v0.9f - revised calibration method
    '    v0.9g - changed the IRQ
    '    v.100 - first formal release
    
     '''    '******************************************************************************************************
    '''    'Setup the XPT2046
    '''
    '''    'Typical calibration range for Hardware SPI when using XPT2046
    '''    'for 320 * 240 screeen... may be different for others
    '''    #define XPT2046XMIN   17
    '''    #define XPT2046YMIN   15
    '''    #define XPT2046XMAX   100
    '''    #define XPT2046YMAX   110
    '''
    '''    '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.  Should be shared port the GLCD (it will be if hardware SPI is used).  No essential to define as the librart will select the GLCD port
    '''    #define XPT2046_DO       DIGITAL_11          ' Data out | MOSI.  .  Should be shared port the GLCD (it will be if hardware SPI is used).  No essential to define as the librart will select the GLCD port
    '''    #define XPT2046_SCK      DIGITAL_13          ' Clock Line.  .  Should be shared port the GLCD (it will be if hardware SPI is used).  No essential to define as the librart will select the GLCD port
    '''
    '''    #define XPT2046_CS       DIGITAL_2           ' Touch chip select line. MUST BE DEFINED.
    '''    #define XPT2046_IRQ      ANALOG_5            ' Event line... something touched the GLCD touch sensor.  MUST BE DEFINED.
    '''    #define XPT2046_HardwareSPI                  ' remove/comment out if you want to use software SPI - must use same method as GLCD
    '''
    '''    '******************************************************************************************************
    
    
    
    'No change below here
    #define XPT2046_ReadSamples 200
    
    #define XPT2046_CFG_START   128       'bit 7
    
    #define XPT2046_CFG_MUX     112       'bits 7-4 the channel select bits   A2, A1 & A0
    #define XPT2046_MUX_Y       80
    #define XPT2046_MUX_X       16
    #define XPT2046_MUX_Z1      48
    #define XPT2046_MUX_Z2      64
    
    
    #define XPT2046_CFG         8
    #define XPT2046_CFG_8BIT    8         'bit 3 - the 12 or 8 bit conversion
    #define XPT2046_CFG_12BIT   0
    
    #define XPT2046_CFG         4
    #define XPT2046_CFG_SER     4         'bit 2 - Single-ended or Differential conversion
    #define XPT2046_CFG_DFR     0
    
    #define XPT2046_CFG_PWR     0         'bit 1-0 - the power mask.  PD1 & PD0
    #define XPT2046_CFG_PWR_1   1         'bit 1-0 - the power mask.  PD1 & PD0
    #define XPT2046_CFG_PWR_2   2         'bit 1-0 - the power mask.  PD1 & PD0
    #define XPT2046_CFG_PWR_3   3         'bit 1-0 - the power mask.  PD1 & PD0
    
    
    #script
    
      'Test, map to the GLCD pins if they exist
      XPT2046_DI_PORT = 0
      if XPT2046_DI Then
        XPT2046_DI_PORT = 1
      end if
      if XPT2046_DI_PORT = 0 Then
        if GLCD_DI Then
            XPT2046_DI = GLCD_DI
            XPT2046_DI_PORT = 1
        end if
      end if
      if XPT2046_DI_PORT = 0 Then
          Warning "Missing Port Assignment XPT2046_DI"
      end if
    
      XPT2046_DO_PORT = 0
      if XPT2046_DO Then
        XPT2046_DO_PORT = 1
      end if
      if XPT2046_DO_PORT = 0 Then
        if GLCD_DO Then
            XPT2046_DO = GLCD_DO
            XPT2046_DO_PORT = 1
        end if
      end if
      if XPT2046_DO_PORT = 0 Then
          Warning "Missing Port Assignment XPT2046_DO"
      end if
    
      XPT2046_SCK_PORT = 0
      if XPT2046_SCK Then
        XPT2046_SCK_PORT = 1
      end if
      if XPT2046_SCK_PORT = 0 Then
        if GLCD_SCK Then
            XPT2046_SCK = GLCD_SCK
            XPT2046_SCK_PORT = 1
        end if
      end if
      if XPT2046_SCK_PORT = 0 Then
          Warning "Missing Port Assignment XPT2046_SCK"
      end if
    
      XPT2046_CS_PORT = 0
      if XPT2046_CS Then
        XPT2046_CS_PORT = 1
      end if
      if XPT2046_CS_PORT = 0 Then
          Warning "Missing Port Assignment XPT2046_CS"
      end if
    
      XPT2046_IRQ_PORT = 0
      if XPT2046_IRQ Then
        XPT2046_IRQ_PORT = 1
      end if
      if XPT2046_IRQ_PORT = 0 Then
          Warning "Missing Port Assignment XPT2046_IRQ"
      end if
    
    #endscript
    
    #startup Init_XPT2046, 99
    
      dim XTouchPoint_XPT2046, YTouchPoint_XPT2046 as Word
      dim XTouchPoint_XPT2046raw, YTouchPoint_XPT2046raw as word
      dim XPT2046_Xmin, XPT2046_Ymin, XPT2046_Xmax, XPT2046_Ymax  as Byte
      dim Current_GLCD_WIDTH, Current_GLCD_HEIGHT as word
    
    Sub Init_XPT2046 (Optional In precision = PREC_EXTREME)
    
        XPT2046_Xmin =  XPT2046XMIN
        XPT2046_Ymin =  XPT2046YMIN
        XPT2046_Xmax =  XPT2046XMAX
        XPT2046_Ymax =  XPT2046YMAX
    
        DIR XPT2046_DI IN
        DIR XPT2046_IRQ IN
        DIR XPT2046_DO OUT
        DIR XPT2046_SCK OUT
        DIR XPT2046_CS  OUT
        #ifdef XPT2046_HardwareSPI
          ' harware SPI mode
          asm showdebug SPI constant used equates to HWSPIMODESCRIPT
          SPIMode HWSPIMODESCRIPT, 0
        #endif
    
        Repeat 4
            set XPT2046_CS OFF
            wait 10 ms
            set XPT2046_CS ON
            wait 10 ms
        End Repeat
    
        #define isTouched_XPT2046  XPT2046_IRQ = 1
        EnableIRQ_XPT2046
    
    End Sub
    dim Current_GLCD_WIDTH, Current_GLCD_HEIGHT as word
    sub SetCalibation_XPT2046( XPT2046_Xmin as word, XPT2046_Xmax as word, XPT2046_Ymin as word, XPT2046_Ymax as word )
         'this simply set the variables
    End Sub
    
    
    sub SetCalibation_XPT2046( XPT2046_Xmin as word, XPT2046_Xmax as word, XPT2046_Ymin as word, XPT2046_Ymax as word, Current_GLCD_WIDTH as word, Current_GLCD_HEIGHT as word )
         'this simply set the variables
    End Sub
    
    
    Sub EnableIRQ_XPT2046
    
        set XPT2046_CS OFF
        SendData_XPT2046( XPT2046_CFG_START | XPT2046_CFG_8BIT | XPT2046_CFG_DFR | XPT2046_MUX_Y )
        wait 1 ms
        set XPT2046_CS ON
    
    End Sub
    
    '''Send a data byte to the XPT2046 GLCD
    '''@param XPT2046SendByte Byte to send
    '''@hide
    sub  TransferData_XPT2046( IN XPT2046SendByte as byte, XPT2046OutByte as byte )
    
      Dim XPT2046OutByte as byte
      XPT2046OutByte = 0
    
      #ifdef XPT2046_HardwareSPI
         SPITransfer  XPT2046SendByte,  XPT2046OutByte
      #endif
    
      #ifndef XPT2046_HardwareSPI
          repeat 8
    
            if XPT2046SendByte.7 = ON then
              set XPT2046_DO ON;
            else
              set XPT2046_DO OFF;
            end if
            SET XPT2046_SCK On;
    
            if XPT2046_DI = 1 then
              XPT2046OutByte.0 = 1
            end if
    
            rotate XPT2046OutByte left
            rotate XPT2046SendByte left
            set XPT2046_SCK Off;
    
          end Repeat
    
      #endif
    '  wait 250 us
    
    end Sub
    
    '''Send a command to the XPT2046 GLCD
    '''@param XPT2046SendByte Command to send
    '''@hide
    sub  SendData_XPT2046( IN XPT2046SendByte as byte )
    
    
      #ifdef XPT2046_HardwareSPI
         SPITransfer  XPT2046SendByte,  XPT2046TempOut
      #endif
    
      #ifndef XPT2046_HardwareSPI
          set XPT2046_SCK Off;
          repeat 8
    
            if XPT2046SendByte.7 = ON  then
              set XPT2046_DO ON;
            else
              set XPT2046_DO OFF;
            end if
            SET XPT2046_SCK On;
            rotate XPT2046SendByte left
            set XPT2046_SCK Off;
    
          end repeat
      #endif
    
    end Sub
    
    
    Sub GetXY_XPT2046 (out XTouchPoint_XPT2046 as word, out YTouchPoint_XPT2046 as word )
        '
        '#define XPT2046_CFG_START   128       'bit 7
        '
        '#define XPT2046_CFG_MUX     112       'bits 7-4 the channel select bits   A2, A1 & A0
        '#define XPT2046_MUX_Y       80
        '#define XPT2046_MUX_X       16
        '#define XPT2046_MUX_Z1      48
        '#define XPT2046_MUX_Z2      64
        '
        '
        '#define XPT2046_CFG         8
        '#define XPT2046_CFG_8BIT    8         'bit 3 - the 12 or 8 bit conversion. Low is 12bit, High is 8bit
        '#define XPT2046_CFG_12BIT   0
        '
        '#define XPT2046_CFG         4
        '#define XPT2046_CFG_SER     4         'bit 2 - Single-ended or Differential conversion
        '#define XPT2046_CFG_DFR     0
        '
        '#define XPT2046_CFG_PWR     0         'bit 1-0 - the power mask.  PD1 & PD0
        '#define XPT2046_CFG_PWR_1   1         'bit 1-0 - the power mask.  PD1 & PD0
        '#define XPT2046_CFG_PWR_2   2         'bit 1-0 - the power mask.  PD1 & PD0
        '#define XPT2046_CFG_PWR_3   3         'bit 1-0 - the power mask.  PD1 & PD0
    
        dim XTouchPoint_XPT2046, YTouchPoint_XPT2046 as Word
        dim NewXTouchPoint_XPT2046, NewYTouchPoint_XPT2046 as Word
        dim XTouchPoint_XPT2046raw, YTouchPoint_XPT2046Raw as word
        dim Current_GLCD_WIDTH, Current_GLCD_HEIGHT as word
    
        set XPT2046_CS OFF
    
          Repeat 10
              TransferData_XPT2046  ( XPT2046_CFG_START | XPT2046_CFG_8BIT | XPT2046_CFG_DFR | XPT2046_MUX_X  | XPT2046_CFG_PWR ), XPT2046TempOut
              TransferData_XPT2046  0,  YTouchPoint_XPT2046
              TransferData_XPT2046  ( XPT2046_CFG_START | XPT2046_CFG_8BIT | XPT2046_CFG_DFR | XPT2046_MUX_Y  | XPT2046_CFG_PWR ), XPT2046TempOut
              TransferData_XPT2046  0, XTouchPoint_XPT2046
              TransferData_XPT2046  0, XPT2046TempOut
          end Repeat
    
          XTouchPoint_XPT2046 = 0
          YTouchPoint_XPT2046 = 0
    
          Repeat XPT2046_ReadSamples
    
              TransferData_XPT2046  XPT2046_CFG_START | XPT2046_CFG_8BIT | XPT2046_CFG_DFR | XPT2046_MUX_X  | XPT2046_CFG_PWR, XPT2046TempOut
              wait 20 us
              TransferData_XPT2046  0,  NewYTouchPoint_XPT2046
              TransferData_XPT2046  XPT2046_CFG_START | XPT2046_CFG_8BIT | XPT2046_CFG_DFR | XPT2046_MUX_Y  | XPT2046_CFG_PWR, XPT2046TempOut
              wait 20 us
              TransferData_XPT2046  0, NewXTouchPoint_XPT2046
              TransferData_XPT2046  0, XPT2046TempOut
    
              yTouchPoint_XPT2046 = ( yTouchPoint_XPT2046 + NewyTouchPoint_XPT2046 )
              XTouchPoint_XPT2046 = ( XTouchPoint_XPT2046 + NewXTouchPoint_XPT2046 )
    
          End Repeat
    
          yTouchPoint_XPT2046 = yTouchPoint_XPT2046 /(XPT2046_ReadSamples )
          xTouchPoint_XPT2046 = xTouchPoint_XPT2046 /(XPT2046_ReadSamples )
    
          SendData_XPT2046( XPT2046_CFG_START | XPT2046_CFG_8BIT | XPT2046_CFG_DFR | XPT2046_CFG_PWR | XPT2046_MUX_Y )
    
        set XPT2046_CS ON
    
        'Set the raw values, folks may want to know this
        XTouchPoint_XPT2046raw = XTouchPoint_XPT2046
        YTouchPoint_XPT2046Raw = yTouchPoint_XPT2046
    
        'Scale to calibration
        XTouchPoint_XPT2046 = scale ( XTouchPoint_XPT2046, XPT2046_Xmin, XPT2046_Xmax, 0, Current_GLCD_WIDTH )
        YTouchPoint_XPT2046 = scale ( YTouchPoint_XPT2046, XPT2046_Ymin, XPT2046_Ymax, 0, Current_GLCD_HEIGHT )
    
    End Sub
    
     
  • Anobium

    Anobium - 2021-08-15

    Stan, it supports hardware and software. See the checks for hardware and software SPI.

     
  • Anobium

    Anobium - 2021-08-16

    @Stan

    The theory - the SPI frequency is too high. This is intended to resolve. I still may be wrong about this but only testing will isolate the issue.


    We know some things about the XPT.

    1. We know software SPI works. But, slower that hardware SPI. Insight: Software SPI is OK...therefore do not touch.
    2. We know hardware SPI works when the chip is less than 32 mHz. So, Hardware SPI in principle is good.
    3. We know hardware SPI can be set a different frequencies and if we select a slower hardware SPI frequency the XPT works but the GLCD is slow.
    4. We know that all XPT communications from the user program uses sub SendData_XPT2046( ) and this method uses the low level SPITransfer() method.
    5. We know that all GLCD communications from the user program uses a sub to send data and this method uses the low level SPITransfer() method. The same method as the XPT
    6. We know this issue is specific to the LGT chip family and when running at 32 Mhz.
    7. We know that the LGTs are chip family 122.
    8. We know to set the SPI frequency is set using the SPIMode command.
    9. We know that from an XPT library point of view that we do not know what frequency the user has selected for the chip or SPI. So, we will have to support all SPI frequency ranges.

    We can therefore conclude the correct place to slow the XPT down is on the XPT library. Specifically, the SendData_XPT2046( ) method and ONLY the hardware section of the method.

    #ifdef XPT2046_HardwareSPI
         SPITransfer  XPT2046SendByte,  XPT2046TempOut
     #endif
    

    We know we do not want to break stuff so we can adapt the method as follows. This is not tested and you will have to test. Replace the above code with the following code. This code should address the nine points above.

    This uses conditional compilation to determine whether to use the new approach.

    Please ignore the color coding.. this is something in the post.

    #ifdef XPT2046_HardwareSPI
        #if ChipFamily <> 122
           'use this for non-LGT chips 
          SPITransfer  XPT2046SendByte,  XPT2046TempOut
        #endif
    
        #if ChipFamily = 122
          'is this an LGT chip?
          #if CHIPMHZ = 32
            'is the frequency equal to 32
    
            'DEFINE a constant.  Mastet may work.. may have to be be MasterSlow
            #DEFINE  LGT_XPT_HWSPIMODE MASTER
    
            'check the currect setting
            Select Case SPICurrentMode
    
              Case MasterUltraFast
                SPIMode LGT_XPT_HWSPIMODE, 0
    
              Case MasterFast
                SPIMode LGT_XPT_HWSPIMODE, 0
    
              Case Master
                'do nothing leave as is
    
              Case MasterSlow
                'do nothing leave as is
    
            End Select
    
            SPITransfer  XPT2046SendByte,  XPT2046TempOut
    
            'restore the SPI setting
            SPIMode HWSPIMODESCRIPT, 0
    
          #endif
        #endif
         #if CHIPMHZ <> 32
           'Handle the non 32mHz chips using this method.
            SPITransfer  XPT2046SendByte,  XPT2046TempOut
          #endif
    #endif
    

    Then, do the same in the TransferData_XPT2046() method. But, ensure XPT2046OutByte is used not XPT2046TempOut in the newly added code segment.

    You may need to turn off SPI (therefore add this to the new section) to change the frequency on the fly, you therefore may need to turn off before restoring. If you get a glitching on the signals using a protocol analyser.

    You may need to change LGT_XPT_HWSPIMODE to MasterSlow. Only testing on an LGT will determine what works. So, try Master then change the constant to MasterSlow.

    You may need to change the ChipmHz test to cover 16mHz and 32mHz. You will need to test all frequencies.

    And, when you have tested the LGT. This will need testing on an UNO to make sure that still works.

    Test, adapt and test the UNO, then retest the LGT, then, post (as an attachment) the working library. Thank you.

    Enjoy....


     

    Last edit: Anobium 2021-08-16
  • stan cartwright

    stan cartwright - 2021-08-16

    Thanks Anobium. I'll sort out a test board... the kitchen is not really a workshop nor is the coffee table in the living room, so when I finish experimenting, I've got to put stuff away then can't find it :)
    I should have learnt how serial works from a "bit banging" view but the "computers" I learnt on in the early '80s didn't have serial, like sinclair or amstrad. Only the BBC micro had a rs232 port and cost the same as a second hand car!
    First thing I ever connected to a rs232 port was a picaxe because I was too mean to buy the picaxe usb solution.
    Wish I had sourced a 3.3V logic ili9341 as using two 8 channel logic level converters creates a birds nest and that's when wiring errors occur.

     
    • Anobium

      Anobium - 2021-08-16

      You need any serial or analysers if you are careful.

      Ensure you have the existing libraries GLCD and XPT working before you start. Dont start editing XPT library until you have a working baseline.

      Have fun

       
  • stan cartwright

    stan cartwright - 2021-08-16

    Thanks again. I got a cheap usb logic analyser, saetek or saelek clone for £2 odd and the real software I got free download. Maybe I'll use that.
    My scope is just a 2 channel usb hantek but might be useful... the openhantek software is better than the real hantek software.
    I might learn something.

     
    • Anobium

      Anobium - 2021-08-16

      Oh you will. :-)

       
  • stan cartwright

    stan cartwright - 2021-08-24

    Hi Anobium. This is the lgt328/ili9341/xpt touch code I am using. It doesn't work.
    It does work if you change

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

    to

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

    this using sofware spi.
    Is there a later version of this program please?

    '''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 LGT8F328P,32
        #option explicit
        #include <lgt8f328p.h>
        #include <glcd.h>
        #include <xpt2046.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_Rev
        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 100 ms
    
          end if
    
        loop
    
     
    • Anobium

      Anobium - 2021-08-24

      You need to revert the port settings as these are the hardware ports. So, you need hardware and software working om same ports. So, change the connection to the GLCD.

      No later code.

       

      Last edit: Anobium 2021-08-24
  • stan cartwright

    stan cartwright - 2021-08-25

    I haven't tried your code. https://sourceforge.net/u/evanvennn/
    May seem daft but I changed #chip LGT8F328P,32 to 16 it would be like a mega328 which does work with hwspi but the lgt didn't.
    I can see in your demo that when the screen is touched the box top right goes from red to yellow so the touch input works. I think not sharing mosi and miso with the touch pins and a bitbang solution to get the data when the screen touch event occurs. No, I can't code that.

     
  • Anobium

    Anobium - 2021-08-26

    Trying to understand what the post means. Is this what you mean ? and the state of play ?

    1. Using the stock (therefore not modified using the info in this thread) SPI library.
    2. Working at steps #3 to #8 means. The GLCD works and the XPT touch events work.
    3. You have a working mega328p solution where the ILI and the XPT work in software mode. Therefore, using software SPI
    4. You have a working mega328p solution where the ILI and the XPT work in hardware mode. Therefore, using hardware SPI
    5. The above two tests work.
    6. Then, you swapped out the mega328p for an lgt328p.
    7. Using the same software SPI source (used at step #1) you changed the program to LGTchip and frequency to 16 ... what happened?
    8. Using the same hardware SPI source (used at step #1) you changed the program to LGTchip and frequency to 16 ... what happened?

    I do not know what demo you are watching. Which one? What is the chip etc etc?


    Re bit banging... we are no where near coding. I do not understand the baseline yet. I need clarity re the tests above #3 to #8 before I can really comment on next steps.

     
  • stan cartwright

    stan cartwright - 2021-08-26

    My bad, I'll try to clarify my progress.
    under GCB@Syn/GreatCowbasic/Demos/touch_sensor_solutions there's a
    glcd_xpt_demonstration_mega328p_for_ili9341.gcb which works with swspi and hwspi.

    There's also a glcd_xpt2046_demonstration_for_lgtf328p_for_ili9341.gcb which works with swspi
    but not with hwspi. The box top right of screen changes colour but no circles drawn or numbers changing.

    You said this was because the lgt was too fast and overclocked the xpt.
    The lgt demo uses #chip LGT8F328P ie not #chip LGT8F328P ,32
    whereas #chip mega328p,16 is most common.
    So I naively thought #chip LGT8F328P,16 would make it same speed as mega328p and so not overclock the xpt but hwspi still didn't work.

    Another idea was not share TDIN and TDO ili pins with mosi and miso,
    instead use them with swspi while the screen ran using hwspi.

    I'll have to look at #include <xpt2046.h> but it's too much for me.
    You have higher priorities than this. It's not a major problem, just a niggle.</xpt2046.h>

     

    Last edit: Anobium 2021-08-26
  • Anobium

    Anobium - 2021-08-26

    GCB@Syn/GreatCowbasic/Demos/touch_sensor_solutions there's a
    glcd_xpt_demonstration_mega328p_for_ili9341.gcb which works with swspi and hwspi.

    OK

    There's also a glcd_xpt2046_demonstration_for_lgtf328p_for_ili9341.gcb which works with swspi but not with hwspi. The box top right of screen changes colour but no circles drawn or numbers changing.

    Did you make any connection changes? Therefore, you only changed the board with the chip.
    Did you try 8mHz? 16mHz? and 32 mHz? What are the results across the frequencies?

    You said this was because the lgt was too fast and overclocked the xpt.

    Yes. This is what we are trying to determine the root cause of.

    The lgt demo uses #chip LGT8F328P ie not #chip LGT8F328P ,32

    These are the same frequency. Check the report window in the IDE to ensure the frequency is 32mHz.

    whereas #chip mega328p,16 is most common.

    OK

    So I naively thought #chip LGT8F328P,16 would make it same speed as mega328p and so not overclock the xpt but hwspi still didn't work.

    Did you only change the board with the chip? I need to know that no connections changed.

    Another idea was not share TDIN and TDO ili pins with mosi and miso,
    instead use them with swspi while the screen ran using hwspi.

    We are no where near considering another approach. I do not yet understand the tests you have done. See questions above.

    I'll have to look at #include <xpt2046.h> but it's too much for me.
    You have higher priorities than this. It's not a major problem, just a niggle.</xpt2046.h>

    Do not give up.

     
  • stan cartwright

    stan cartwright - 2021-08-26

    Anobium, I can't just change the boards. The arduino and lgt are both nano boards and pin to pin the same but run at different logic levels. mega328 through logic converter... lgt straight wiring.
    I use 2 breadboards and swap the many ili9341 displays I have to check display consistency and they are interchangeable so the same.
    They mega328 wiring is not fun compared to lgt but it works, so I didn't mess up there.
    @Kent I think said choose the logic level devices for your project but what if you have 2 devices at different logic? Which gets preference?

     
    • Anobium

      Anobium - 2021-08-27

      What happens if you try to run the arduino board in the lgt setup?

       
  • stan cartwright

    stan cartwright - 2021-08-27

    @Anobiwan, "What happens if you try to run the arduino board in the lgt setup?"
    the mega uses logic shifters so you can't just swap them.

    I tried the ili9341 with just the graphic hwspi connected and got the simple glcd demo for mega328p working with lgt by just the #chip and include <lgf>.
    It works fast so OK.
    I then connected a scope to the touch IRQ line and it is high running the simple glcd demo but goes low when the screen is touched. It just does , honest.
    I'm looking at the spi solutions in demos and the mega328 version which has a good swspi code example.
    At the cost of 2 pins could the screen pressed event be used to start an on pin change interrupt to start the swspi code as a sub to read the touch data?
    Am I wasting my time with this idea? </lgf>

    ps. was lgt interrupts documented. I always copied mega328 interrupts same as pics cos they a bit hard but timercalc was handy.

     

    Last edit: stan cartwright 2021-08-27
  • stan cartwright

    stan cartwright - 2021-08-28

    I used a white led from touch IRQ to ground to show the pin behaving.
    https://www.youtube.com/watch?v=YWs3Bg6Imbw

     
    • Anobium

      Anobium - 2021-08-28

      @Anobiwan, "What happens if you try to run the arduino board in the lgt setup?"
      the mega uses logic shifters so you can't just swap them.

      Why not? the logic shifter does not work... did you try?

      I tried the ili9341 with just the graphic hwspi connected and got the simple glcd demo for mega328p working with lgt by just the #chip and include lgf It works fast so OK.

      OK. I am assuming ( again ) the frequency is 32 and the Touch is not working.

      I then connected a scope to the touch IRQ line and it is high running the simple glcd demo but goes low when the screen is touched. It just does , honest.

      I would agree. The issue with the XPT is not the event but the read operation. The event working is a good sign that things are good.

      I'm looking at the spi solutions in demos and the mega328 version which has a good swspi code example.
      At the cost of 2 pins could the screen pressed event be used to start an on pin change interrupt to start the swspi code as a sub to read the touch data?
      Am I wasting my time with this idea?

      I think so.

      ps. was lgt interrupts documented. I always copied mega328 interrupts same as pics cos they a bit hard but timercalc was handy.

      Yes. There are a few more interrupts but the main interrupts are the same across the chips.


      So, in summary. You have the LGT working in terms of GLCD writes and the XPT is raising the event.

      Now. To figure out how to change the SPI frequency so the XPT works. The easy bit.

       
    • Anobium

      Anobium - 2021-08-28

      Good to see the interrupt working

       
  • Anobium

    Anobium - 2021-08-28

    So, attached is the LGT XPT demo updated to 32 mHz. This has a new constant LGT_XPT_HWSPIMODE. This constant defines the SPI operating mode of the XPT for the LGT. This constant is optional but I have included in the demo so you play with it.

    The .h has been updated
    1. To change the SPI frequency to the frequency specified by the constant LGT_XPT_HWSPIMODE. The changes are in TransferData_XPT2046() and SendData_XPT2046(). The changes are detailed in the earlier post in this thread.
    2. To add a script section to determine is the user has set LGT_XPT_HWSPIMODE or not. If not, create a constant called LGT_XPT_HWSPIMODE set to MASTER

    Do test. You can try #define LGT_XPT_HWSPIMODE MASTERSLOW or #define LGT_XPT_HWSPIMODE MASTERFAST. Hopefully MASTERSLOW works and MASTERFAST fails as MASTERFAST will set the SPI frequency to be too fast.

    Enjoy.

     

    Last edit: Anobium 2021-08-28
  • stan cartwright

    stan cartwright - 2021-08-28

    I'll check it out asap ... but I got my ebike stolen today... and I can't walk far so the half mile home was painful. totally.. I hope the thief fucks up recharging and sets the li-ion pack alight.
    not really but I'm dissed off.
    I will check your code asap. cheers sir.

     
  • stan cartwright

    stan cartwright - 2021-08-29

    Used your code and copied the h file to gcb includes.
    Used #define LGT_XPT_HWSPIMODE MASTERSLOW
    https://youtu.be/IXt9-1WlGCc
    It shows number changing but calibration is wrong.. change portrait_rev to portrait, same.
    Getting somewhere.

     
  • Anobium

    Anobium - 2021-08-29

    looks good. calibrate one direction then the other. lookong good.

     
  • stan cartwright

    stan cartwright - 2021-08-29

    I don't know if I tested it correctly. Thanks for your work.
    Ili9341 for graphics is fast. Getting touch sorted would be icing on the cake but both working hwspi would make it a handy cheap display.
    The nextion displays work fine if you like the necessary gui needed but sending it serial commands and it's a slow display.
    The ili displays at 320 x 240 and lots of colours is a nice display and works ok with lgt328.
    The lgt speed increase shows when doing graphics. bit of trig and nice dials if you like dials.
    Try the sprite demo on a nextion. No don't.
    The pixel at x,y value on ili is great for graphic games,
    The write data I ripped from your glcd is handy.
    Ok. everything has to be universal in gcb but it's not. If there's stuff that is only sorted for one device doesn't mean it should be dismissed, it should be a bonus for using that device.
    I think your lgt work is brill. The device has it's uses that 64MHz pic and 16 MHz avr couldn't do so cool you supported it.
    I thought it would become more popular.
    Sorry there's been problems but resolvable later. the lgt is cool as is. Well done.
    Who wants to write games on a pic anyway?

     
    • Anobium

      Anobium - 2021-08-30

      Wow - that is a summary. A few questions.


      I don't know if I tested it correctly. Thanks for your work.

      Do the XPT changes work ? Where you able to calibrate ?

      Ili9341 for graphics is fast. Getting touch sorted would be icing on the cake but both working hwspi would make it a handy cheap display.

      I guess the XPT is not sorted as this sentence refers to 'icing on the cake' which would mean it is not sorted.

      The nextion displays work fine if you like the necessary gui needed but sending it serial commands and it's a slow display.

      OK

      The ili displays at 320 x 240 and lots of colours is a nice display and works ok with lgt328.
      The lgt speed increase shows when doing graphics. bit of trig and nice dials if you like dials.

      So, I think you are saying the ILI9341 GLCD works well.

      Try the sprite demo on a nextion. No don't.
      The pixel at x,y value on ili is great for graphic games,
      The write data I ripped from your glcd is handy.

      OK

      Ok. everything has to be universal in gcb but it's not. If there's stuff that is only sorted for one device doesn't mean it should be dismissed, it should be a bonus for using that device.

      Agree

      I think your lgt work is brill. The device has it's uses that 64MHz pic and 16 MHz avr couldn't do so cool you supported it.

      :-)

      I thought it would become more popular.

      Me too.

      Sorry there's been problems but resolvable later. the lgt is cool as is. Well done.

      Cheers. But, I need clarity on XPT on LGT.

      Who wants to write games on a pic anyway?

      We do!

       

      Last edit: Anobium 2021-08-30
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.