Touch Screen Library

Aus ProjectWiki
Wechseln zu:Navigation, Suche

Overview

Based on the Appnote AVR341 - Four and five wire Resistive Touch Screen Controller by Atmel, this library handles everything needed to use a 4-wire resistive touchscreen in Bascom.

Features:

  • Interrupt-controlled sampling
  • Touch contact pressure measurement
  • Adjustable sampling rate
  • Screen coordinate calibration (stored in EEPROM)
  • Screen rotation
  • Click/Drag detection
  • AtXMega, AtMega (untested)

How to use

Options

' width of the underlying screen [pixels]
   Const Touch_screen_width = 240

' height of the underlying screen [pixels]
   Const Touch_screen_height = 320

' if the display supports rotation, then the touch coordinates need to be rotated too
   Const Touch_enable_rotation = False

' measure the touch contact resistance [False: Off, 1: Method 1, 2: Method 2]
   Touch_read_resistance = False

' if contact resistance measurement is enabled, the touch ITO layer resistances need to be known [ohms]
   Const Touch_resistance_x = 200
   Const Touch_resistance_y = 600

' control port for touch screen
   Touch_ctrl_port Alias Porta

' control port pin connections
   Const Touch_pin_xp = 0
   Const Touch_pin_xn = 1
   Const Touch_pin_yp = 2
   Const Touch_pin_yn = 3

' adc port pin connections
   Const Touch_pin_xn_adc = 4
   Const Touch_pin_yn_adc = 5
   Const Touch_pin_yp_adc = 6

' measurements per sample
   Const Touch_samples = 5

' new coordinate samples [coordinates/sec]
   Const Touch_updaterate = 100

' no. of samples to be sure the touch screen has been released
   Const Touch_release_count = 5

' they bounce badly [sec]
   Const Touch_time_debounce = 0.05

' depends on RC time constant of touch screen, 0.2ms should work for most [sec]
   Const Touch_time_switch_measurement = 0.0002

' click/drag detection delta [pixels]
   Const Touch_click_delta = 15

' max speed [pixels/sec]
   Const Touch_max_delta = 3000


API

Status Flags:

Dim Touch_newsample As Byte
Dim Touch_calibrated As Bit
Dim Touch_touched As Bit
Dim Touch_clicked As Byte
Dim Touch_dragging As Bit

Coordinates:

Dim Touch_x As Word , Touch_y As Word
#if Touch_measure_resistance >= 1
   Dim Touch_r As Word
#endif

Display rotation:

' Rotation = 0: Portrait
'          = 1: Landscape
'          = 2: Portrait 180°
'          = 3: Landscape 180°
Dim Touch_rotation As Byte

Sub routines:

Sub Touch_init()
Sub Touch_calibrate()
Sub Touch_load_calibration()
Sub Touch_calculate_calibration()
Sub Touch_reset_calibration()
Function Touch_getsample() As Byte


Other Display Types

Change used Timer-, ADC-Hardware-Module

Sample

Download