// TFT module connections
char TFT_DataPort at LATE;
char TFT_DataPort_Direction at TRISE;
sbit TFT_RST at LATC1_bit;
sbit TFT_BLED at LATD2_bit;
sbit TFT_RS at LATB15_bit;
sbit TFT_CS at LATF12_bit;
sbit TFT_RD at LATD5_bit;
sbit TFT_WR at LATD4_bit;
sbit TFT_RST_Direction at TRISC1_bit;
sbit TFT_BLED_Direction at TRISD2_bit;
sbit TFT_RS_Direction at TRISB15_bit;
sbit TFT_CS_Direction at TRISF12_bit;
sbit TFT_RD_Direction at TRISD5_bit;
sbit TFT_WR_Direction at TRISD4_bit;
sbit SoftSpi_SDI at LATD0_bit;
sbit SoftSpi_SDO at LATC4_bit;
sbit SoftSpi_CLK at LATD10_bit;
sbit SoftSpi_SDI_Direction at TRISD0_bit;
sbit SoftSpi_SDO_Direction at TRISC4_bit;
sbit SoftSpi_CLK_Direction at TRISD10_bit;
sbit T_CS at LATA14_bit;
sbit T_CS_Direction at TRISA14_bit;
sbit T_Penirq at LATA1_bit;
sbit T_Penirq_Direction at TRISA1_bit;
unsigned int T_x,T_y;
unsigned int TP_x = 0, TP_y = 0;
unsigned int proba=321;
char probat[7];
char txtx[7];
char txty[7];
unsigned char tacsie(void)
{
if (PORTAbits.RA1==0) // is SW1 pressed?
{ //yes
delay_ms(10); // wait 10mS for debounce.
if (LATAbits.LATA1==0) // is SW1 still has pressed status after 10mS delay?
{ // yes, we have key press
return 1;
}
}
return 0;// if key is not pressed, return 0
}
void touch_Init()
{
T_CS = 1;
SoftSPI_CLK = 1;
SoftSPI_SDI = 1;
SoftSPI_CLK = 1;
TFT_Write_Text("------------------------------", 5, 50);
TFT_Write_Text("Touch initialized...", 5, 60);
}
void WriteCharTo7843(unsigned char num)
{
unsigned char count=0;
SoftSpi_CLK = 0;
for(count=0;count<8;count++)
{
if (num&0x80)
{SoftSpi_SDI = 1;}
else
{SoftSpi_SDI = 0;}
num = num<<1;
SoftSpi_CLK = 0; asm nop; asm nop; asm nop;
SoftSpi_CLK = 1; asm nop; asm nop; asm nop;
}
TFT_Write_Text("Write char", 5, 80);
}
//**********************************************************
unsigned int ReadFromCharFrom7843()
{
unsigned char count=0;
unsigned int num=0;
for(count=0;count<12;count++)
{
num = num<<1;
SoftSpi_CLK = 1; asm nop; asm nop; asm nop;
SoftSpi_CLK = 0; asm nop; asm nop; asm nop;
if(SoftSpi_SDO == 1) num++;
}
TFT_Write_Text("Read char", 5, 100);
return(num);
}
//**********************************************************
void touch_GetAdXY()
{
T_CS = 0;
TFT_Write_Text("GET ADXY", 5, 120);
WriteCharTo7843(0x90);
SoftSpi_CLK = 1; asm nop; asm nop; asm nop; asm nop;
SoftSpi_CLK = 0; asm nop; asm nop; asm nop; asm nop;
T_y = ReadFromCharFrom7843();
WriteCharTo7843(0xD0);
SoftSpi_CLK = 1; asm nop; asm nop; asm nop; asm nop;
SoftSpi_CLK = 0; asm nop; asm nop; asm nop; asm nop;
T_x = ReadFromCharFrom7843();
T_CS = 1;
TP_x = abs(abs(T_x>>4)-255); //abs(T_x>>4); to reverse x position!
if (TP_x==255) TP_x=0;
TP_y = (((abs((T_y>>4)-255))*10)/7);
}
void Drow_Touch_Point(unsigned int x,unsigned int y)
{ TFT_Set_Pen(CL_BLACK, 2);
TFT_Line(x-4,y,x+5,y);
TFT_Line(x,y-4,x,y+5);
TFT_Dot(x+1,y+1,CL_BLACK);
TFT_Dot(x-1,y+1,CL_BLACK);
TFT_Dot(x+1,y-1,CL_BLACK);
TFT_Dot(x-1,y-1,CL_BLACK);
TFT_Set_Font(TFT_defaultFont, CL_RED , FO_HORIZONTAL);
TFT_Write_Text("D-Amp TFT Calibration", 70,5);
TFT_Circle(x,y,3);
}
void position_T()
{
unsigned int lx = 0;
unsigned int ly = 30;
unsigned char ss[6];
TFT_Fill_Screen(CL_WHITE);
TFT_Set_Font(TFT_defaultFont, CL_RED , FO_HORIZONTAL);
while(1){
touch_GetAdXY();
IntToStr(TP_x,ss);
TFT_Write_Text("PosX1:",lx,ly);
TFT_Write_Text(ss,lx+60,ly);
IntToStr(TP_y,ss);
TFT_Write_Text("PosY1:",lx,ly+20);
TFT_Write_Text(ss,lx+60,ly+20);
Drow_Touch_Point(TP_y-10,TP_x-10);
/*if(tacsie() == 1){
TFT_Write_Text("Touched",5,160);
}
*/
delay_ms(50);
}
}
void PMPWaitBusy() {
while(PMMODEbits.BUSY);
}
void Set_Index(unsigned short index) {
TFT_RS = 0;
PMDIN = index;
TFT_RS = 1;
}
void Write_Command( unsigned short cmd ) {
PMDIN = cmd;
}
void Write_Data(unsigned int _data) {
PMDIN = _data;
}
void Set_BackLight(unsigned short Bkl)
{
TFT_CS = 0;
Set_Index(0xBE); //set PWM for B/L
Write_Command(0x06);
Write_Command(Bkl);
Write_Command(0x01);
Write_Command(0xf0);
Write_Command(0x00);
Write_Command(0x00);
TFT_CS = 1;
}
Init_Screen(){
// PMP setup
PMMODE = 0;
PMAEN = 0;
PMCON = 0; // WRSP: Write Strobe Polarity bit
PMMODEbits.MODE = 2; // Master 2
PMMODEbits.WAITB = 0;
PMMODEbits.WAITM = 1;
PMMODEbits.WAITE = 0;
PMMODEbits.MODE16 = 1; // 16 bit mode
PMCONbits.CSF = 0;
PMCONbits.PTRDEN = 1;
PMCONbits.PTWREN = 1;
PMCONbits.PMPEN = 1;
TFT_Set_Active(Set_Index,Write_Command,Write_Data);
Set_BackLight(0x80);
TFT_Init_SSD1963(480, 272);
TFT_Fill_Screen(CL_WHITE);
TFT_Set_Font(TFT_defaultFont, CL_BLACK , FO_HORIZONTAL);
Soft_SPI_Init();
TFT_Write_Text("System ready...", 5, 0);
}
void main() {
AD1PCFG = 0xFFFF; // Configure AN pins as digital I/O
Init_Screen();
touch_Init();
while(1){
Drow_Touch_Point(Tp_x,Tp_y);
//position_T();
//touch_GetAdXY();
/* IntToStr(proba,probat);
TFT_Write_Text(probat, 40, 20);
IntToStr(T_y,txty);
IntToStr(T_x,txtx);
TFT_Write_Text("X:", 5, 20);
TFT_Write_Text(txtx, 20, 20);
TFT_Write_Text("Y:", 5, 40);
TFT_Write_Text(txty, 20, 40); */
Delay_Ms(10);
}
}