int writeTouch (int data);
int readTouch (void);
void waitTouch (void);
int touchcalSet(void); // Has cal been done already?
int touchDisable(void);
int touchEnable(void);
int touchgetOffset(void);
int touchsetmode(int offset,int mode,int calib);
//***************************
//Write to the touch screen
//***************************
int writeTouch (int data)
{
SPI1BUF = data;
while(!SPI1STATbits.SPITBF);
return(ReadSPI1());
}
int readTouch (void)
{
SPI1BUF = 0;
while(SPI1STATbits.SPIRBF);
return(ReadSPI1());
}
void waitTouch (void)
{
while(!INT_TOUCH)
__delay32(100);
}
//***************************
//Initialize touch screen
//***************************
void initialize_Touch(void)
{
int regOffset;
CS_TOUCH_TRIS = 0; //Output CS
Nop();
CS_TOUCH = 1; //Active Low
Nop();
INT_TOUCH_TRIS = 1; //tris input
Nop();
touchDisable();
regOffset = touchgetOffset();
touchsetmode(regOffset,TOUCH_M_4,0); //4wire mode !
touchCaibrate(); // cal the touch ic
__delay32(900000); //50ms Delay 100000, wait a bit
touchEnable();
}
//***************************
//Get offset register data
//***************************
int touchgetOffset(void) // get Reg start address
{ //Receive : <0x55><0x03><0x00><0x22>REG>
int d1,d2,d3,pass,regStart;
CS_TOUCH = 0;
d1 = writeTouch(HEADER_T); //GET add offset
d1 = writeTouch(0x01); //number of bytes
d1 = writeTouch(TOUCH_REGSTART);
waitTouch();
d1 = readTouch();
d2 = readTouch();
pass = readTouch();
d3 = readTouch();
regStart = readTouch();
CS_TOUCH = 1;
return regStart;
}
//***************************
//Disable Touch screen
//***************************
int touchDisable(void)
{
int d1,d2,d3,d4;
Nop();
__delay32(200000); //50ms Delay 100000
CS_TOUCH = 0; //Active Low
Nop();
__delay32(100);
d1 = writeTouch(HEADER_T); //Disable
d1 = writeTouch(0x01); //number of bytes
d1 = writeTouch(TOUCH_DIS);
waitTouch();
d1 = readTouch(); //0x85
d2 = readTouch(); //data
d3 = readTouch(); //0 if sucsess
d4 = readTouch();
CS_TOUCH = 1;
__delay32(200000); //50ms Delay 100000
return d3; // 0 = good
}
//***************************
//Enable touch screen
//***************************
int touchEnable(void)
{
int d1,d2,d3,pass;
Nop();
CS_TOUCH = 0; //Active Low
Nop();
CS_TOUCH = 1;
Nop();
CS_TOUCH = 0; //Active Low
d1 = writeTouch(0x55); //
d1 = writeTouch(0x01); //number of bytes
d1 = writeTouch(0x12);
waitTouch();
d1 = readTouch();
d2 = readTouch();
pass = readTouch();
d3 = readTouch();
Nop();
CS_TOUCH = 1;
__delay32(200000); //50ms Delay 100000
return pass;
}
//***************************
//Setup touch screen mode
//***************************
int touchsetmode(int offset,int mode,int calib)
{
int d1,d2,d3,pass;
int data = 0;
data = ((mode<<1)|(calib));
CS_TOUCH = 0; //Active Low
Nop();
__delay32(100);
d1 = writeTouch(HEADER_T);
d1 = writeTouch((0x04+0x01)); // 4 bytes
d1 = writeTouch(TOUCH_WRITE); //
d1 = writeTouch(0x00); // Reg high byte = 0x00
d1 = writeTouch(offset + TOUCH_OP_REG);
d1 = writeTouch(0x01); // Number of Reg to write
d1 = writeTouch(data); // Mode data
waitTouch();
d1 = readTouch();
d2 = readTouch();
pass = readTouch();
d3 = readTouch();
CS_TOUCH = 1;
return pass;
}
//***************************
//Touch screen was pressed! get x/y
//***************************
void touchPress (void)
{
int pen,xlo,xhi,ylo,yhi;
CS_TOUCH = 0;
Nop();
pen = readTouch();
xlo = readTouch();
xhi = readTouch();
ylo = readTouch();
yhi = readTouch();
CS_TOUCH = 1;
pen = (pen>>7)&0x00FF;
yhi = (((yhi<<7)|ylo)&0x0FFF); // set it to a 0-1024 type thing
xhi = (((xhi<<7)|xlo)&0x0FFF);
}