This is an addition to my post about the Touch Screen Shield for Arduino UNO, so if you’ve landed here, that may be a good place to start for more information.
Several people have asked about using this touch screen shield with the Arduino Mega, but I didn’t have much advice to offer because I didn’t own a Mega until yesterday. Now that I have one in hand and have had the opportunity to spend an evening experimenting, I’ve been successful in getting all of the features working with an Aruino Mega 2560, using the screen as a shield. The disadvantage to this method, as opposed to running wires to a detached screen, is that the data pins span three ports and are in no particular order, meaning several bit shifts and masks must be performed to get the data to/from the screen, slowing things down a bit. As of this writing, I’m focusing solely on using the screen as a shield, not wiring it externally. Given more time, I’ll experiment with both methods and post speed comparisons.
I haven’t done a lot of Arduino programming, so it took me a little while to figure out the whole pin situation in the header files. Once I thought I understood it, I sketched everything out on my whiteboard and started modifying the TFTLCD.cpp source file to use the correct pins during read/write operations on the Mega. Even after I had what I thought should be a working version, however, I wasn’t having any luck. Oddly, the screen suddenly worked after changing one line of code for an equivalent that accomplished the same task. This led me to believe that it might be a timing issue, where a certain code sequence might be running just fast enough to throw off the timing off enough to cause communication to fail. So, I looked at the timing charts for the graphics chip, did some code clean up, and added a couple of short delays in the write operation. With that, graphics became completely functional.
The screen uses analog pins A0-A4 for LCD control and touch screen operations. Luckily, these pins are in the same position on the UNO and the Mega, so the touch screen should work without any modification. One caveat is that the pins must be left in the right state after touch screen operations. Otherwise, the screen won’t function correctly. For the time being, I’m performing that cleanup as follows:
// Clean up pin modes for LCD pinMode(XM, OUTPUT); digitalWrite(XM, LOW); pinMode(YP, OUTPUT); digitalWrite(YP, HIGH); pinMode(YM, OUTPUT); digitalWrite(YM, LOW); pinMode(XP, OUTPUT); digitalWrite(XP, HIGH);
SD Card Reader
I’ve been using the sdfat library for SD card access on the Arduino. One of its built-in features is an ability to use a software-based SPI library. With this, accessing the screen’s built-in SD card reader with the Mega is a breeze, requiring only a few configurations.
Locate the SdFatConfig.h file inside of the SdFat library directory. You’ll find two defines that should be changed to a “1” value to enable software SPI: MEGA_SOFT_SPI and USE_SOFTWARE_SPI. The default pin configurations are already correct.
/** * Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos. * Default pins used are SS 10, MOSI 11, MISO 12, and SCK 13. * Edit Software Spi pins to change pin numbers. * * MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used * on Mega Arduinos. Software SPI works well with GPS Shield V1.1 * but many SD cards will fail with GPS Shield V1.0. */ #define MEGA_SOFT_SPI 1 //------------------------------------------------------------------------------ /** * Define LEONARDO_SOFT_SPI nonzero to use software SPI on Leonardo Arduinos. * Default pins used are SS 10, MOSI 11, MISO 12, and SCK 13. * Edit Software Spi pins to change pin numbers. * * LEONARDO_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used * on Leonardo Arduinos. Software SPI works well with GPS Shield V1.1 * but many SD cards will fail with GPS Shield V1.0. */ #define LEONARDO_SOFT_SPI 0 //------------------------------------------------------------------------------ /** * Set USE_SOFTWARE_SPI nonzero to always use software SPI on AVR. */ #define USE_SOFTWARE_SPI 1 // define software SPI pins so Mega can use unmodified 168/328 shields /** Default Software SPI chip select pin */ uint8_t const SOFT_SPI_CS_PIN = 10; /** Software SPI Master Out Slave In pin */ uint8_t const SOFT_SPI_MOSI_PIN = 11; /** Software SPI Master In Slave Out pin */ uint8_t const SOFT_SPI_MISO_PIN = 12; /** Software SPI Clock pin */ uint8_t const SOFT_SPI_SCK_PIN = 13;
I made some modifications to the TFTLCD.cpp to support using this screen as a shield with the Mega 2560 . I’m providing just the modified source file here for now. The rest of the source can be located under the original article. I plan to clean up the code further and provide a better means of using the screen either as a shield or externally through more appropriate pin assignments using compiler directives. If this is what you were looking for, however, here it is:
Extract and replace original TFTLCD.cpp in Arduino/libraries/TFTLCD folder.