Using XPT2046_Touchscreen Library with Audio Shield

Status
Not open for further replies.

DCL

Member
I am trying to use a Color 320x240 TFT Touchscreen with ILI9341 Controller Chip along with the audio shield Rev.C and Teensy 3.2.
I have gotten the screen to display an image with this setup, so I'm pretty sure my connections are all good.

I am now trying to run the XPT2046_Touchscreen library example called ILI9341Test. I have changed the #define's to the ones recommended for usage with audio board (also the way I have connected the board) but it does not seem to be working. The sketch uploads and compiles but the screen is just blank white (or gray).

I noticed in the README for the XPT2046_Touchscreen library it says "The normal MISO, MOSI and SCK pins will be used automatically." I'm not seeing where they are set or if I can change them anywhere.

Code:
#include <ILI9341_t3.h>
#include <font_Arial.h> // from ILI9341_t3
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN  8
//#define TFT_DC  9
//#define TFT_CS 10
// MOSI=11, MISO=12, SCK=13

#define TFT_DC      20
#define TFT_CS      21
#define TFT_RST    255  // 255 = unused, connect to 3.3V
#define TFT_MOSI     7
#define TFT_SCLK    14
#define TFT_MISO    12
//ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);

XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN  2
//XPT2046_Touchscreen ts(CS_PIN);  // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255);  // Param 2 - 255 - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);

void setup() {
  Serial.begin(38400);
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);
  ts.begin();
  ts.setRotation(1);
  while (!Serial && (millis() <= 1000));
}

boolean wastouched = true;

void loop() {
  boolean istouched = ts.touched();
  if (istouched) {
    TS_Point p = ts.getPoint();
    if (!wastouched) {
      tft.fillScreen(ILI9341_BLACK);
      tft.setTextColor(ILI9341_YELLOW);
      tft.setFont(Arial_60);
      tft.setCursor(60, 80);
      tft.print("Touch");
    }
    tft.fillRect(100, 150, 140, 60, ILI9341_BLACK);
    tft.setTextColor(ILI9341_GREEN);
    tft.setFont(Arial_24);
    tft.setCursor(100, 150);
    tft.print("X = ");
    tft.print(p.x);
    tft.setCursor(100, 180);
    tft.print("Y = ");
    tft.print(p.y);
    Serial.print(", x = ");
    Serial.print(p.x);
    Serial.print(", y = ");
    Serial.println(p.y);
  } else {
    if (wastouched) {
      tft.fillScreen(ILI9341_BLACK);
      tft.setTextColor(ILI9341_RED);
      tft.setFont(Arial_48);
      tft.setCursor(120, 50);
      tft.print("No");
      tft.setCursor(80, 120);
      tft.print("Touch");
    }
    Serial.println("no touch");
  }
  wastouched = istouched;
  delay(100);
}
 
Hello? Is anybody out there? Please let me know if there is something in the way I asked this question that made it difficult to answer. I would like to know because I will probably be posting more questions in the future. I tried to follow the posting guidelines as well as I could.
 
I have not used the audio shield, but, ISTR that pin#13 which is the SPI SCLK on the T3.2 conflicts with the I2S audio connection. To reroute the SPI signals to the alternate pins listed on the card one would use SPI.setSCK(14). Same goes for MOSI & MISO pins. Look at the card and the alternate pins are in gray. Similar comands are available for I2C. You didn't say which II9341 you are using, but, I use the one available from Buydisplay.com and have had no problem with it. Paul's display also works and he takes the time to test them so you always get a good one.

Please be more patient in these tough times. Every body has important things to attend to.
 
I have not used the audio shield, but, ISTR that pin#13 which is the SPI SCLK on the T3.2 conflicts with the I2S audio connection. To reroute the SPI signals to the alternate pins listed on the card one would use SPI.setSCK(14). Same goes for MOSI & MISO pins. Look at the card and the alternate pins are in gray. Similar comands are available for I2C. You didn't say which II9341 you are using, but, I use the one available from Buydisplay.com and have had no problem with it. Paul's display also works and he takes the time to test them so you always get a good one.

