Touchscreen

MBED Lib ERROR

At the moment you have to use a old version of the mbed lib to use this library, because of a error in using the analog pins. The actual mbed lib is not switching the analog pins to digital mode !

The latest working mbed version is 43 from 26. oct 12.

To connect a resistiv touchscreen to the mbed we need four pins. Two analog inputs and two digital IOs. The connections on the touchscreen are normaly marked as x+,x-,y+ and y-.

The touchscreen is working like two potenciometers. First we have to read the X direction. To get a voltage across the X direction we connect the Y lines one to 0V and one to 3,3V. If the screen is hit we get a voltage between 0 to 3,3V - depending on the position. After that we connect the X lines to 0V and 3,3V and we detect the voltage on one of the Y lines. If there is no hit the line is not connected and we get a low reading.

/media/uploads/dreschpe/touch.jpg

/media/uploads/dreschpe/tft2.png

Library

http://mbed.org/users/dreschpe/libraries/Touch_tft/lx1972

The commands :

  • calibrate() ask the user to point onto two point to calibrate the screen
  • point get_touch() return analog x and y
  • bool is_touched(point) test if the screen is touched
  • point to_pixel(point) use the calibration data to calculate the pixel position

The demo code :

 #include "mbed.h"
 #include "SPI_TFT.h"
 #include "Arial12x12.h"
 #include "touch_tft.h"

 // the TFT is connected to SPI pin 5-7
 // the touch is connected to 19,20,16,17
 
 touch_tft tt(p19,p20,p16,p17,p5, p6, p7, p8, p15,"TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs, reset
 int main() {
   point p;
 
   tt.claim(stdout);        // send stdout to the TFT display
   tt.background(Black);    // set background to black
   tt.foreground(White);    // set chars to white
   tt.cls();                // clear the screen
   tt.set_font((unsigned char*) Arial12x12);  // select the font
   tt.set_orientation(1);
 
   tt.calibrate();          // calibrate the touch
  while (1) {
        p = tt.get_touch();   // read analog pos.
        if (tt.is_touched(p)) {  // test if touched
            p = tt.to_pixel(p);             // convert to pixel pos
            tt.fillcircle(p.x,p.y,3,Blue);  // print a blue dot on the screen
      }
  }
 

will print a blue dot if the screen is hit.

Also see the demo :

http://mbed.org/users/dreschpe/notebook/micro-paint/


32 comments on Touchscreen:

15 Oct 2011

Hello

i have some questions if u may... in the data sheet at mikroelectonika they mention its important to use pull-up resistor .. so my question is do i have to connect resistor or do i connect directly .. and what are the values of the resistors if i have to use any ???

Thanks in advance

15 Oct 2011

Hello Mohammed, you can connect the touchscreen signals direct to the mbed pins. I use the pull-up inside the mbed pins.

Regards, Peter

14 Nov 2011

Hi, I try this code but I have a grey screen. Anybody have an idea ? I note that "SPI_TFT library" works great.

It's OK I solved my problem. I used the new version of "SPI_TFT library"

But I have a new problem : When I touched the screen the position is always X=0 and Y=0.

The Pencil is mandatory for this ? Or I can do it with other thing ?

14 Nov 2011

Hello Maxime, do you see the background light ? Without the illumination you will not see the graphic.

To debug the touch screen - print out the analog readout with xx.get_touch(). Do you get different readings ?

Peter

15 Nov 2011

Hi Peter I try to debug with xx.get_touch(). I get value but after xx.to_pixel(p), p.x and p.y are 0 all time. Do you have an idea ?

15 Nov 2011

Hello Maxime, you have to call the calibrate() function one time before using the to_pixel function. Inside the class there are four unsigned short variable x_off,y_off,pp_tx,pp_ty. They are declared as protected at the moment - you can't use them outside. I think it will be handy to save them to the local storage to use them next time the device is powered up. I will add a function to do.

Peter

15 Nov 2011

Thanks Peter for your answer. But I already used the calibrate function and it doesn't work.

15 Nov 2011

can you get the demo program to work AS IS

I have this screen, and it works very well,

Something to check, is the connections for the touch screen X+, X-, Y+, Y-

Apart from a little jitter/noise it runs lovely.

Cheers

Ceri

15 Nov 2011

You can check the touchscreen with a multimeter. Disconnect the touch signals from the mbed.

Between the x+ and x- I have 350 ohm and between y+ and y- 370ohm.

x+/- to y+/- should be isolated until you touch the screen.

Peter

16 Nov 2011

I check with a multimeter. I got 340 ohm between X+ and X- and 370 ohm between Y+ and Y-. What I need to do ?

16 Nov 2011

Have yoy double checked the connections between MBED & Screen ?

If you mix up X's and Y's, then it cannot work at all !

16 Nov 2011

Hi Ceri,

I checked the connections several times, but that's OK, I'm sure. The problem is xx.to_pixel function that returns 0 every time, then p have different values​​ each time.

If I try p.x without used xx.to_pixel(p) I got big value (1000 < value < 50000). And this values are logical for axes X and Y.

I don't see where is my mistake.

16 Nov 2011

Hello Maxime, is the calibration screen working ? Do the programm detect if you touch the two small crosses ?

17 Nov 2011

I don't really if the calibration program works. Because when I touch the crosses it's seem work's. I touch the first cross and the second and i get the "home screen". So I can't understood why it's doesn't work :S

17 Nov 2011

Maxime, I have "stored" values below for my touchscreen. These values can change by as much as 10% every time you attempt a calibration. I had added routines to touch_tft.cpp / .h where I can "remember and restore" previously calibrated values so I don't have to recalibrate every reboot. I store the values in a file I call tft.ini in the main flash.

pp_tx=145 pp_ty=191 x_off=9477 y_off=10210

...kevin

18 Nov 2011

Thanks Kévin,

You solved my problem !

02 Dec 2011

Thanks for this code, most impressive, however I have one hopefully small problem.

I notice the documentation says the touch screen requires two analogue inputs and two digital IO. Why then have you used four pins with analogue capability? For my application I can spare two analogue ins but I still need four for the main application. I have tried the following without success:-

Move Y- from pin 17 to pin 13

Move X+ from pin 19 to pin 15

Move X- from pin 20 to pin 14

Y+ remains on pin 16

and replacing this code line:-

touch_tft TFT(p19,p20,p16,p17,p5, p6, p7, p8, p9,"TFT"); x+,x-,y+,y-,mosi, miso, sclk, cs, reset

with this

touch_tft TFT(p15,p14,p16,p13,p5, p6, p7, p8, p9,"TFT"); x+,x-,y+,y-,mosi, miso, sclk, cs, reset

Any ideas? Thanks Jon

02 Dec 2011

Ah, further research shows the above pin swapping does work. The problem now becomes rather different. Elsewhere in my code I have

AnalogIn foo(p19); AnalogIn bar(p20); These lines cause no problem, touch still works

but then within a 'Ticker' service routine, called every 625 micro seconds I use

float A = foo.read(); and likewise for 'bar'

This hangs the touch software in 'calibrate' where I see the '+' in the top left corner, but no response to stylus stab!

Disabling the Ticker, so preventing reads of pins 19 and 20, allows touch screen software to work properly.

Thanks for any ideas

Jon

02 Dec 2011

Hello Jon, I have started with 4 analog pins to read out the Touch and in the end I only need two of them, but I didn't change my setup.

I think there will be a problem with the re-entrancy of the mbed function to read out the analog pins. Your ticker routine is interrupting the touch routine inside the calling of the analog reading. The mbed cpu has only one ADC. This can cause a problem. You can try to disable the timer3 interrupt while reading the touch pins. Timer3 fire the ticker. Right Click on the touch.lib and activate edit libary...

Inside the touch_tft::get_touch function you block the interrupts while reading the analog pins :

...
NVIC_DisableIRQ(TIMER3_IRQn);  
y1 = Ay.read_u16(); // get y voltage
NVIC_EnableIRQ(TIMER3_IRQn);

...

NVIC_DisableIRQ(TIMER3_IRQn);  
x1 = Ax.read_u16(); // get x voltage
NVIC_EnableIRQ(TIMER3_IRQn);

...

This will delay the Ticker until the adc is read.

Peter

02 Dec 2011

Many thanks Peter, Problem solved!

24 Feb 2013

Hi Peter,

I'm trying to break out the calibration into a separate function (I've got different functions to draw different screens) but the calibration isn't working. It brings up the message "press cross to calibrate" but it doesn't respond to any touch.

To check my wiring I've imported your micro-paint code and run that and it calibrates and runs fine.

Any idea what might be wrong?

touch_tft tt(p19,p20,p16,p17,p11, p12, p13, p14, p15,"TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs, reset

void drawCalibrate(){
    tt.claim(stdout);        // send stdout to the TFT display  ????
    tt.background(Black);    // set background to black
    tt.foreground(White);    // set chars to white
    tt.cls();                // clear the screen
    tt.set_font((unsigned char*) Arial12x12);  // select the font
    tt.set_orientation(1);
    tt.calibrate();
    tt.locate(0,0);
    currentScreen = 1;
    lastDrawnScreen = 0;
}

Also, I don't know if it's relevant (as the micro-paint code runs fine), but I'm running the code on an lpcexpresso board rather than my mbed.

Cheers,

Matt

25 Feb 2013

If you update the mbed lib the touchscreen stop working :-((

It is a problem with my lib. The Touch signals do not operate. The DigitalInOut pins are not working ... I'm toggle between Digital IO and analog input. I will have a closer look end of the week.

27 Feb 2013

Hi Peter,

Ok, it's good to know I'm not doing anything stupid :-) I've got enough other code to be working on for now.

Cheers,

Matt

07 Mar 2013

what is the model of this TFT

07 Mar 2013
19 Oct 2013

does this lib support the frdm kl25z ?

19 Oct 2013

I have not Test it... It is using two analog Inputs, which have to been switched to digital outputs and back. There was a problem with the mbed lib starting ver. 44 because it does not switch between analog and digital mode... I have not test to compiler the Lib in the last time... The Lib is using only mbed functions also working on kl25z.

21 Oct 2013

Instead of switching the DigitalInOut pins to input or output, save just the PinName in the class constructor and the construct DigitalIn / DigitalOut objects in the getTouch function. this will fix the problem with the mbed libraries.

I did a similar workaround in this library: http://mbed.org/users/mazgch/code/SeeedStudioTFTv2/file/dfd69b3be5b6/SeeedStudioTFTv2.cpp

22 Nov 2013

I have a problem my touchscreen LCD HY32D does not have x,y pins.however it does have XPT2046.

http://www.hotmcu.com/32-touch-screen-tft-lcd-with-16-bit-parallel-interface-p-36.html

How can i use this LCD to detect touch ?

22 Nov 2013

maryam wasim wrote:

I have a problem my touchscreen LCD HY32D does not have x,y pins.however it does have XPT2046.

http://www.hotmcu.com/32-touch-screen-tft-lcd-with-16-bit-parallel-interface-p-36.html

How can i use this LCD to detect touch ?

The XPT2046 does all the analog to digital conversion stuff. It has a serial SPI interface. You need a library that controls the device and reads out the internal registers to get the X,Y coordinates. There are some libs on the mbed site that should help you. Here is one.

25 Nov 2013

but there are pins x and y used in the files what should i connect to it ??

08 Jan 2014

Maxime Nounckele wrote:

Hi, I try this code but I have a grey screen. Anybody have an idea ? I note that "SPI_TFT library" works great.

It's OK I solved my problem. I used the new version of "SPI_TFT library"

But I have a new problem : When I touched the screen the position is always X=0 and Y=0.

The Pencil is mandatory for this ? Or I can do it with other thing ?

Hi Peter, I encountered the same problem as maxime and did the same, updated with the new SPI_TFT_ILI9341 file. It worked but now i have another problem. I'm stuck in the calibration part, the 1st cross on the top left corner of the screen. I keep pressing the cross and the screen but nothing happens.

I tried to display the values of p.x and p.y using the code below (i've added display to the calibrate function):

for (i=0; i<5; i++) { do { p = get_touch(); float x,y; x = p.x; y = p.y; printf("%f \n",x); printf("%f \n",y);

} while (p.x < 0x2000 | p.y < 0x2000); wait for touch a += p.x; b += p.y; }

if i dont touch the screen, values range from x(0-150) y(0). if i touch the screen, values range from x always 0, y around 64000+

Please log in to post comments.