Routines for Touch controler XPT2046
Posted: 01 Sep 2015 21:33
Hello Mikroe team,
here is code for Mikrobasic Pro for PIC32 and Visual TFT for Touch screen controler XPT2046:
--------------------------------------------------------------------
module xpt2046
' Use HW spi2 modul
dim Xcoord as word external
Ycoord as word external
PenDown as byte external
dim SPI_TP_CS as sbit at LATD8_bit
SPI_TP_CS_Direction as sbit at TRISD8_bit
SPI_TP_IRQ as sbit at PORTD.B11
SPI_TP_IRQ_Direction as sbit at TRISD11_bit
dim xvysl as word
yvysl as word
xnativ as word
ynativ as word
sub procedure SPI_TP_Init()
sub procedure SPI_TP_Check()
sub function SPI_TP_Read_Once(dim address as char) as word
sub function SPI_TP_Get_Coordinates() as char
sub procedure SPI_TP_Info(dim sloupec as word,dim radek as word,dim barva as word)
implements
sub procedure SPI_TP_Init()
SPI_TP_CS_Direction = 0
SPI_TP_IRQ_Direction = 1
SPI_TP_CS = 0
SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 1024, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_IDLE_2_ACTIVE)
SPI_TP_IRQ_Direction = 1
SPI_TP_CS = 1
Xcoord = 0
Ycoord = 0
end sub
sub function SPI_TP_Read_Once(dim address as char) as word
dim hodnota as word
SPI2_Write(address)
hodnota = SPI2_Read(0)
hodnota = hodnota << 8
hodnota = hodnota + SPI2_Read(0)
hodnota = hodnota >> 3 ' Prise en charge CLK pendant BUSY
result = hodnota
end sub
sub function SPI_TP_Get_Coordinates() as char
dim x as word
y as word
dim x_min as word
x_max as word
y_min as word
y_max as word
dim x_div as float
y_div as float
'char debug_text[100]
x_min = 200
x_max = 3900
y_min = 200
y_max = 3900
SPI_TP_CS = 0
x = SPI_TP_Read_Once(0xD0)
y = SPI_TP_Read_Once(0x90)
SPI_TP_CS = 1
'sprintf(debug_text, "X = %u ; Y = %u", x, y);
'UART4_Write_Text(debug_text);
'UART4_Write(13);
'UART4_Write(10);
ynativ=y
xnativ=x
if (x = 0) or (x = 4095) or (y = 0) or (y = 4095) then
result= 1
exit
end if
if x < x_min then
x = x_min
end if
if x > x_max then
x = x_max
end if
if y < y_min then
y = y_min
end if
if y > y_max then
y = y_max
end if
x = x-x_min
y = y-y_min
x_div = x_max
x_div = x_div - x_min
x_div = x_div / 800
y_div = y_max
y_div = y_div - y_min
y_div = y_div / 480
x = x / x_div
y = y / y_div
xvysl = x
yvysl = y
result= 0
end sub
sub procedure SPI_TP_Check()
if SPI_TP_IRQ = 0 then
if SPI_TP_Get_Coordinates() = 0 then
TFT_Dot(Xvysl, Yvysl, CL_white)
TFT_Dot(Xvysl + 1, Yvysl, CL_white)
TFT_Dot(Xvysl, Yvysl + 1, CL_white)
TFT_Dot(Xvysl + 1, Yvysl + 1, CL_white)
delay_ms(200) ' Anti rebond
TFT_Dot(Xvysl, Yvysl, CL_BLACK)
TFT_Dot(Xvysl + 1, Yvysl, CL_BLACK)
TFT_Dot(Xvysl, Yvysl + 1, CL_BLACK)
TFT_Dot(Xvysl + 1, Yvysl + 1, CL_BLACK)
end if
end if
end sub
sub procedure SPI_TP_Info(dim sloupec as word,dim radek as word,dim barva as word)
dim txt65buf as string[5]
dim txt66buf as string[5]
dim txt67buf as string[14]
if SPI_TP_IRQ = 0 then
txt67buf=""
txt66buf=""
txt65buf=""
WordToStr(xnativ, txt65buf)
WordToStr(ynativ, txt66buf)
txt67buf=txt65buf+","+txt66buf
TFT_Set_Pen(CL_black, 1)
TFT_Write_Text(txt67buf, sloupec, radek)
' TFT_Set_Pen(CL_white, 12)
TFT_Set_Brush(1, 1, 0, LEFT_TO_RIGHT, CL_white, CL_white)
TFT_Rectangle(sloupec, radek, sloupec+50, radek+12)
' TFT_Line(sloupec, radek, sloupec+55, radek)
end if
end sub
end.
----------------------------------------------------------------------------------------------------
SUB Check_TP() for VISUAL TFT
sub procedure Check_TP()
if SPI_TP_IRQ = 0 then
' After a PRESS is detected read X-Y and convert it to 128x64 space
if (SPI_TP_Get_Coordinates=0) then
Process_TP_Press(Xvysl, Yvysl)
if PenDown = 0 then
PenDown = 1
Process_TP_Down(Xvysl, Yvysl)
end if
end if
else
if PenDown = 1 then
PenDown = 0
Process_TP_Up(Xvysl, Yvysl)
end if
end if
end sub
Would it be possible for this module and procedure for Visual TFT integrated into Visual TFT as an additional touch controler.
It is very annoying after each change in Visual TFT rewrite automatic insertion routine !!!!
Thank you
here is code for Mikrobasic Pro for PIC32 and Visual TFT for Touch screen controler XPT2046:
--------------------------------------------------------------------
module xpt2046
' Use HW spi2 modul
dim Xcoord as word external
Ycoord as word external
PenDown as byte external
dim SPI_TP_CS as sbit at LATD8_bit
SPI_TP_CS_Direction as sbit at TRISD8_bit
SPI_TP_IRQ as sbit at PORTD.B11
SPI_TP_IRQ_Direction as sbit at TRISD11_bit
dim xvysl as word
yvysl as word
xnativ as word
ynativ as word
sub procedure SPI_TP_Init()
sub procedure SPI_TP_Check()
sub function SPI_TP_Read_Once(dim address as char) as word
sub function SPI_TP_Get_Coordinates() as char
sub procedure SPI_TP_Info(dim sloupec as word,dim radek as word,dim barva as word)
implements
sub procedure SPI_TP_Init()
SPI_TP_CS_Direction = 0
SPI_TP_IRQ_Direction = 1
SPI_TP_CS = 0
SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 1024, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_IDLE_2_ACTIVE)
SPI_TP_IRQ_Direction = 1
SPI_TP_CS = 1
Xcoord = 0
Ycoord = 0
end sub
sub function SPI_TP_Read_Once(dim address as char) as word
dim hodnota as word
SPI2_Write(address)
hodnota = SPI2_Read(0)
hodnota = hodnota << 8
hodnota = hodnota + SPI2_Read(0)
hodnota = hodnota >> 3 ' Prise en charge CLK pendant BUSY
result = hodnota
end sub
sub function SPI_TP_Get_Coordinates() as char
dim x as word
y as word
dim x_min as word
x_max as word
y_min as word
y_max as word
dim x_div as float
y_div as float
'char debug_text[100]
x_min = 200
x_max = 3900
y_min = 200
y_max = 3900
SPI_TP_CS = 0
x = SPI_TP_Read_Once(0xD0)
y = SPI_TP_Read_Once(0x90)
SPI_TP_CS = 1
'sprintf(debug_text, "X = %u ; Y = %u", x, y);
'UART4_Write_Text(debug_text);
'UART4_Write(13);
'UART4_Write(10);
ynativ=y
xnativ=x
if (x = 0) or (x = 4095) or (y = 0) or (y = 4095) then
result= 1
exit
end if
if x < x_min then
x = x_min
end if
if x > x_max then
x = x_max
end if
if y < y_min then
y = y_min
end if
if y > y_max then
y = y_max
end if
x = x-x_min
y = y-y_min
x_div = x_max
x_div = x_div - x_min
x_div = x_div / 800
y_div = y_max
y_div = y_div - y_min
y_div = y_div / 480
x = x / x_div
y = y / y_div
xvysl = x
yvysl = y
result= 0
end sub
sub procedure SPI_TP_Check()
if SPI_TP_IRQ = 0 then
if SPI_TP_Get_Coordinates() = 0 then
TFT_Dot(Xvysl, Yvysl, CL_white)
TFT_Dot(Xvysl + 1, Yvysl, CL_white)
TFT_Dot(Xvysl, Yvysl + 1, CL_white)
TFT_Dot(Xvysl + 1, Yvysl + 1, CL_white)
delay_ms(200) ' Anti rebond
TFT_Dot(Xvysl, Yvysl, CL_BLACK)
TFT_Dot(Xvysl + 1, Yvysl, CL_BLACK)
TFT_Dot(Xvysl, Yvysl + 1, CL_BLACK)
TFT_Dot(Xvysl + 1, Yvysl + 1, CL_BLACK)
end if
end if
end sub
sub procedure SPI_TP_Info(dim sloupec as word,dim radek as word,dim barva as word)
dim txt65buf as string[5]
dim txt66buf as string[5]
dim txt67buf as string[14]
if SPI_TP_IRQ = 0 then
txt67buf=""
txt66buf=""
txt65buf=""
WordToStr(xnativ, txt65buf)
WordToStr(ynativ, txt66buf)
txt67buf=txt65buf+","+txt66buf
TFT_Set_Pen(CL_black, 1)
TFT_Write_Text(txt67buf, sloupec, radek)
' TFT_Set_Pen(CL_white, 12)
TFT_Set_Brush(1, 1, 0, LEFT_TO_RIGHT, CL_white, CL_white)
TFT_Rectangle(sloupec, radek, sloupec+50, radek+12)
' TFT_Line(sloupec, radek, sloupec+55, radek)
end if
end sub
end.
----------------------------------------------------------------------------------------------------
SUB Check_TP() for VISUAL TFT
sub procedure Check_TP()
if SPI_TP_IRQ = 0 then
' After a PRESS is detected read X-Y and convert it to 128x64 space
if (SPI_TP_Get_Coordinates=0) then
Process_TP_Press(Xvysl, Yvysl)
if PenDown = 0 then
PenDown = 1
Process_TP_Down(Xvysl, Yvysl)
end if
end if
else
if PenDown = 1 then
PenDown = 0
Process_TP_Up(Xvysl, Yvysl)
end if
end if
end sub
Would it be possible for this module and procedure for Visual TFT integrated into Visual TFT as an additional touch controler.
It is very annoying after each change in Visual TFT rewrite automatic insertion routine !!!!
Thank you