Touch Panel Library

The mikroPascal PRO for dsPIC30/33 and PIC24 provides a library for working with Touch Panel.

Library Dependency Tree

Touch Panel Library Dependency Tree

External dependencies of Touch Panel Library

The following variables must be defined in all projects using Touch Panel Library: Description : Example :
var DriveA : sbit; sfr; external; DriveA line. var DriveA : sbit at LATC13_bit;
var DriveB : sbit; sfr; external; DriveB line. var DriveB : sbit at LATC14_bit;
var DriveA_Direction : sbit; sfr; external; Direction of the DriveA pin. var DriveA_Direction : sbit at TRISC13_bit;
var DriveB_Direction : sbit; sfr; external; Direction of the DriveB pin. var DriveB_Direction : sbit at TRISC14_bit;

Library Routines

TP_Init

Prototype

procedure TP_Init(display_width : word; display_height : word; readX_ChNo : byte; readY_ChNo : byte);

Description

Initialize touch panel display. Default touch panel ADC threshold value is set to 3900.

Parameters
  • display_width: set display width.
  • display_height: set display height.
  • readX_ChNo: read X coordinate from desired ADC channel.
  • readY_ChNo: read Y coordinate from desired ADC channel.
Returns

Nothing.

Requires

Before calling this function initialize ADC module.

Example
ADC1_Init();               // Initalize ADC module
TP_Init(128, 64, 6, 7);    // Initialize touch panel, dimensions 128x64
Notes

None.

TP_Set_ADC_Threshold

Prototype

procedure TP_Set_ADC_Threshold(threshold : word);

Description

Set custom ADC threshold value, call this function after TP_Init.

Parameters
  • threshold: custom ADC threshold value.
Returns

Nothing.

Requires

TP_Init has to be called before using this routine.

Example
TP_Set_ADC_Threshold(3900);    // Set touch panel ADC threshold
Notes

None.

TP_Press_Detect

Prototype

function TP_Press_Detect() : byte;

Description

Detects if the touch panel has been pressed.

Parameters

None.

Returns
  • 1 - if touch panel is pressed.
  • 0 - otherwise.
Requires

Global variables :

  • DriveA: DriveA.
  • DriveB: DriveB.
  • DriveA_Direction: Direction of DriveA pin.
  • DriveB_Direction: Direction of DriveB pin.
must be defined before using this function.

Example
// Touch Panel module connections
var DriveA : sbit at LATC13_bit;
    DriveB : sbit at LATC14_bit;
    DriveA_Direction : sbit at TRISC13_bit;
    DriveB_Direction : sbit at TRISC14_bit;
// End Touch Panel module connections

if (TP_Press_Detect() <> 0) then
  begin
  ...
  end;
Notes

None.

TP_Get_Coordinates

Prototype

function TP_Get_Coordinates(x_coordinate : ^word; y_coordinate : ^word) : byte;

Description

Get touch panel coordinates and store them in x_coordinate and y_coordinate parameters.

Parameters
  • x_coordinate: x coordinate of the place of touch.
  • y_coordinate: y coordinate of the place of touch.
Returns
  • 0 - if reading is within display dimension range.
  • 1 - if reading is out of display dimension range.
Requires

Nothing.

Example
if (TP_Get_Coordinates(@x_coord, @y_coord) = 0) then
  begin
  ...
  end;
Notes

None.

TP_Calibrate_Bottom_Left

Prototype

procedure TP_Calibrate_Bottom_Left();

Description

Calibrate bottom left corner of the touch Panel.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
TP_Calibrate_Bottom_Left();    // Calibration of bottom left corner
Notes

None.

TP_Calibrate_Upper_Right

Prototype

procedure TP_Calibrate_Upper_Right();

Description

Calibrate upper right corner of the touch Panel.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
TP_Calibrate_Upper_Right();    // Calibration of upper right corner
Notes

None.

TP_Get_Calibration_Consts

Prototype

procedure TP_Get_Calibration_Consts(x_min : ^word; x_max : ^word; y_min : ^word; y_max : ^word);

Description

Gets calibration constants after calibration is done and stores them in x_min, x_max, y_min and y_max parameters.

Parameters
  • x_min: x coordinate of the bottom left corner of the working area.
  • x_max: x coordinate of the upper right corner of the working area.
  • y_min: y coordinate of the bottom left corner of the working area.
  • y_max: y coordinate of the upper right corner of the working area.
