Simple Touch Screen Interface

Program Download:

Main C File
Touch_Screen.c
Touch_Screen.hex

The Software
           The code is too long to include everything on this page, so I'll just cover the two important specifics:
    - LCD Update Cycle
    - Coordinate A/D Conversion


           The program has 4 main purposes. First it uses one A/D channel to convert the X-axis data, then a seperate A/D channel to convert the Y-axis data. Afterward, the X-axis data is converted and updated to the LCD, similarly the Y-Axis is converted and updated. Then the cycle repeats itself again.

Conversion And LCD Update

------------« Begin Code »------------
     ..
    ...
	//Don't Display If X or Y Axis Not Active
    if( (result_x > 80 && result_x < 900 ) ){
                            
    /******** X-Axis Update ALL *********/
    itoa( result_x, x_axis );
    
    for(i=0;i<4;i++){
    //Update Display
         if(isalnum(x_axis[i])){
         prnt(x_axis[i]);
         }
         else{
         prnt(0x20);
         }
    }
    ...
    ..
------------« End Code »------------

           The itoa() function is used to convert the analog input to a 10-bit digital number in character form. That means the maximum value output can be 1023 and minimum 0. The conversion is then output as characters to the LCD display.

X-Axis Touch Screen A/D Conversion

------------« Begin Code »------------
..
...
//Set PORTA To Inputs/High Impedance
TRISAbits.TRISA0 = 1;
TRISAbits.TRISA1 = 1;

//Set Lower 2 Bits to High Impedance
TRISCbits.TRISC0 = 1;
TRISCbits.TRISC1 = 1;
//Set Higher 2 Bits to Output
TRISCbits.TRISC2 = 0;
TRISCbits.TRISC3 = 0;

PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;
//Provide Ground To X-axis Of Touch Screen
PORTCbits.RC2 = 0;
//Provide Power To X-axis Of Touch Screen
PORTCbits.RC3 = 1;

// configure A/D convertor
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST &
ADC_8ANA_0REF,ADC_CH0 & ADC_INT_OFF );

Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result_y = ReadADC(); // Read result
CloseADC();
...
..
------------« End Code »------------

           Each axis of the touch screen requires a similar process for testing to see if it is currently being touched and where. First the Power and Ground lines need to be turned on for the appropriate axis, all other connections should be high-impedance so as not to interfere. Then the touch screen output line should be read via the A/D converter. Do this for both X and Y axis and you have a 10-bit value signifying where the screen was touched.



;