TOUCHSCREEN GLCD Drawing Program

General discussion on mikroBasic.
Author
Message
xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

TOUCHSCREEN GLCD Drawing Program

#1 Post by xor » 14 Oct 2007 05:40

Here is a video demonstration using mikroBasic and a 18F452. The code is below: I now have Touchscreen GLCD's available on http://circuit-ed.com that are compatible with the mE boards. I connected the 4-wire touchscreen tab to the EasyPIC board by soldering 4 wires of the IDC10 Flat Cable to the touchscreen tab and connecting it directly to the EasyPIC's PORTA pin connector. On the website is a complete datasheet of the touchscreen and GLCD: http://www.circuit-ed.com/128x64-GLCD-B ... 146C8.aspx.

The connection of the touchscreen is:
  • X+ = RA0/AN0
    Y+ = RA1/AN1
    X- = RA2/AN2
    Y- = RA3/AN3
Here is the mikroBasic Code:

Code: Select all

const Buttons as byte[4] = (109,127,0,8)
dim Xmin, Ymin, Ymax as word
dim Xrng, Yrng as word
dim Xold, Yold, Xloc, Yloc as byte
dim LineFlag, TouchFlag as boolean
dim outstr as string[3]

sub procedure D30
   delay_ms(30)
end sub

sub function Ypos as word
 dim tmp as word
   TRISA = (TRISA And 240) Or 4                   ' AN2 as input
   PORTA = 10                                     ' energize X plate, RA1 and RA3
   result = ADC_Read(2)                           ' get Y axis value
   If result <> 0 Then
      TRISA.2 = 0
      TRISA.0 = 1
      tmp = (1023 - ADC_Read(0))
      result = (result + tmp) >> 1
   End If

end sub

sub function Xpos as word
 dim tmp as word
   TRISA = (TRISA And 240) Or 2                   ' AN1 as input
   PORTA = 5                                      ' energize Y plate, RA0 and RA2
   result = ADC_Read(1)                           ' get Y axis value
   If result <> 0 Then
      TRISA.1 = 0
      TRISA.3 = 1
      tmp = (1023 - ADC_Read(3))
      result = (result + tmp) >> 1
   End If
end sub

sub function Touch as boolean                     ' is the screen touched?
 dim tmp, ty, tx as word
   ty = Ypos                                      ' get Y
   If ty > 0 Then
      D30                                         ' debounce
      ty = Ypos                                   ' get Y
      tx = Xpos                                   ' get X
      If ty > Ymin Then                           ' test the range
         If ty < Ymax Then
            result = True
            tmp = (tx - Xmin) << 7                ' calculate GLCD X and Y Values
            Xloc = word(tmp div Xrng)             '
            If Xloc > 127 Then Xloc = 127 End If  '
            tmp = (ty - Ymin) << 6                '
            Yloc = word(tmp div Yrng)             '
            If Yloc > 63 Then Yloc = 63 End If    '
         End If
      End If
   End If
end sub

sub procedure CalibrateScreen
 dim inv as byte
   inv = 1                                         ' invert character flag
   GLCD_FILL(255)                                  ' fill screen
   While Xpos = 0                                  ' wait for touch
      GLCD_WRITE_CHAR(32,0,0,inv)
      delay_ms(300)
      inv = inv xor 1
   Wend
   D30
   Ymin = Ypos                                     ' save minimum values
   Xmin = Xpos
   GLCD_WRITE_CHAR(32,0,0,0)
   While Xpos > 0                                  ' wait for release
   Wend
   D30
   While Xpos = 0                                  ' wait for touch
      GLCD_WRITE_CHAR(32,122,7,inv)
      delay_ms(300)
      inv = inv xor 1
   Wend
   D30
   Ymax = Ypos                                     ' save max and range values
   Yrng = Ymax - Ymin
   Xrng = Xpos - Xmin
   While Xpos > 0
   Wend
   GLCD_FILL(0)
end sub

