// Global variables
unsigned int Xcoord, Ycoord;
char PenDown;
void *PressedObject;
int PressedObjectType;
unsigned int caption_length, caption_height;
unsigned int display_width=480, display_height=272;
sbit SPI_TP_CS at LATA7_bit;
sbit SPI_TP_CS_Direction at TRISA7_bit;
sbit SPI_TP_IRQ at LATE9_bit;
sbit SPI_TP_IRQ_Direction at TRISE9_bit;
unsigned int x, y;
unsigned int x_min=450, x_max=3500, y_min=280, y_max=3700;
void SPI_TP_Init() {
SPI_TP_CS_Direction = 0;
SPI_TP_IRQ_Direction = 1;
SPI_TP_CS = 1;
Xcoord = 0;
Ycoord = 0;
}
char xkoord[7];
char ykoord[7];
extern unsigned int color;
extern unsigned int enableDRAW;
void SPI_TP_Check() {
if(SPI_TP_IRQ == 1) {
if(SPI_TP_Get_Coordinates(&Xcoord, &Ycoord) == 0) {
Process_TP_Press(Xcoord, Ycoord);
delay_ms(100); // Anti rebond
if(enableDRAW==1) {
TFT_Dot(Xcoord, Ycoord, color);
TFT_Dot(Xcoord + 1, Ycoord, color);
TFT_Dot(Xcoord,Ycoord + 1, color);
TFT_Dot(Xcoord + 1, Ycoord + 1, color);
}
}
}
}
unsigned int SPI_TP_Read_Once(char address) {
unsigned int val;
SPI2_Write(address);
val = SPI2_Read(0);
val <<= 8;
val += SPI2_Read(0);
val >>= 3;
return val;
}
unsigned int xregi=0;
char SPI_TP_Get_Coordinates(unsigned int *x_coordinate, unsigned int *y_coordinate) {
float x_div=11.213235294, y_div=7.125;
SPI_TP_CS = 0;
SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT,80, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_IDLE_2_ACTIVE);
y = SPI_TP_Read_Once(0xD0);
x = SPI_TP_Read_Once(0x90);
y=abs((y-3830))/y_div;
x=abs((x-3600))/x_div;
SPI_TP_CS = 1;
Xcoord=y;
Ycoord=x;
return 0;
}