Problem with touch panel

Discussion in 'Embedded Systems and Microcontrollers' started by mamech, Aug 25, 2011.

  1. mamech

    Thread Starter New Member

    Nov 3, 2010
    23
    0
    I have easypic6 development board and I want to develop touch panel and GLCD applications on pic18f452.

    But when I tried the touch panel example (in mikroC compiler examples) after making some modifications to suit pic18F452(as it is originally designed to suit pic16F887), I got the writings clear on the screen, but the touch screen does not work.

    I suppose that the configuration of ADCON1 is the source of the problem now.

    As I understand, it needs to change A0 and A1 to analog inputs, while all other port A pins are digital output.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. /*
    4.  * Project name:
    5.      TouchPanelWrite (Demo for working with TouchPanel Controller)
    6.  * Copyright:
    7.      (c) Mikroelektronika, 2010.
    8.  * Revision History:
    9.      20100203:
    10.        - initial release;
    11.  * Description:
    12.      This code works with TouchPanel and GLCD. Two digital output and
    13.      two analog input signals are used for communication with TouchPanel.
    14.      This example is for writing on the screen, calibration constants for touch
    15.      panel are set via library function.
    16.  * Test configuration:
    17.      MCU:             PIC16F887
    18.                       http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
    19.      Dev.Board:       EasyPIC6 - ac:Touch_Panel
    20.                       http://www.mikroe.com/eng/products/view/297/easypic6-development-system/
    21.      Oscillator:      HS, 08.0000 MHz
    22.      Ext. Modules:    GLCD 128x64, Touch Panel
    23.                       http://www.mikroe.com/eng/categories/view/43/components/
    24.      SW:              mikroC PRO for PIC
    25.                       http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
    26.  * Notes:
    27.      - Turn on GLCD backlight switch SW8.8.
    28.      - Turn on TouchPanel Controller switches SW9.5, SW9.6, SW9.7 and SW9.8.
    29.      - Turn off PORTA LEDs SW9.1.
    30. */
    31.  
    32. // Glcd module connections
    33. char GLCD_DataPort at PORTD;
    34.  
    35. sbit GLCD_CS1 at RB0_bit;
    36. sbit GLCD_CS2 at RB1_bit;
    37. sbit GLCD_RS  at RB2_bit;
    38. sbit GLCD_RW  at RB3_bit;
    39. sbit GLCD_EN  at RB4_bit;
    40. sbit GLCD_RST at RB5_bit;
    41.  
    42. sbit GLCD_CS1_Direction at TRISB0_bit;
    43. sbit GLCD_CS2_Direction at TRISB1_bit;
    44. sbit GLCD_RS_Direction  at TRISB2_bit;
    45. sbit GLCD_RW_Direction  at TRISB3_bit;
    46. sbit GLCD_EN_Direction  at TRISB4_bit;
    47. sbit GLCD_RST_Direction at TRISB5_bit;
    48. // End Glcd module connections
    49.  
    50. // Touch Panel module connections
    51. sbit DriveA at RC0_bit;
    52. sbit DriveB at RC1_bit;
    53. sbit DriveA_Direction at TRISC0_bit;
    54. sbit DriveB_Direction at TRISC1_bit;
    55. // End Touch Panel module connections
    56.  
    57. bit          write_erase;
    58. char         pen_size;
    59. char write_msg[] = "WRITE";                                // GLCD menu messages
    60. char clear_msg[] = "CLEAR";
    61. char erase_msg[] = "ERASE";
    62. unsigned int x_coord, y_coord;
    63.  
    64. void Initialize() {
    65.   /*ANSEL  = 3;                                              // Configure AN0 and AN1 pins as analog inputs
    66.   ANSELH = 0;                                              // and other AN pins as digital I/O
    67.   TRISA  = 3;
    68.  
    69.   C1ON_bit = 0;                                            // Disable comparators
    70.   C2ON_bit = 0; */
    71.  
    72.   ADCON1 = 4;   //This is the closest configuration to ANSEL =3
    73.                //but here AN3 also will be analog
    74.  
    75. CCP1CON = 0;
    76. CCP2CON = 0;
    77. TRISA = 0b00000011;
    78.  
    79.   Glcd_Init();                                             // Initialize GLCD
    80.   Glcd_Fill(0);                                            // Clear GLCD
    81.  
    82.   ADC_Init();                                              // Initialize ADC
    83.   TP_Init(128, 64, 0, 1);                                  // Initialize touch panel
    84.   TP_Set_ADC_Threshold(900);                               // Set touch panel ADC threshold
    85. }
    86.  
    87. void main() {
    88.  
    89.   Initialize();
    90.  
    91.   // You can get calibration constants using touch panel calibration example
    92.   TP_Set_Calibration_Consts(76, 917, 108, 906);           // Set calibration constants
    93.  
    94.   Glcd_Write_Text("WRITE ON SCREEN", 20, 5, 1) ;
    95.   Delay_ms(1000);
    96.  
    97.   Glcd_Fill(0);                                            // Clear GLCD
    98.   Glcd_V_Line(0,7,0,1);
    99.   Glcd_Write_Text(clear_msg,1,0,0);
    100.   Glcd_V_Line(0,7,97,1);
    101.   Glcd_Write_Text(erase_msg,98,0,0);
    102.  
    103.   // Pen Menu:
    104.   Glcd_Rectangle(41,0,52,9,1);
    105.   Glcd_Box(45,3,48,6,1);
    106.   Glcd_Rectangle(63,0,70,7,1);
    107.   Glcd_Box(66,3,67,4,1);
    108.   Glcd_Rectangle(80,0,86,6,1);
    109.   Glcd_Dot(83,3,1);
    110.  
    111.   write_erase = 1;
    112.   pen_size = 1;
    113.   while (1) {
    114.  
    115.     if (TP_Press_Detect()) {
    116.       // After a PRESS is detected read X-Y and convert it to 128x64 space
    117.       if (TP_Get_Coordinates(&x_coord, &y_coord) == 0) {
    118.  
    119.         if ((x_coord < 31) && (y_coord < 8)) {
    120.  
    121.           Glcd_Fill(0);
    122.  
    123.           // Pen Menu:
    124.           Glcd_Rectangle(41,0,52,9,1);
    125.           Glcd_Box(45,3,48,6,1);
    126.           Glcd_Rectangle(63,0,70,7,1);
    127.           Glcd_Box(66,3,67,4,1);
    128.           Glcd_Rectangle(80,0,86,6,1);
    129.           Glcd_Dot(83,3,1);
    130.  
    131.           Glcd_V_Line(0,7,0,1);
    132.           Glcd_Write_Text(clear_msg,1,0,0);
    133.           Glcd_V_Line(0,7,97,1);
    134.           if (write_erase)
    135.             Glcd_Write_Text(erase_msg,98,0,0);
    136.           else
    137.             Glcd_Write_Text(write_msg,98,0,0);
    138.           }
    139.  
    140.         // If write/erase is pressed
    141.         if ((x_coord > 96) && (y_coord < 8)) {
    142.           if (write_erase) {
    143.             write_erase = 0;
    144.             Glcd_Write_Text(write_msg,98,0,0);
    145.             Delay_ms(500);
    146.             }
    147.           else {
    148.             write_erase = 1;
    149.             Glcd_Write_Text(erase_msg,98,0,0);
    150.             Delay_ms(500);
    151.             }
    152.           }
    153.  
    154.         // If pen size is selected
    155.         if ((x_coord >= 41) && (x_coord <= 52) && (y_coord <= 9))
    156.           pen_size = 3;
    157.  
    158.         if ((x_coord >= 63) && (x_coord <= 70) && (y_coord <= 7))
    159.           pen_size = 2;
    160.  
    161.         if ((x_coord >= 80) && (x_coord <= 86) && (y_coord <= 6))
    162.           pen_size = 1;
    163.  
    164.         if (y_coord < 11)
    165.           continue;
    166.  
    167.         switch (pen_size) {
    168.           case 1 : {
    169.                       if ( (x_coord >= 0) && (y_coord >= 0)  && (x_coord <= 127) && (y_coord <= 63) )
    170.                        Glcd_Dot(x_coord, y_coord, write_erase);
    171.                      break;
    172.                    }
    173.           case 2 : {
    174.                       if ( (x_coord >= 0) && (y_coord >= 0)  && (x_coord <= 127-1) && (y_coord <= 63-1) )
    175.                        Glcd_Box(x_coord, y_coord, x_coord + 1, y_coord + 1, write_erase);
    176.                      break;
    177.                    }
    178.           case 3 : {
    179.                       if ( (x_coord >= 1) && (y_coord >= 1)  && (x_coord <= 127-2) && (y_coord <= 63-2) )
    180.                        Glcd_Box(x_coord-1, y_coord-1, x_coord + 2, y_coord + 2, write_erase);
    181.                      break;
    182.                    }
    183.         }
    184.       }
    185.     }
    186.   }
    187. }
    188.  
    189.  

    Can anyone here help me in this problem ?


    Thanks
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    The touchscreen needs a bit more handling then just reading the A2D: you also need to drive the other sides. What I mean is if you're reading Y then you need to set X+ and X- to a high and low voltage.

    Can you post the schematic of this area? I've used Mikroe's touch screens in the past but I made my own interface. Just need which touchscreen pins go to which A2D inputs, and the transistors in the drive too.

    Also gonna need the code behind TP_Get_Coordinates() too.
     
    mamech likes this.
  3. mamech

    Thread Starter New Member

    Nov 3, 2010
    23
    0
    Actually, the code works perfectly with pic16F887. The only difference is how should I initialize analog port I think and other necessary initializations.

    The schematic is from Visual GLCD tutorial :
    s1.jpg


    Actually, I do not know code behind TP_Get_Coordinates() , here is some kind of illustration in MikroC help:
    s2.jpg


    Datasheet of pic18F452:

    ww1.microchip.com/downloads/en/devicedoc/39564c.pdf


    Thanks for your time
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Wow, you linked to everything beautifully, even the PIC part spec. Thanks, that makes checking this so much faster for me.

    OK, with the schematic and the code looks like you have the driver pins nailed down correctly. (Aside: I always wondered why Mikroe used 5 transistors to do that, I only needed 4.) It works like this:

    Drive A low: Right & Left both float
    Drive A high: Vcc->Right GND->Left
    Drive B low: Top & Bottom both float
    Drive B high: Vcc->Top GND->Bottom

    To read Top to Bottom you make A low & B high and read what Left is.
    To read Top to Bottom you make A high & B low and read what Bottom is.

    That may seen backwards but it's how these silly things work.

    So the code to do that looks OK, you are telling the code in "Touch Panel module connections" that A & B are on RC0 and 1. Leads me to think the problem is in the A2D.

    For the A2D to work you need to set both ADCON0 and ADCON1. With an 8 MHz clock (see Table 17-1 to select ADCS2:ADCS0) I get these to be:

    ADCON0 = b01000001 // to read Right
    ADCON0 = b01001001 // to read Left
    ADCON1 = b11000100 // for both inputs

    In the same way you set the DriveA and DriveB symbols in "Touch Panel module connections" you need a way to define these config bits. Somewhere in the manual for library that supplies TP_Get_Coordinates these should be called out.

    Sorry I don't have that manual to look that up, but congrats in getting that display to work!
     
  5. mamech

    Thread Starter New Member

    Nov 3, 2010
    23
    0
    Haha, this is logic. I should make you feel comfortable while helping me, so you can help me [​IMG]


    But I do not think so. I do not need to make anything to be called. I think that all what I have to do is to set A2d correctly -which I could not till now :(

    The original code for pic16f887 has Ansel =3 and Anselh=0, C1ON_bit = 0 ,C2ON_bit = 0, and then everything works well. The name of the game here is how to make the PIC18f452 with its different register names and funtionalities to do the same as Ansel=3 and Anselh =0

    There is a new remark that I want to say, may be you can conclude something from it:

    There is an example in help of MikroC, this example do the same as the above mentioned code (i.e writes on the screen), but it has an additional part in the beginning that makes calibration before writing on the screen.
    So it asks the user to click the left bottom of the screen then to click the upper right of the screen. When I used the example, I noticed that after calibration, when I try to write on the screen , the writing in permitted only in 2 vertical lines in the right side of the screen, so if I moved my finger right and left nothing written, but when I move it up and down any where on the screen, the filling happens only vertically in those two fixed places.

    I conclude from this that there is something works, but works wrongly.

    *This is MikroC compiler, with its help section :
    http://www.mikroe.com/eng/downloads/get/29/mikroc_pro_pic_2011_v501.zip


    *this is the data sheet of pic16f887:
    http://ww1.microchip.com/downloads/en/DeviceDoc/41291D.pdf



    Need to hear your opinion.


    Thanks for your time
     
    Last edited: Aug 28, 2011
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I don't think it is related to MikroC, it sounds like the fault might be your ADC right/left justification setting.

    If your ADC is set to the wrong justifiy your ADC reading might be taken as 4 times higher than what it is, (ie; 10bit not 8bit) so the X coordinates appear on the far right quarter of the screen as the X coords are 4 times too big.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    mamech: Sorry I disappeared but yesterday morning I had a hurricane pass about 25 miles from my house and have not had power or interwebs since 8AM. (Just some downed tree limbs for me.) I have to say the best time I have responding to posts is when the person on the other end actually has a fully functional brain, not just someone looking for help thru "post me some code to do X."

    Now at work I discover I have that compiler installed here, not that I have ever used it much. I can't get your code to compile clean, looks like its too big for the demo version.

    Anyway, what I did notice is you don't seem to call TP_Init. This function is the place where you tell the lib where the A2D channels are, that's the "char readX_ChNo" and "char readY_ChNo" numbers you send it. You also let it know how big your screen is.

    I don't get what TP_Set_ADC_Threshold() is doing but that looks important too.

    Unfortunately, looks like Mikroe sells you per-compiled libraries without the source code so you can't peek at how it does what it does (nor tweak then if need be). I looked for the source but only found ".mcl" files that are not source code.
     
Loading...