sub procedure PrintXYVal
 dim tmp as byte
   If Touch = True Then
      Touchflag = True
      ByteToStr(Xloc,outstr)
      GLCD_WRITE_TEXT(outstr,14,6,1)
      ByteToStr(Yloc,outstr)
      GLCD_WRITE_TEXT(outstr,14,7,1)
   Else
      Touchflag = False
      GLCD_WRITE_TEXT("   ",14,6,1)
      GLCD_WRITE_TEXT("   ",14,7,1)
   End If
   GLCD_WRITE_TEXT("X=",0,6,1)
   GLCD_WRITE_TEXT("Y=",0,7,1)
end sub

sub procedure PrintLine
 dim tmp as word
   If LineFlag = False Then
      GLCD_Dot(Xloc, Yloc, 1)
      LineFlag = True
   Else
      GLCD_Line(Xold, Yold, Xloc, Yloc, 1)
   End If
   Xold = Xloc
   Yold = Yloc
end sub

sub procedure RangeTest                            ' check range for button press
   If (Xloc > Buttons[0]) And (Xloc < Buttons[1]) Then
      If (Yloc > Buttons[2]) And (Yloc < Buttons[3]) Then
         GLCD_FILL(0)
         GLCD_WRITE_TEXT("CLR",109,0,0)
         LineFlag = False
         TouchFlag = False
         Xloc = 0
         Yloc = 0
      End If
   End If
end sub

sub procedure Initialize
   ADCON1 = 2
   TRISA = 0
   PORTA = 0
   LineFlag = False
   Glcd_Init(PORTB, 0, 1, 2, 3, 5, 4, PORTD)
   Glcd_Set_Font(@Font5x7, 5, 8, 32)
end sub

main:

   Initialize
   CalibrateScreen
   GLCD_WRITE_TEXT("CLR",109,0,0)
   
   While true
      PrintXYVal                                   ' print GLCD X and Y values
      If TouchFlag = True Then
        PrintLine                                  ' if screen touched print lines
        RangeTest                                  ' check if button was pressed
      Else
        LineFlag = TouchFlag
      End If
   Wend
   
end.
Last edited by xor on 21 Oct 2007 23:10, edited 1 time in total.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Philtkp
Posts: 307
Joined: 26 Apr 2006 00:58
Location: Tucson Arizona

#2 Post by Philtkp » 14 Oct 2007 06:53

Xor,
That is to cool. can't wait to check it out. I've been working on the keypad project I told you about. Is there a way to place text on the screen using the the Y coordinate? I would like to move it down a few pixels. Here's a link to picture

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#3 Post by xor » 14 Oct 2007 13:22

Nice graphics on the keypad. I am just learning about the GLCD library myself. So if I figure out how to place the character better I'll let you know.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Philtkp
Posts: 307
Joined: 26 Apr 2006 00:58
Location: Tucson Arizona

#4 Post by Philtkp » 15 Oct 2007 04:55

Nice graphics on the keypad
Thanks, finally found a good use for MS Paint.

Just gave your project a test run. This is sweet!

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#5 Post by xor » 16 Oct 2007 01:59

Thought I would add a couple pictures of my connections for the above code. Also, make sure that PORTA pull-down resistors are in place on the board.
  • Image
    Image
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Propaganda
Posts: 177
Joined: 14 Jan 2007 02:30
Location: VA, USA

#6 Post by Propaganda » 09 Feb 2008 08:47

Hi

Great project.

If you lightly touch the screen do you get "ghosting"? I use that term to refer to false location positives which have the program place dots on such false locations.

I cant compile the above code for Mbasic due to not having a license but
I do have a rough interpertation in MikroC and I get ghosting.
"The whole world must learn of our peaceful ways, by force!" - Bender Bending Rodríguez

Copy'nPaste
Posts: 573
Joined: 25 Apr 2006 15:39
Location: Cape Town, South Africa

#7 Post by Copy'nPaste » 10 Feb 2008 07:43

