Here is an other Touchscreen module

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
NielsNL
Posts: 18
Joined: Tue Aug 05, 2008 6:40 pm

Here is an other Touchscreen module

Post by NielsNL » Thu Aug 07, 2008 6:44 am

Hallo

This module is based on the new easyPic5 board from http://www.mikroe.com/en/ .

It is very easy to use i think.

Code: Select all

{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : Np. v/d Spek                                                   *
*  Notice  : Copyright (c) 2008 LumenSoft Int.                              *
*          : All Rights Reserved                                            *
*  Date    : 6-8-2008                                                       *
*  Version : 1.0                                                            *
*  Notes   : This libery works with the touch interfase from the easypic5   *
*          : board.                                                         *
*****************************************************************************
}
Module EasyTouch

//******************************************************************* 
//*                              
//******************************************************************* 

Include "GLCD.bas" 
Include "adc.bas" 
Include "convert.bas" 
Include "minADtime.bas"
Include "eeprom.bas"

Const
   ADC_THRESHOLD = 900
   
Dim 
  Xmin, Xmax, Ymin, Ymax As Word 

//******************************************************************* 
//*                              
//******************************************************************* 

Public Function isTouched() As Boolean              // test for active screen & get location 
  PORTC.0 = 0         // DRIVEA = 0 (LEFT drive off, RIGHT drive off, TOP drive on)
  PORTC.1 = 0         // DRIVEB = 0 (BOTTOM drive off)
  DelayUS(3500)                              // debounce delay 
  result = (ADC.Read(1) > ADC_THRESHOLD)                      // if logical one is detectet
  //debouncing, repeat detecting after 2ms
  DelayMS(2)
  result = result And (ADC.Read(1) > ADC_THRESHOLD)
End Function    
  
//******************************************************************* 
//*                              
//******************************************************************* 

Sub mydot(x, y, z As Byte)
  Dim oldcolor As Byte
  oldcolor = GLCD.Pen.color
  If z = 0 Then
    GLCD.Pen.Color = 0
  ElseIf z= 1 Then
    GLCD.Pen.Color = 1
  Else
    GLCD.Pen.Color = Not GLCD.GetPixel(x,y)
  EndIf
  GLCD.Rectangle(x, y, x + 1, y + 1)
  GLCD.Pen.Color = oldcolor
End Sub


Sub BlinkCursor(x,y As Byte)
  Dim wait As Byte
  wait = 0
  mydot(x,y,1)
  While isTouched() = false
    wait = wait +1
    DelayMS(30)
    If (wait = 16) Then 
      mydot(x,y,2)
      wait = 0
    End If
  Wend
  mydot(x,y,0)
End Sub
  
//******************************************************************* 
//*                              
//******************************************************************* 

Function Xpos() As Word                  // get X position 
  PORTC.0 = 1         // DRIVEA = 1 (LEFT drive on, RIGHT drive on, TOP drive off)
  PORTC.1 = 0         // DRIVEB = 0 (BOTTOM drive off)

  DelayMS(5)
  result = ADC.Read(0) // reading X value from RA0 (BOTTOM)
End Function 
 

Public Function XTouched() As LongInt
   result = LongInt((Xpos() - Xmin) * 128) / (Xmax - LongInt(Xmin))
   If result <  0 Then result =   0 End If
   If result >127 Then result = 127 End If
End Function

//******************************************************************* 
//*                              
//******************************************************************* 

Function Ypos() As Word                  // get Y position 
  PORTC.0 = 0         // DRIVEA = 0 (LEFT drive off, RIGHT drive off, TOP drive on)
  PORTC.1 = 1         // DRIVEB = 1 (BOTTOM drive on)

  DelayMS(5)
  result = ADC.Read(1) // reading Y value from RA1 (from LEFT)
End Function 

Public Function YTouched() As LongInt
  result = (((Ypos() - Ymin) *64) / (Ymax - LongInt(Ymin)))
  If result < 0 Then result =  0 End If
  If result >63 Then result = 63 End If
  result = 63 - result
End Function
  
//******************************************************************* 
//*                              
//******************************************************************* 

Public Sub Calibrate()
  GLCD.Cls
  GLCD.WriteAt(30, 15,"CALIBRATION")
  GLCD.WriteAt(15, 35,"Touch Bottom Left")
  // get calibration constants (reading and compensating TouchPanel nonlinearity)
  BlinkCursor(0,62)

  Xmin = Word(Xpos - 10)
  Ymin = Word(Ypos - 10)

  GLCD.WriteAt(15, 35,"                 ")
  While isTouched() Wend

  GLCD.WriteAt(15, 35,"Touch Top Right")
  BlinkCursor(126,0)
  // get calibration constants (reading and compensating TouchPanel nonlinearity)
  Xmax = Xpos() + 5
  Ymax = Ypos() + 5
  While isTouched() Wend
  GLCD.Cls
  EE.Write(0, Xmin, Ymin, Xmax, Ymax)
End Sub

//******************************************************************* 
//*                              
//******************************************************************* 

Public Function Touch_init() As Boolean
  ADCON1 = $02
  PORTA  = $00
  TRISA  = $03                               // RA0 i RA1 are analog inputs
  PORTC  = 0
  TRISC  = 0

  EE.Read(0, Xmin, Ymin, Xmax, Ymax)
  result = (Xmin <> $ffff)
End Function
Have fun with it.
Last edited by NielsNL on Thu Aug 07, 2008 8:30 am, edited 1 time in total.

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Thu Aug 07, 2008 7:52 am

That looks really helpful - thanks for sharing. I do not have the EasyPIC touchscreen interface, but a lot of users will.

In the sub mydot, you save and restore GLCD.Pen.Mode, but do not alter it in the sub - is it supposed to be GLCD.Pen.Color or have I made a mistake?

You might also think about posting the module on the wiki.

Kind regards,

Steve

NielsNL
Posts: 18
Joined: Tue Aug 05, 2008 6:40 pm

Post by NielsNL » Thu Aug 07, 2008 8:29 am

good point about saving an restoring GLCD.Pen.Mode. it is a left over solution from a older effort to blink to pixel.

i have removed it now

Post Reply