Please be more patient in these tough times. Every body has important things to attend to.

Thanks for your reply. I totally understand that these are crazy times and people are busy. I didn't mean to seem impatient. Just wanted to make sure I didn't do anything wrong as I'm new to this forum.
 
Examples for changing the SPI pins can be seen commented in the cardinfo.ino :
Code:
  //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
  //SPI.setSCK(14);  // Audio shield has SCK on pin 14

The Rev C audio board should work with T_3.2 - when soldered?

Then as noted the alternate SPI pins to work around the audio as documented should allow it to work.
 
I have a similar problem (with no audio) in that it seems I have "mutually exclusive" touch and TFT display. I can get beautifully performing TFT displays and terrific touch response showing all the right "decodable" touch values..... but not when both are "ts" and "tft" are active. I have a new Teensy 4.0 (my first Teensy experience and I am impressed) and a generic 3.2" TFT SPI display(MSP3218 running on 3.3V) using:
#include <XPT2046_Touchscreen.h> and
#include <Adafruit_ILI9341.h> plus all of the other stuff

My program works great but can't work with both
tft.begin(); and
ts.begin();(?)

I am wondering if the two header files specify different SPI speeds as described elsewhere in the forum. As I said above, each works just fine separately..... Great TFT but no touch reponse with "// ts.begin();" and great touch response with "ts.begin();" but no TFT.... makes no difference where in the program the "begin" statements are located.
How to identify and sync the speeds on both inclusions?
I would be glad to post everything I have if this isn't a SPISPEED problem. You might try temporarily commenting out either "begin()" then the other to make sure it isn't a ts vs tft conflict rather than an audio conflict. is it possible that there is a SPI speed conflict or that there needs to be a "SPI stop" between the shift between touch and display. Also, I know there is a "begin()" before usage but is there something to "unbegin()" the operation (is that the SPI stop?)
 
@Eagleman - what results show up with '...\hardware\teensy\avr\libraries\XPT2046_Touchscreen\examples\ILI9341Test\ILI9341Test.ino' or other example code? Those use Teensy SPI and ILI9341_and generally work when properly wired.
Code:
#include <ILI9341_t3.h>
#include <font_Arial.h> // from ILI9341_t3
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

Not sure what MSP3218 is - sounds like it is not an ILI9341 display?

It is possible it doesn't properly TriState the MISO/MOSI lines perhaps?
 
I have not used the audio shield, but, ISTR that pin#13 which is the SPI SCLK on the T3.2 conflicts with the I2S audio connection. To reroute the SPI signals to the alternate pins listed on the card one would use SPI.setSCK(14). Same goes for MOSI & MISO pins.
I believe I had already made these changes correctly to my connections on the board. I'm less sure of how I'm dealing with it in the code.
As I said in an earlier post, the display is working in other sketches. i.e. the ILI9341_t3 example called pictureEmbed. That one has the 6 pin constructor code for both "Normal Connections" and "Alternate Connections with Teensy Audio Shield" connections available. So, I'm pretty sure my connections are good.



You didn't say which II9341 you are using, but, I use the one available from Buydisplay.com and have had no problem with it. Paul's display also works and he takes the time to test them so you always get a good one.
The ILI9341 display I am using was purchased by my friend from someone on eBay. It looks identical to the one pictured on the PJRC store. It says "2.8 TFT SPI 240x320 V1.2".
 
In my hardware I have the CS pins pulled up to 3.3v via a 10K resistor to keep the TFT, Touch screen, SD Card and WIZ820/WIZ850 adapters inactive while the Teensy pins are being configured and the individual components being initialized by their respective parts of the software. I remember reading that somewhere in this group and have implemented it regularly and no longer have issues with the SDCard causing instability. I do not have problems with my ILI9341 display and touch screen initializations. I started out with the AdaFruit touch paint example, struggled with adding the SD Card and the WIZnet later on just seemed to work.
 
Examples for changing the SPI pins can be seen commented in the cardinfo.ino :
Code:
  //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
  //SPI.setSCK(14);  // Audio shield has SCK on pin 14
When I add the above to my ILI9341 example, i get an error: 'SPI' does not name a type
Code:
Arduino: 1.8.11 (Mac OS X), TD: 1.50, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz (overclock), Faster, US English"

ILI9341Test_w_AudioSheild:20: error: 'SPI' does not name a type
 SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
 ^
ILI9341Test_w_AudioSheild:21: error: 'SPI' does not name a type
 SPI.setSCK(14);  // Audio shield has SCK on pin 14
 ^
'SPI' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 
Those were what is in the example indicated - try that example to see what happens.

that will show what your hardware does when using the PJRC software.

Errors above show the code in use is not PJRC SPI etc.
 
Those were what is in the example indicated - try that example to see what happens.

that will show what your hardware does when using the PJRC software.

Errors above show the code in use is not PJRC SPI etc.

I tried the CardInfo example and uncommented the lines for the audio board. It builds and uploads. I'm not using the SD card reader for anything at the moment, so not sure what to look for. There is an SD card in there but it's 128gb which I think is not recommended. The serial monitor says this:

initialization failed. Things to check:
* is a card inserted?
* is your wiring correct?
* did you change the chipSelect pin to match your shield or module?
 
Idea was to use ILI9341Test from post #7

Then use the SPI lines from post #5 to get the correct SPI pin connect.

May need to add the right value for SPI.setMISO( # ); for correct function
 
The MSP3218 (typo on first post - not MSP2318) TFT display is as shown on www.LCDWIKI.com under SPI TFT.

It uses the XPT2046 chip(visually verified) and it works perfectly when not trying to display and touch sense in the same program.
I can not get the #include <ILI9341_t3.h> to work; it has errors related to KINETISK which is Greek to me.
I will try to test the examples you listed. Again, the only thing that determines if I get touch or display to work is if I execute "ts.begin();".

Is it possible to change the SCK pin used for the XPT2046 thus having separate clock signals for the ILI9341 and XPT2046? If so, which pin and how to specify in the program. I am pulling out some 100 ohm resistors to try them for my SPI signals.


MSP2318_Teensy40.jpg
 
That display number may be ili9341 compatible - but I don't recognize it.

If it works with the indicated sample then that is a good indication and start.
 
I think I fixed my problem, but would like some reassurance that everything is correct. Like I said, I am new to Teensy and Arduino having worked with PICs for many years.

Apparently there was a SPI conflict when trying to use a "tft.begin();" command and a "ts.begin();" command concurrently.

I added the following function to use before any TFT command:
void SPI2tft()
{
SPI.end();
tft.begin();
tft.setRotation(3);
if (spi_flg != 0)
{
Serial.println("SPI > TFT"); // use for possible troubleshooting
spi_flg = 0;
}
}

And use this function before a touch command:

void SPI2ts()
{
SPI.end();
ts.begin();
ts.setRotation(1);
if (spi_flg != 1)
{
Serial.println("SPI > Touch");
spi_flg = 1;
}
}

I mistakenly thought this would be taken care of automatically. Seems to work; I'll know better when the MENU is linking to the correct places.
Thanks to everyone for just letting me vent which helped(?) to solve my problem.
 
That seems like time wasting overkill that should not be needed? On T_3.2 years back when first working with the XPT2046 the simple samples with the ILI9341 worked well, there was one case where folks adding SD card as well had trouble - and it worked for me - perhaps it involved 'proper .begin order' - but the with a single begin for each was all that was needed for continued operation.

Did the sample display sketch with edits to the moved SPI pins work as written / tested?

It should work and given presented info the troubles seen are either software stack in use {using only PJRC T3 libraries?} - or something unique about the MSP3218 display hardware.
 
defragster, I can't yet answer those questions partly because of needing a better grasp on the whole new world (to me) of Teensy and Arduino. I asked for reassurance because it seemed like overkill to me also. I thank you for your response; you might not understand how that one sentence guides me in the directions I might take to make things work more efficiently.
I am still struggling to figure out all of the kinetisk errors I get when trying to use "ILI9341_t3.h". I honestly think I may have compromised my software resources by flailing out at different combinations and configurations. Now that I have the menu working with both tft and ts functions it might not be a bad idea to delete all Teensy and Arduino tools (not my working program!) and completely reload with a fresh IDE, fresh Teensyduino, fresh libraries and a fresh outlook with more confidence in my setup. I know how when trying to set up something totally new like the first Teensy/Arduino install it's like a prototype where I could be doing stupid things that will cause future grief. Stating out fresh again isn't that hard.... and it is a LOT better and a LOT more rewarding than the last 3 years spent trying to master the PIC32MZ. A very special thanks to Paul et all, whose development of the Teensy line gave me a tremendous toolset to replace the confusion, mis-organization, and cryptic world of the PIC32MZ.
 
So, I still have not gotten Paul Stoffregen's XPT2046_Touchscreen library to work with the audio board connections.
The README for the XPT2046_Touchscreen library states "The normal MISO, MOSI and SCK pins will be used automatically."
Can anybody explain how, or even if, this can be changed in my code? or by making a change to the library code?
 
So, I still have not gotten Paul Stoffregen's XPT2046_Touchscreen library to work with the audio board connections.
The README for the XPT2046_Touchscreen library states "The normal MISO, MOSI and SCK pins will be used automatically."
Can anybody explain how, or even if, this can be changed in my code? or by making a change to the library code?

See post #5 above ...
 
The Audio card Tutorial uses this display with Audio card - see :: \hardware\teensy\avr\libraries\Audio\examples\Tutorial\Part_3_03_TFT_Display\Part_3_03_TFT_Display.ino

Hopefully that works - with No Touch - then add minimal touch in some fashion and test from one of the XPT examples?

For that to compile the audio GUI data needs to be placed:
// Part 3-3: Add a TFT Display

// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=136,65
AudioAnalyzePeak peak2; //xy=348,219
AudioAnalyzePeak peak1; //xy=358,171
AudioOutputI2S i2s1; //xy=380,92
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 0, peak1, 0);
AudioConnection patchCord3(playSdWav1, 1, i2s1, 1);
AudioConnection patchCord4(playSdWav1, 1, peak2, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=155,192
// GUItool: end automatically generated code
 
The Audio card Tutorial uses this display with Audio card - see :: \hardware\teensy\avr\libraries\Audio\examples\Tutorial\Part_3_03_TFT_Display\Part_3_03_TFT_Display.ino

Hopefully that works - with No Touch - then add minimal touch in some fashion and test from one of the XPT examples?

Thanks so much, that absolutely works! I feel like a fool for not figuring that out.
 
Awesome you quite fool'in around and saw it working :) I was sure it should work and didn't want to fool with it.

Interesting to know something called ' MSP3218 ' seems to work like the ILI9341 - same resolution in a 3.2" size with common touch controller. I only see it on Alibaba/Aliexpress … like $6-9
 
I am glad you were able to get that to work. on the 2 projects I have built so far I opted to use one Teensy for the audio shield and another for the ILI9341 display, touch screen and SD card. I also set them up to communicate thru serial ports. I am fairly new to coding in C.
 
Awesome you quite fool'in around and saw it working :) I was sure it should work and didn't want to fool with it.

Interesting to know something called ' MSP3218 ' seems to work like the ILI9341 - same resolution in a 3.2" size with common touch controller. I only see it on Alibaba/Aliexpress … like $6-9

:)Very good to know. Thanks
 
Status
Not open for further replies.
Back
Top