Hi XOR,
So one doesn't really need the interface circuit as is on the EasyPic 5 board :?
I see the fitted screens/glcd you supply have the ribbon on the opposite side to where the EP 5 requires it.
Edit 01:
I see now your touchscreen is somewhat different to the one supplied by mW in layout too.
I assume they work the same though :?
I'm really surprised by the resolution offered 8)
"Copy'nPaste"

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#8 Post by xor » 10 Feb 2008 17:22

Propaganda wrote:If you lightly touch the screen do you get "ghosting"? I use that term to refer to false location positives which have the program place dots on such false locations.
No, I do not have any effects the same as you described. Of course, the idea is to press firmly to get the correct reading from the screen. There are differences in readings depending on using a finger as opposed to a blunt pointed instrument. But I have mostly resolved that in my new touchscreen code methods.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#9 Post by LGR » 10 Feb 2008 17:41

Ghosting can happen if the contrast is set too high. Try backing off on the contrast.
If you know what you're doing, you're not learning anything.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#10 Post by xor » 10 Feb 2008 17:42

Copy'nPaste wrote:Hi XOR,
So one doesn't really need the interface circuit as is on the EasyPic 5 board :?
I see the fitted screens/glcd you supply have the ribbon on the opposite side to where the EP 5 requires it.
Edit 01:
I see now your touchscreen is somewhat different to the one supplied by mW in layout too.
I assume they work the same though :?
I'm really surprised by the resolution offered 8)
All 4-wire touchpanels work the same way. For the sake of saying it, you do not need a controller. You can connect the touchscreen directly to the PIC without ado. I have now developed 2 new toupanel reading routines that are lightning fast and accurate, whether using a finger or stylus. Please note that my code for the above project was a bit of a learning/experimenting stage of my touchscreen code development. It works fine but can be greatly improved.

Sorry, I will not publish my new touchscreen code. I have received many requests for more information and now realize that some of these requests are from companies and engineers are who seeking commercial advantage for free, after I have already put a lot of effort and money into expanding this part of my business.

Here is a new product to make it easier to connect ANY 4-panel touchpanel to ANY development board:

Image
Image
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Propaganda
Posts: 177
Joined: 14 Jan 2007 02:30
Location: VA, USA

#11 Post by Propaganda » 10 Feb 2008 21:33

Thanks for the replies.
"The whole world must learn of our peaceful ways, by force!" - Bender Bending Rodríguez

Csaba
Posts: 238
Joined: 01 Jun 2007 16:25
Location: Switzerland/Spain
Contact:

This is a great Code

#12 Post by Csaba » 11 Feb 2008 22:49

Thanks so much XOR, this is really a great code.

If anybody cares the connector is a 84984-4:Connector, FPC 1.00mm 4WAY
Article Number 124-5255 from Farnell.

:)

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#13 Post by xor » 11 Feb 2008 23:42

We provide a free FPC SMD Connector with each TS GLCD. The EZ-FPC is separate.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#14 Post by rmteo » 11 Feb 2008 23:58

Xor, I received the EZ-FPC and TS on Saturday, thank you. Installed it on the GLCD of my EP4 and got it working fine. I modified your code to store the calibration constants in EEPROM, so that it would not need to be calibrated each time. Works great!!! :lol:

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#15 Post by xor » 12 Feb 2008 04:36

Yes, I have done the same by storing those values in EEPROM. You may want to make it an optional feature in the future, possibly by letting the user simply touch the screen for 3-5 seconds anywhere and then the calibration screen pops up. It will give your programs a lot of flexibility depending on environmental conditions which might affect initial calibration, although I have tested mine in many conditions and have seen very little ill effects of excessive and variable temperature on the screens, and I have tested mine sitting right over a steam radiator over many days.

That new connector makes a world of difference in speeding development and experimenting with touchpanel code. I think it's a must for a serious developer.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Locked

Return to “mikroBasic General”