4.17 Example 15
Using 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 output signal. There are a few types of touch panel. The simplest one is the 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 the foils have contacts available for use through a flat cable.
The process of determining coordinates of the point in which the touch panel is pressed can be broken up 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 obtained by pressing the touch panel. The value of the divider is read on the bottom contact of the
surface B. Voltage can be in the range of 0V to the power supply 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 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 create a circuit for touch panel control. By means of this circuit, the microcontroller connects appropriate contacts of the touch panel to ground and the power supply (as described above) in order to determine the X and Y coordinates. 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 software consists of writing a menu on graphic LCD, turning the circuit for touch panel control on/off (driving touch panel) and reading the values of A/D converter which actually represent the X and Y coordinates of the point. Once the coordinates are determined, it is possible to decide what we want the microcontroller to do. In this example, microcontroller turns on/off two digital pins, connected to LED diodes A and B. This example use functions belonging to the
Glcd and
ADC library.
Considering that the touch panel surface is slightly larger than the surface of the graphic LCD, in case you want greater accuracy when determining the coordinates, it is necessary to perform the software calibration of the touch panel.
const char msg1[] = "TOUCHPANEL EXAMPLE";
const char msg2[] = "MIKROELEKTRONIKA";
const char msg3[] = "BUTTON1";
const char msg4[] = "BUTTON2";
const char msg5[] = "RC6 OFF";
const char msg6[] = "RC7 OFF";
const char msg7[] = "RC6 ON ";
const char msg8[] = "RC7 ON ";
long x_coord, y_coord, x_coord128, y_coord64; // scaled x-y position
char msg[16];
char * CopyConst2Ram(char * dest, const char * src){
for(;*dest++ = *src++;)
;
return dest;
}
// Glcd module connections
char GLCD_DataPort at PORTD;
sbit GLCD_CS1 at RB0_bit;
sbit GLCD_CS2 at RB1_bit;
sbit GLCD_RS at RB2_bit;
sbit GLCD_RW at RB3_bit;
sbit GLCD_EN at RB4_bit;
sbit GLCD_RST at RB5_bit;
sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections
unsigned int GetX() {
//reading X
PORTC.F0 = 1; // DRIVEA = 1 (LEFT drive on, RIGHT drive on
// , TOP drive off )
PORTC.F1 = 0; // DRIVEB = 0 (BOTTOM drive off )
Delay_ms(5);
return ADC_read(0); // reading X value from RA0 (BOTTOM)
}
unsigned int GetY() {
//reading Y
PORTC.F0 = 0; // DRIVEA = 0 (LEFT drive off , RIGHT drive off
// , TOP drive on)
PORTC.F1 = 1; // DRIVEB = 1 (BOTTOM drive on)
Delay_ms(5);
return ADC_read(1); // reading Y value from RA1 (from LEFT)
}
void main() {
PORTA = 0x00;
TRISA = 0x03; // RA0 i RA1 are analog inputs
ANSEL = 0x03;
ANSELH = 0; // Configure other AN pins as digital I/O
PORTC = 0 ;
TRISC = 0 ; // PORTC is output
Glcd_Init(); // Glcd_Init_EP5
Glcd_Set_Font(FontSystem5x7_v2, 5, 7, 32); // Choose font size 5x7
Glcd_Fill(0); // Clear GLCD
CopyConst2Ram(msg,msg1); // Copy "TOUCHPANEL EXAMPLE" string to RAM
Glcd_Write_Text(msg,10,0,1);
CopyConst2Ram(msg,msg2); // Copy "MIKROELEKTRONIKA" string to RAM
Glcd_Write_Text(msg,17,7,1);
//Display Buttons on GLCD:
Glcd_Rectangle(8,16,60,48,1);
Glcd_Rectangle(68,16,120,48,1);
Glcd_Box(10,18,58,46,1);
Glcd_Box(70,18,118,46,1);
CopyConst2Ram(msg,msg3); // Copy "BUTTON1" string to RAM
Glcd_Write_Text(msg,14,3,0);
CopyConst2Ram(msg,msg5); // Copy "RC6 OFF" string to RAM
Glcd_Write_Text(msg,14,4,0);
CopyConst2Ram(msg,msg4); // Copy "BUTTON2" string to RAM
Glcd_Write_Text(msg,74,3,0);
CopyConst2Ram(msg,msg6); // Copy "RC7 OFF" string to RAM
Glcd_Write_Text(msg,74,4,0);
while (1) {
// 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) && (x_coord128 <= 58) && (y_coord64 >= 18) && (y_coord64 <= 46)) {
if(PORTC.F6 == 0) {
PORTC.F6 = 1;
CopyConst2Ram(msg,msg7); // Copy "RC6 ON " string to RAM
Glcd_Write_Text(msg,14,4,0);
}
else {
PORTC.F6 = 0;
CopyConst2Ram(msg,msg5); // Copy "RC6 OFF" string to RAM
Glcd_Write_Text(msg,14,4,0);
}
}
//if BUTTON2 is selected
if ((x_coord128 >= 70) && (x_coord128 <= 118) && (y_coord64 >= 18) && (y_coord64 <= 46)) {
if(PORTC.F7 == 0) {
PORTC.F7 = 1;
CopyConst2Ram(msg,msg8); // Copy "RC7 ON " string to RAM
Glcd_Write_Text(msg,74,4,0);
}
else {
PORTC.F7 = 0;
CopyConst2Ram(msg,msg6); // Copy "RC7 OFF" string to RAM
Glcd_Write_Text(msg,74,4,0);
}
}
Delay_ms(100);
}
}
In order to make this example work properly, it is necessary to tick off the following libraries in the
Library Manager prior to compiling: