LCD interface intermittent on Mega, works on Uno

Sorry if this is the wrong forum for this. I’ve bought one of those cheap 2.4" TFT LCD resistive touchscreens. as near as I can tell identical to this:

I’ve got it working (at least the screen; haven’t tried the touchscreen or SD slot yet) using the code from this github repository. Now I’m trying to get it to work with an Arduino Mega.

The main difference that I can see is in the port mappings of the data bus. The shield has an 8-bit data bus, with bits[0:7] mapped to digital pins 8, 9, 2, 3, 4, 5, 6, 7. On the Uno, that works out to:

data = (PIND & 0xFC) | (PINB & 0x03);

AFAICT, on the Mega, that works out to:

data[0:7] = ((PORTH >> 5) & 0x03)
   | ((PORTH << 3) & 0xc0)
   | ((PORTG >> 1) & 0x10)
   | ((PORTE >> 2) & 0x0C)
   | ((PORTE << 2) & 0x20)

I’ve rewritten the TFTLCD::read8 and TFTLCD::write8 functions accordingly, as well as TFTLCD::setReadDir and TFTLCD::setWriteDir. The screen doesn’t work, though. I’ve cut the example code down to just resetting the LCD controller and reading the chip ID register, ie:


unsigned id = Tft.readData();

About four times out of five, this obtains the correct ID, 0x9325. But the other time out of five, it returns 0. I initially suspected this was due to the Mega running faster, but a liberal sprinkling of delayMicroseconds(10) has made no difference.

Is there something that I’m missing that is messing with the use of the PWM lines on the Arduino Mega as ordinary digital port pins?

Never mind; this was me making a mess of the bit masking. See this repository for working Mega support.