You are on page 1of 2

/*

touchscreen.c
Chris Chow, Anup Tapase, Lingyu Xie
Interface PIC with capacitive touchscreen and display X-Y coordinates on LCD
Feb 12, 2009
*/
#include <18f4520.h>
#use delay(clock=40000000)
#use rs232(baud=9600, UART1) // hardware UART; uses RC6/TX and RC7/RX
#include "flex_lcd.c" //must include in order to output to LCD
int j;
int16 x_pos, y_pos;
void get_screen_pos(int16& x_pos, int16& y_pos);
void main()
{
lcd_init(); // Always call this first.
//command Format Tablet: "<SOH>FT<CR>" sent to touchscreen via RC6/TX pin
putc(0x01);
putc(0x46);
putc(0x54);
putc(0x0D);
while (TRUE)
{
//get the finger position relative to the touchscreen in 16-bit int format
get_screen_pos(x_pos, y_pos);
printf(lcd_putc,"\fX: %Lu\n",x_pos); //output "X: <x_pos>" to first line o
f LCD
printf(lcd_putc,"Y: %Lu",y_pos); //output "Y: <y_pos>" to second line
of LCD
}
}
//Function adapted from Nick Marchuk's IR touchscreen code
void get_screen_pos(int16& x_pos, int16& y_pos)
{
int i, gotD8, j;
int touchscreen_rcx[20];
int pos[5];
int16 high16, low16, xtotal16, ytotal16;
int temp4, total8;
high16 = 0;
low16 = 0;
xtotal16 = 0;
ytotal16 = 0;
temp4 = 0;
total8 = 0;
//get 10 bytes from the touchscreen (each touch signal is 10 bytes)
for (i=1;i<=10;++i)
{
touchscreen_rcx[i] = getc();
}
//find the first instance of 0xD8 (start of each signal)
gotD8 = 0;
j = 1;
while(gotD8==0)
{
if((touchscreen_rcx[j] == 0xD8))
{
gotD8 = 1; //j is the location of 0xD8
}
else
{
j = j+1;
}
if(j==9)
{
gotD8 = 1; //didnt find it, dont loop forever
}
}
pos[1] = touchscreen_rcx[j+1]; //xlow, bits: 0 X6 X5 X4 X3 X2 X1 X0 (see touc
hscreen datasheet for FT response))
pos[2] = touchscreen_rcx[j+2]; //xhigh, bits: 0 X13 X12 X11 X10 X9 X8 X7
pos[3] = touchscreen_rcx[j+3]; //ylow, bits: 0 Y6 Y5 Y4 Y3 Y2 Y1 Y0
pos[4] = touchscreen_rcx[j+4]; //yhigh, bits: 0 Y13 Y12 Y11 Y10 Y9 Y8 Y7
//make the x
temp4 = pos[2];
high16 = temp4; //make high16= 0000 0000 0X13X12X11 X10X9X8X7
low16 = pos[1]; //make low16= 0000 0000 0X6X5X4 X3X2X1X0
xtotal16 = low16|(high16<<7); //OR 00X13X12 X11X10X9X8 X7000 0000 with 0000 0
000 0X6X5X4 X3X2X1X0
x_pos = xtotal16; //x_pos=00X13X12 X11X10X9X8 X7X6X5X4 X3X2X1X0
//make the y
temp4 = pos[4];
high16 = temp4; //make high16= 0000 0000 0Y13Y12Y11 Y10Y9Y8Y7
low16 = pos[3]; //make low16= 0000 0000 0Y6Y5Y4 Y3Y2Y1Y0
ytotal16 = low16|(high16<<7); //OR 00Y13Y12 Y11Y10Y9Y8 Y7000 0000 with 0000 0
000 0Y6Y5Y4 Y3Y2Y1Y0
y_pos = ytotal16; //x_pos=00Y13Y12 Y11Y10Y9Y8 Y7Y6Y5Y4 Y3Y2Y1Y0
}

You might also like