Returns

Nothing.

Requires

Nothing.

Example
TP_Get_Calibration_Consts(@x_min, @y_min, @x_max, @y_max);    // Get calibration constants
Notes

None.

TP_Set_Calibration_Consts

Prototype

procedure TP_Set_Calibration_Consts(x_min : word; x_max : word; y_min : word; y_max : word);

Description

Sets calibration constants.

Parameters
  • x_min: x coordinate of the bottom left corner of the working area.
  • x_max: x coordinate of the upper right corner of the working area.
  • y_min: y coordinate of the bottom left corner of the working area.
  • y_max: y coordinate of the upper right corner of the working area.
Returns

Nothing.

Requires

Nothing.

Example
TP_Set_Calibration_Consts(148, 3590, 519, 3370);    // Set calibration constants
Notes

None.

Library Example

The following drawing demo tests routines of the Touch Panel library :

Copy Code To ClipboardCopy Code To Clipboard
program TouchPanelCalibrationAndWrite;

// Glcd module connections
var GLCD_D7 : sbit at RD3_bit;
    GLCD_D6 : sbit at RD2_bit;
    GLCD_D5 : sbit at RD1_bit;
    GLCD_D4 : sbit at RD0_bit;
    GLCD_D3 : sbit at RB3_bit;
    GLCD_D2 : sbit at RB2_bit;
    GLCD_D1 : sbit at RB1_bit;
    GLCD_D0 : sbit at RB0_bit;
    GLCD_D7_Direction : sbit at TRISD3_bit;
    GLCD_D6_Direction : sbit at TRISD2_bit;
    GLCD_D5_Direction : sbit at TRISD1_bit;
    GLCD_D4_Direction : sbit at TRISD0_bit;
    GLCD_D3_Direction : sbit at TRISB3_bit;
    GLCD_D2_Direction : sbit at TRISB2_bit;
    GLCD_D1_Direction : sbit at TRISB1_bit;
    GLCD_D0_Direction : sbit at TRISB0_bit;

var GLCD_CS1 : sbit at LATB4_bit;
    GLCD_CS2 : sbit at LATB5_bit;
    GLCD_RS  : sbit at LATF0_bit;
    GLCD_RW  : sbit at LATF1_bit;
    GLCD_EN  : sbit at LATF4_bit;
    GLCD_RST : sbit at LATF5_bit;

var GLCD_CS1_Direction : sbit at TRISB4_bit;
    GLCD_CS2_Direction : sbit at TRISB5_bit;
    GLCD_RS_Direction  : sbit at TRISF0_bit;
    GLCD_RW_Direction  : sbit at TRISF1_bit;
    GLCD_EN_Direction  : sbit at TRISF4_bit;
    GLCD_RST_Direction : sbit at TRISF5_bit;
// End Glcd module connections

// Touch Panel module connections
var DriveA : sbit at LATC13_bit;
    DriveB : sbit at LATC14_bit;
    DriveA_Direction : sbit at TRISC13_bit;
    DriveB_Direction : sbit at TRISC14_bit;
// end Touch Panel module connections

var write_erase : bit;
    pen_size : byte;
    x_coord, y_coord : word;
    write_msg, clear_msg, erase_msg : array[5] of char;      // GLCD menu messages

procedure Initialize();
  begin
    ADPCFG := 0xFF3F;                                        // set AN6 and AN7 channel pins as analog

    DriveA_Direction := 0;                                   // Set DriveA pin as output
    DriveB_Direction := 0;                                   // Set DriveB pin as output

    Glcd_Init();                                             // Initialize GLCD
    Glcd_Fill(0);                                            // Clear GLCD

    ADC1_Init();                                             // Initialize ADC
    TP_Init(128, 64, 6, 7);                                  // Initialize touch panel
    TP_Set_ADC_Threshold(3900);                              // Set touch panel ADC threshold
  end;

procedure Calibrate();
  begin
    Glcd_Dot(0,63,1);                                        // Draw bottom left dot
    Glcd_Write_Text('TOUCH BOTTOM LEFT',12,3,1);
    TP_Calibrate_Bottom_Left();                              // Calibration of bottom left corner
    Delay_ms(1000);

    Glcd_Dot(0,63,0);                                        // Clear bottom left dot
    Glcd_Dot(127,0,1);                                       // Draw upper right dot
    Glcd_Write_Text('                 ',12,3,1);
    Glcd_Write_Text('TOUCH UPPER RIGHT',12,4,1);
    TP_Calibrate_Upper_Right();                              // Calibration of upper right corner
    Delay_ms(1000);
  end;

begin
  write_msg := 'WRITE';
  clear_msg := 'CLEAR';
  erase_msg := 'ERASE';

  Initialize();

  Glcd_Fill(0);                                              // Clear GLCD
  Glcd_Write_Text('CALIBRATION',12,3,1);
  Delay_ms(1000);
  Glcd_Fill(0);                                              // Clear GLCD
  Calibrate();
  Glcd_Fill(0);

  Glcd_Write_Text('WRITE ON SCREEN', 20, 5, 1) ;
  Delay_ms(1000);
  Glcd_Fill(0);

  Glcd_V_Line(0,7,0,1);
  Glcd_Write_Text(clear_msg,1,0,0);
  Glcd_V_Line(0,7,97,1);
  Glcd_Write_Text(erase_msg,98,0,0);

  // Pen Menu:
  Glcd_Rectangle(41,0,52,9,1);
  Glcd_Box(45,3,48,6,1);
  Glcd_Rectangle(63,0,70,7,1);
  Glcd_Box(66,3,67,4,1);
  Glcd_Rectangle(80,0,86,6,1);
  Glcd_Dot(83,3,1);

  write_erase := 1;
  pen_size := 1;

  while (TRUE) do
    begin

      if (TP_Press_Detect() <> 0) then
        begin
          // After a PRESS is detected read X-Y and convert it to 128x64 space
          if (TP_Get_Coordinates(@x_coord, @y_coord) = 0) then
            begin
              if ((x_coord < 31) and (y_coord < 8)) then
                begin
                  Glcd_Fill(0);

                  // Pen Menu:
                  Glcd_Rectangle(41,0,52,9,1);
                  Glcd_Box(45,3,48,6,1);
                  Glcd_Rectangle(63,0,70,7,1);
                  Glcd_Box(66,3,67,4,1);
                  Glcd_Rectangle(80,0,86,6,1);
                  Glcd_Dot(83,3,1);

                  Glcd_V_Line(0,7,0,1);
                  Glcd_Write_Text(clear_msg,1,0,0);
                  Glcd_V_Line(0,7,97,1);
                  if (write_erase) then
                    Glcd_Write_Text(erase_msg,98,0,0)
                  else
                    Glcd_Write_Text(write_msg,98,0,0);
                end;

              // If write/erase is pressed
              if ((x_coord > 96) and (y_coord < 8)) then
                begin
                  if (write_erase) then
                    begin
                      write_erase := 0;
                      Glcd_Write_Text(write_msg,98,0,0);
                      Delay_ms(500);
                    end
                  else
                    begin
                      write_erase := 1;
                      Glcd_Write_Text(erase_msg,98,0,0);
                      Delay_ms(500);
                    end;
                end;

              // If pen size is selected
              if ((x_coord >= 41) and (x_coord <= 52) and (y_coord <= 9)) then
                pen_size := 3;

              if ((x_coord >= 63) and (x_coord <= 70) and (y_coord <= 7)) then
                pen_size := 2;

              if ((x_coord >= 80) and (x_coord <= 86) and (y_coord <= 6)) then
                pen_size := 1;

              if (y_coord < 11) then
                continue;

              case pen_size of
                1: if ( (x_coord >= 0) and (y_coord >= 0) and (x_coord <= 127) and (y_coord <= 63) ) then
                     Glcd_Dot(x_coord, y_coord, write_erase);

                2: if ( (x_coord >= 0) and (y_coord >= 0) and (x_coord <= 127-1) and (y_coord <= 63-1) ) then
                     Glcd_Box(x_coord, y_coord, x_coord + 1, y_coord + 1, write_erase);


                3: if ( (x_coord >= 1) and (y_coord >= 1) and (x_coord <= 127-2) and (y_coord <= 63-2) ) then
                     Glcd_Box(x_coord-1, y_coord-1, x_coord + 2, y_coord + 2, write_erase);

              end;
            end;
        end;
    end;
end.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code