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