The connection of the touchscreen is:
- X+ = RA0/AN0
Y+ = RA1/AN1
X- = RA2/AN2
Y- = RA3/AN3
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.