Touch Panel Library
The mikroC PRO for AVR provides a library for working with 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 : |
---|---|---|
extern sfr sbit DriveA; |
DriveA line. | sbit DriveA at PORTA2_bit; |
extern sfr sbit DriveB; |
DriveB line. | sbit DriveB at PORTA3_bit; |
extern sfr sbit DriveA_Direction; |
Direction of the DriveA pin. | sbit DriveA_Direction at DDA2_bit; |
extern sfr sbit DriveB_Direction; |
Direction of the DriveB pin. | sbit DriveB_Direction at DDA3_bit; |
Library Routines
- TP_Init
- TP_Set_ADC_Threshold
- TP_Press_Detect
- TP_Get_Coordinates
- TP_Calibrate_Bottom_Left
- TP_Calibrate_Upper_Right
- TP_Get_Calibration_Consts
- TP_Set_Calibration_Consts
TP_Init
Prototype |
void TP_Init(unsigned int display_width, unsigned int display_height, char readX_ChNo, char readY_ChNo); |
---|---|
Description |
Initialize touch panel display. Default touch panel ADC threshold value is set to 3900. |
Parameters |
|
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 |
TP_Set_ADC_Threshold
Prototype |
void TP_Set_ADC_Threshold(unsigned int threshold); |
---|---|
Description |
Set custom ADC threshold value, call this function after |
Parameters |
|
Returns |
Nothing. |
Requires |
|
Example |
TP_Set_ADC_Threshold(3900); // Set touch panel ADC threshold |
TP_Press_Detect
Prototype |
char TP_Press_Detect(); |
---|---|
Description |
Detects if the touch panel has been pressed. |
Parameters |
None. |
Returns |
|
Requires |
Global variables :
|
Example |
// Touch Panel module connections sbit DriveA at PORTA2_bit; sbit DriveB at PORTA3_bit; sbit DriveA_Direction at DDA2_bit; sbit DriveB_Direction at DDA3_bit; // End Touch Panel module connections if (TP_Press_Detect()) { ... } |
TP_Get_Coordinates
Prototype |
char TP_Get_Coordinates(unsigned int *x_coordinate, unsigned int *y_coordinate); |
---|---|
Description |
Get touch panel coordinates and store them in |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
if (TP_Get_Coordinates(&x_coord, &y_coord) == 0) { ... } |
TP_Calibrate_Bottom_Left
Prototype |
void 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 |
TP_Calibrate_Upper_Right
Prototype |
void 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 |
TP_Get_Calibration_Consts
Prototype |
void TP_Get_Calibration_Consts(unsigned int *x_min, unsigned int *x_max, unsigned int *y_min, unsigned int *y_max); |
---|---|
Description |
Gets calibration constants after calibration is done and stores them in |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
TP_Get_Calibration_Consts(&x_min, &y_min, &x_max, &y_max); // Get calibration constants |
TP_Set_Calibration_Consts
Prototype |
void TP_Set_Calibration_Consts(unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max); |
---|---|
Description |
Sets calibration constants. |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Example |
TP_Set_Calibration_Consts(148, 3590, 519, 3370); // Set calibration constants |
Library Example
The following drawing demo tests routines of the Touch Panel library :
// Glcd module connections char GLCD_DataPort at PORTC; char GLCD_DataPort_Direction at DDRC; sbit GLCD_CS1 at PORTD2_bit; sbit GLCD_CS2 at PORTD3_bit; sbit GLCD_RS at PORTD4_bit; sbit GLCD_RW at PORTD5_bit; sbit GLCD_EN at PORTD6_bit; sbit GLCD_RST at PORTD7_bit; sbit GLCD_CS1_Direction at DDD2_bit; sbit GLCD_CS2_Direction at DDD3_bit; sbit GLCD_RS_Direction at DDD4_bit; sbit GLCD_RW_Direction at DDD5_bit; sbit GLCD_EN_Direction at DDD6_bit; sbit GLCD_RST_Direction at DDD7_bit; // End Glcd module connections // Touch Panel module connections sbit DriveA at PORTA2_bit; sbit DriveB at PORTA3_bit; sbit DriveA_Direction at DDA2_bit; sbit DriveB_Direction at DDA3_bit; // End Touch Panel module connections bit write_erase; char pen_size; char write_msg[] = "WRITE"; // GLCD menu messages char clear_msg[] = "CLEAR"; char erase_msg[] = "ERASE"; unsigned int x_coord, y_coord; void Initialize() { Glcd_Init(); // Initialize GLCD Glcd_Fill(0); // Clear GLCD ADC_Init(); // Initialize ADC TP_Init(128, 64, 0, 1); // Initialize touch panel TP_Set_ADC_Threshold(900); // Set touch panel ADC threshold } void Calibrate() { 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); } void main() { Initialize(); Glcd_Write_Text("CALIBRATION",32,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); // Clear GLCD 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 (1) { if (TP_Press_Detect()) { // After a PRESS is detected read X-Y and convert it to 128x64 space if (TP_Get_Coordinates(&x_coord, &y_coord) == 0) { if ((x_coord < 31) && (y_coord < 8)) { 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) Glcd_Write_Text(erase_msg,98,0,0); else Glcd_Write_Text(write_msg,98,0,0); } // If write/erase is pressed if ((x_coord > 96) && (y_coord < 8)) { if (write_erase) { write_erase = 0; Glcd_Write_Text(write_msg,98,0,0); Delay_ms(500); } else { write_erase = 1; Glcd_Write_Text(erase_msg,98,0,0); Delay_ms(500); } } // If pen size is selected if ((x_coord >= 41) && (x_coord <= 52) && (y_coord <= 9)) pen_size = 3; if ((x_coord >= 63) && (x_coord <= 70) && (y_coord <= 7)) pen_size = 2; if ((x_coord >= 80) && (x_coord <= 86) && (y_coord <= 6)) pen_size = 1; if (y_coord < 11) continue; switch (pen_size) { case 1 : { if ( (x_coord >= 0) && (y_coord >= 0) && (x_coord <= 127) && (y_coord <= 63) ) Glcd_Dot(x_coord, y_coord, write_erase); break; } case 2 : { if ( (x_coord >= 0) && (y_coord >= 0) && (x_coord <= 127-1) && (y_coord <= 63-1) ) Glcd_Box(x_coord, y_coord, x_coord + 1, y_coord + 1, write_erase); break; } case 3 : { if ( (x_coord >= 1) && (y_coord >= 1) && (x_coord <= 127-2) && (y_coord <= 63-2) ) Glcd_Box(x_coord-1, y_coord-1, x_coord + 2, y_coord + 2, write_erase); break; } } } } } }
What do you think about this topic ? Send us feedback!