Use a touch panel
A touch panel is a thin, self-adhesive transparent panel placed over the screen of a graphic LCD. It is very sensitive to pressure so that even a soft touch causes some changes on the output signal. There are a few types of touch panel. The simplest one is a resistive touch panel. It consists of two transparent rigid foils, forming a ‘sandwich’ structure, that have resistive layers on their inner sides. The resistance of these layers usually does not exceed 1K. The opposite sides of these foils have contacts available for use via a flat cable. The process of determining coordinates of the point in which the touch panel is pressed can be broken into two steps. The first one is the determination of the X coordinate and the second one is the determination of the Y coordinate of the point. In order to determine the X coordinate, it is necessary to connect the left contact on the surface A to ground and the right contact to the power supply. This enables a voltage divider to be formed by pressing the touch panel. The value of the divider is read on the bottom contact of the surface B. Voltage can go within the range of 0V to the power supply (5V) and depends on the X coordinate. If the point is closer to the left contact of the surface A, the voltage will be closer to 0V. In order to determine the Y coordinate, it is necessary to connect the bottom contact on the surface B to ground, and the upper contact to the power supply. In this case, the voltage is read on the left contact of the surface A. In order to connect a touch panel to the microcontroller it is necessary to provide a circuit for touch panel control. By means of this circuit, the microcontroller connects appropriate contacts of the touch panel to the ground and the power supply (as described above) so as to determine coordinates X and Y. The bottom contact of the surface B and left contact of the surface A are connected to the microcontroller’s A/D converter. The X and Y coordinates are determined by measuring voltage on these contacts, respectively. The related program outlines a menu on the graphic LCD, turns the circuit for touch panel control on/off (driving touch panel) and reads results of A/D conversion which actually represent the X and Y coordinates of the point. On the basis of these coordinates it is possible to decide what you want the microcontroller to do. In this example, the microcontroller turns on/off two digital pins connected to LEDs A and B. Functions belonging to the Glcd, Glcd_Fonts and ADC librares are used in this example. As the touch panel surface is slightly larger than the surface of the graphic LCD, it is necessary to perform the software calibration of the touch panel in order to provide greater accuracy when determining the coordinates'Header****************************************************** program example_15 ' Name of program dim GLCD_DataPORT as byte at PORTD ' GLCD module connections dim GLCD_CS1 as sbit at RB0_bit GLCD_CS2 as sbit at RB1_bit GLCD_RS as sbit at RB2_bit GLCD_RW as sbit at RB3_bit GLCD_EN as sbit at RB4_bit GLCD_RST as sbit at RB5_bit dim GLCD_CS1_Direction as sbit at TRISB0_bit GLCD_CS2_Direction as sbit at TRISB1_bit GLCD_RS_Direction as sbit at TRISB2_bit GLCD_RW_Direction as sbit at TRISB3_bit GLCD_EN_Direction as sbit at TRISB4_bit GLCD_RST_Direction as sbit at TRISB5_bit ' End Glcd module connections dim x_coord, y_coord, x_coord128, y_coord64 as longint ' Scaled x-y position sub function GetX() as word ' Reading X PORTC.0 = 1 ' DRIVEA = 1 (LEFT drive on, RIGHT drive on, TOP drive off) PORTC.1 = 0 ' DRIVEB = 0 (BOTTOM drive off) Delay_ms(5) result = ADC_Read(0) ' READ-X (BOTTOM) end sub sub function GetY() as word ' Reading Y PORTC.0 = 0 ' DRIVEA = 0 (LEFT drive off, RIGHT drive off, TOP drive on) PORTC.1 = 1 ' DRIVEB = 1 (BOTTOM drive on) Delay_ms(5) result = ADC_Read(1) ' READ-X (LEFT) end sub main: ' Start of program PORTA = 0x00 TRISA = 0x03 ' RA0 i RA1 are analog inputs ANSEL = 0x03 ANSELH = 0 ' Configure other analog pins as digital I/O PORTC = 0 TRISC = 0 ' PORTC pins are configured as outputs Glcd_Init() ' Glcd_Init_EP5 Glcd_Set_Font(@font5x7, 5, 7, 32) ' Choose font size 5x7 Glcd_Fill(0) ' Clear GLCD Glcd_Write_Text("TOUCHPANEL EXAMPLE",10,0,1) Glcd_Write_Text("MIKROELEKTRONIKA",17,7,1) Glcd_Rectangle(8,16,60,48,1) ' Outline two ‘buttons’ on GLCD: Glcd_Rectangle(68,16,120,48,1) Glcd_Box(10,18,58,46,1) Glcd_Box(70,18,118,46,1) Glcd_Write_Text("BUTTON1",14,3,0) Glcd_Write_Text("RC6 OFF",14,4,0) Glcd_Write_Text("BUTTON2",74,3,0) Glcd_Write_Text("RC7 OFF",74,4,0) while TRUE ' Read X-Y and convert it to 128x64 space x_coord = GetX() y_coord = GetY() x_coord128 = (x_coord * 128) / 1024 y_coord64 = 64 -((y_coord *64) / 1024) ' If BUTTON1 is selected: if ((x_coord128 >= 10) and (x_coord128 <= 58) and (y_coord64 >= 18) and (y_coord64 <= 46)) then if(PORTC.6 = 0) then PORTC.6 = 1 Glcd_Write_Text("RC6 ON ",14,4,0) else PORTC.6 = 0 Glcd_Write_Text("RC6 OFF",14,4,0) end if end if ' If BUTTON2 is selected: if ((x_coord128 >= 70) and (x_coord128 <= 118) and (y_coord64 >= 18) and (y_coord64 <= 46)) then if(PORTC.7 = 0) then PORTC.7 = 1 Glcd_Write_Text("RC7 ON ",74,4,0) else PORTC.7 = 0 Glcd_Write_Text("RC7 OFF",74,4,0) end if end if Delay_ms(100) wend ' While true end. ' End of programIn order to make this example work properly, it is necessary to check the following libraries in the Library Manager prior to compiling: