Touchscreen LCD setCursor() Problem

Hi All,

Recently I purchased a 320x240 TFT LCD with Resisitive Touch from Jaycar Electronics here in Australia. Well!!..after a week of trying and trying to get libraries that work with this particular screen I have success. Of course there was little useful documentation on this screen so it was just a matter of trial and error. The driver chip turns out to be the ST7783 and along with the Adafruit GFX Libraries and the MCUfriend Libraries I finally got it working quite well. I am using a Mega which the screen plugs directly into…all well and good.

My problem comes with using tft.setCursor() function.
In my initial Setup I set the screen rotation to 1…meaning Landscape orientation and print a simple message to the screen using tft.setCursor() and tft.print(). This is fine. The text is displayed just as it should.

Now, when I use any further call to the setCursor() function and try to print values to the screen the text is displayed in PORTRAIT orientation AND it is reversed! It also ignores the setTextSize() function and displays the text in default size. hmmmm.

Has anyone else had this problem and if so is their a fix?

I have posted the code below. It doesn’t contain any real functionality as all I wanted to do is set up a template to use for further sketches without having to redo all the initial setup. The only function it performs is to draw some text and pixels so that I could sort out the Touchscreen mapping…which works perfectly with code below! (Sorry if my {} are not in standard position…I prefer it that way)

// Basic TFT LCD Touchscreen Setup Template - Suits 7783 chip ID
 
#include <stdint.h> 
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
 
#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin
 

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
 
#define LCD_CS A3
#define LCD_RS A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
 

#define BLACK   0x0000
#define BLUE    0x001F
#define CYAN    0x07FF
#define WHITE   0xFFFF
 
Adafruit_TFTLCD tft(LCD_CS, LCD_RS, LCD_WR, LCD_RD, LCD_RESET);

void setup() 
{
tft.reset();
uint16_t identifier = 0x7783;
MCUFRIEND_kbv tft;

tft.begin(identifier);
tft.fillScreen(BLACK);
tft.setRotation(1);
tft.setTextSize(2);
tft.setCursor(50,50);
tft.print("Hello");
tft.setCursor(50,150);
tft.print("X 50 Y 150"); 
}

void loop() 
{

TSPoint p = ts.getPoint();

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

//PORTRAIT MAPPING 
if (p.z > 0 && p.z < 1000) 
{
  //p.x = map(p.x, 921, 126, 0, tft.width() );
  //p.y = map(p.y, 921, 75, tft.height(), 0);

//LANDSCAPE 1 MAPPING
p.x = map(p.x, 126, 921, 0, tft.width() );
p.y = map(p.y, 75,921, 0, tft.height());

//Testing the mapping
tft.drawPixel(p.x,p.y,BLUE); //PERFECT

tft.setCursor(50,150);
tft.print("X Val");
tft.print(p.x);         
tft.print(" ");   //displays text in Portrait x/y and in Reverse(esreveR)and at default text size????
tft.print("Y Val");
tft.print(p.y); 

}

}

Any help/suggestions much appreciated.
Best Regards

Make your mind up. Either use Adafruit_TFTLCD.h or MCUFRIEND_kbv.h . Do NOT have both.

e.g. for Adafruit_TFTLCD

#include <Adafruit_TFTLCD.h> // Hardware-specific library
Adafruit_TFTLCD tft(LCD_CS, LCD_RS, LCD_WR, LCD_RD, LCD_RESET);

or for MCUFRIEND_kbv

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft(LCD_CS, LCD_RS, LCD_WR, LCD_RD, LCD_RESET);

Note that MCUFRIEND_kbv ignores the constructor arguments. It ALWAYS uses the Shield wiring.
So you can just say:

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

I am surprised that anything would work with a local tft object in setup() but a global tft object is used by loop() (and anything else)

David.

Hi David,

Thank you for your reply. Ahhhh!!...Ok...That was silly of me...well I am kinda new to this.

It works fine now except the mapping is now all wrong..but I am sure I can work that one out now. At least the text is displaying where and how it should.

I think my head was spinning and I just couldn't see the wood for the trees :)

Once again thanks for your help and I owe you a virtual beer!

Best regards

There is a new Release of MCUFRIEND_kbv. You might find the new TouchScreen_Calibr_native example useful.

I strongly recommend using different variables for the screen coordinates e.g.

    //LANDSCAPE 1 MAPPING
    xcoord = map(p.x, 126, 921, 0, tft.width() );
    ycoord = map(p.y, 75, 921, 0, tft.height());

    //Testing the mapping
    tft.drawPixel(xcoord, ycoord, BLUE); //PERFECT

Your Calibration values look unusual. What does the Calibration sketch tell you?

I also suggest that you use ctl-T to format your code.

David.

Hi David,

Thanks I will get the new lib. I can see your point in assigning the values to variables making them more useable in sketches and possibly removing a source of conflicts.

The calibration values were obtained from a calibration sketch supplied in the library I have.
The syntax used in the map function was basically cut and pasted from an example sketch also…so just following along there, but modifying the values to suit the calibration values found ie:

MinX = 126
MaxX = 917
MinY = 75
MaxY = 905

After playing around a bit I arrived at the values used in my sketch.
Before I followed your excellent advice the sketch using the mapped values worked very well…using drawPixel() the accuracy was excellent.

All advice you have given is gratefully taken on board.

Being a newbie it takes some time to get through the learning curve and actually comprehend what is and what is not.

I have set up using MCUFRIEND as using Adafruit gave me nothing but a white screen. I have removed the #includes for Adafruit headers…which has basically halved the sketch size…GREAT!!

I think it time for me to do some more reading.

Thanks David
Best Regards
Derek