Need help with remapping MCUFriend TFT Display to Arduino Mega Custom breakout board

Hi,
I need help with getting 3.5 inch TFT LCD working properly with Arduino Mega. These LCD modules were bought from some online stores and I don't know the manufacturer's details. They look very similar to the ones explained on LCDwiki page and the label says the driver is ILI9486 (8-bit)

LCD Wiki LCD modules

Since I have already made a PCB with the following pin mapping I need help with getting MCUFriend_bkv working with this pin map. The pin mapping is explained in the below sketch. This is a test program that displays time and some related parameters.

/*
 *  
 * The program is adapted from Adafruit Graphicstest example
 * 
 * By: Harinath R
 * Date: 23May2021
 * 
  */

#include <Adafruit_GFX.h>    // Core graphics library
//#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <MCUFRIEND_kbv.h>


//Control Pin Mapping

#define LCD_RD 10 // LCD Read 
#define LCD_WR 11 // LCD Write 
#define LCD_CD 12 // Command/Data same as LCD_CS
#define LCD_CS 13 // Chip Select 
#define LCD_RESET 14 // reset 


//  digital pins 22 through 29 are connected to LCD D0 to D7


// Assign human-readable names to some common 16-bit color values:
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

MCUFRIEND_kbv tft;

uint16_t ID;

uint8_t clk_hour = 7;
uint8_t clk_minute = 31;
uint8_t clk_second = 59;
uint8_t toggle_flag = 0;
uint8_t is_am_pm = 0;


void setup(void) {
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));


  tft.reset();
  ID = tft.readID();
  Serial.print("TFT ID = 0x");
  Serial.println(ID, HEX);
  
//  tft.begin(0x8357);
//  tft.begin(0x9341);

  ID = 0x9486;
  tft.begin(ID);


  tft.fillScreen(BLACK);
  tft.setRotation(tft.getRotation()+1);

}

void loop(void) {
  
        clk_second++;
        if (clk_second == 60) {
           clk_second = 0;
           clk_minute++;
           if (clk_minute == 60) {
              clk_minute = 0;
              clk_hour++;
              if (clk_hour == 24) {
                clk_hour = 0;
              }
           }

           if (clk_hour < 12) {
            is_am_pm = 0;
           } else {
            is_am_pm = 1;
           }

        }

  if (clk_second == 0) {
    //      tft.fillRect(10, 20, 10, 20, BLUE);
      tft.fillScreen(BLACK);
      tft.drawRect(2, 2,tft.width()-2,tft.height()-4, GREEN);
    
      tft.setCursor(0, 0); 
      tft.setTextSize(2);
      tft.println("  "); 
      tft.setTextColor(CYAN);       
      tft.setTextSize(6);
      tft.print("   ");  
      tft.print(clk_hour);
      tft.print(":");
      if (clk_minute < 10) {
            tft.print("0"); 
       }
      tft.print(clk_minute);  
      tft.print(" ");
      if (is_am_pm == 0) {
           tft.println("am"); 
      } else {
           tft.println("pm"); 
      } 

    tft.setTextSize(2);
    tft.println(" ");  
    tft.setTextSize(4);
    tft.println(" 15Jul2021 Guruvara"); 

//   tft.setTextSize(2);
//    tft.println(" ");
//    tft.setTextSize(4);  
//    tft.println("      Bhanuvara"); 
//    tft.drawFastHLine(2,tft.height()/2-32 , tft.width()-2, GREEN);
    
    tft.setTextSize(2);
    tft.println(" ");    
    tft.setTextSize(4);   
    tft.setTextColor(YELLOW);       
    tft.println("  Shukla Shasti");
    tft.setTextSize(2);
    tft.println(" ");    
    tft.setTextSize(4);
    tft.println("   Aashada Masa");   
    tft.setTextSize(2);
    tft.println(" ");      
    tft.setTextSize(4);
    tft.println("  Plava Samvatsara");       
    tft.setTextSize(2);
    tft.println(" ");
    tft.setTextSize(4);
    tft.setTextColor(MAGENTA); 
//    tft.println("    Shubha Dina");
//    tft.println(" Rahu:  5:04p  6:40p");  
    tft.setTextSize(1);
//    tft.println(" ");
     tft.setTextSize(2);
//    tft.println(" Guli:  3:28p  5:04p");  
    tft.setTextSize(1);
//    tft.println(" ");
    tft.setTextSize(2);
//    tft.println(" Yama: 12:16p  1:52p");      




  }

   if (clk_second > 0) {
      if (toggle_flag == 0) {
          toggle_flag = 1;
        
          tft.setCursor(0, 0); 
          tft.setTextSize(2);
          tft.println("  "); 
          tft.setTextColor(CYAN);       
          tft.setTextSize(6);
          tft.print("   ");  
          tft.print(clk_hour);       
          tft.print(":");
        
        } else {
          toggle_flag = 0;

          tft.setCursor(0, 0); 
          tft.setTextSize(2);
          tft.println("  "); 
          tft.setTextColor(CYAN);       
          tft.setTextSize(6);
          tft.print("   ");  
          tft.print(clk_hour); 
          tft.setTextColor(BLACK);                
          tft.print(":");
       }
   }
  

      delay(1000);
 

}








Based on the below thread, I had modified the mcufriend_special.h as follows:

Arduino Forum discussion on pin reassignment

#elif defined(__AVR_ATmega328P__) && defined(TFT_3P5_PC_BOARD)
#define RD_PORT PORTB
#define RD_PIN  4
#define WR_PORT PORTB
#define WR_PIN  5
#define CD_PORT PORTB
#define CD_PIN  6
#define CS_PORT PORTB
#define CS_PIN  7
#define RESET_PORT PORTJ
#define RESET_PIN  1  


#define write_8(x)                                                        
  {                                                                            
    PORTA = (x);                                                               
                                                                       
  }
#define read_8() ( PINA);                                                                                                                                 \


#define setWriteDir() { DDRA = 0xff;}
#define setReadDir() { DDRA = 0x00;}


#define PIN_LOW(p, b)        (p).OUT &= ~(1<<(b))
#define PIN_HIGH(p, b)       (p).OUT |= (1<<(b))
#define PIN_OUTPUT(p, b)     (p).DIR |= (1<<(b))

------------------

However I still get a blank white screen. I appreciate your help in this regard. 

Attached is the photo of the LCD module.

![Not_Working_Adafruit_LCD|689x381](upload://wFAcRK2oNztR75NTizX4hYneV8Q.jpeg)

From the thread that you linked :

The constructor arguments are all dummies. MCUFRIEND_kbv is designed to run with the shields that God has designed for you.

Just look at the ready-made 2560 SPECIALs that come with the installation e.g. USE_MEGA_8BIT_PROTOSHIELD and USE_MEGA_8BIT_SHIELD

They both use PORTA for the data bus. i.e. D22 - D29
They use different control pins. You just edit these to match the PORT pins on a mega2560 e.g. D10 is PB4, D14 is PJ1

But since you are using an ATmega2560 you need :

#elif defined(__AVR_ATmega2560__) && defined(TFT_3P5_PC_BOARD)

Life is a mystery. If you are designing a custom pcb you would mount the 74LVC245 buffers on your pcb and buy a bare TFT with plain micro-ribbon.

If you are using a genuine Arduino MEGA2560 you would just plug in the Shield as Nature intended.

David.

Hi David,
Thanks a lot for your input. I modified the pin definition as you suggested in the below section.

#elif defined(__AVR_ATmega2560__) && defined(USE_MEGA_8BIT_SHIELD)

and defined USE_MEGA_8BIT_SHIELD in mcufriend_shield.h.

Lo! It just worked. Only thing the display is inverted and I think that is simpler issue to solve.

Some background on why I ended up with this design.

  1. As this is the first time with TFT LCDs I wasn't confident of directly using the bare TFT with micro-ribbon. I plan to do that as the next step.

  2. I had bought a similar TFT module that looks almost similar and it worked well with Adafruit_TFTLCD library, with the following init:

tft.begin(0x8357);

That was the reason I had taken the liberty of remapping the control signals based on other constraints.

However when I bought this module from a different vendor it didn't work. On trial and error I found out that Adafruit_TFTLCD library can be made to work by using the below ID:

tft.begin(0x9341);

However the display flickers.

  1. I had missed the thread until yesterday that MCUfriend_kbv is the recommended library for 3.5 inch display. That's when I started trying out editing the specials.h file.

Once again many many thanks to your immediate response and it is a pleasure to interact with you and read your posts.

I hope to stay in touch.

By editing the below line in the sketch, the display is now proper.

//  tft.setRotation(tft.getRotation()+1);
  tft.setRotation(tft.getRotation()-1);

You have something seriously wrong !!

Install MCUFRIEND_kbv via the IDE Library Manager.
Edit for USE_SPECIAL, USE_MEGA_8BIT_SHIELD

Run graphictest_kbv.ino from the examples.
Or diagnose_TFT_controller.ino from the examples.

Report back if the ID, colors or rotations are wrong.

David.

p.s. the Touch will not work with your control pins. You need XM, YP to be Analog

Hi David,
I ran graphictest_kbv on the setup. Everything worked very well. I will share some snapshots. I was looking for an option to upload video and realized I cannot do it.

Let me know if you notice something not right.

There is no plan to use touch feature.

Thanks,
Hari

I would like to see your includes and constructor in #4 and #5.

And your tft.begin() and tft.setRotation() statements.

But most importantly. Where did you get the library from ?

David.

Library was installed from Arduino IDE library manager. Here are the snapshots.

I had shared the sketch earlier. I haven't changed the sketch and had updated only the Specials file as per your suggestion.





rotation in #4 is 1. i.e LANDSCAPE
rotation in #5 is 3. i.e LANDSCAPE_REV

Hi David,
Sorry I I couldn't figure out the two things you are asking. Here is the graphics test sketch I have used.

graphictest_kbv.ino (17.8 KB)

Here is another snapshot.

Regards,
Hari
Capture6

#11 is rotation(0) i.e. PORTRAIT.

BLUE Penguin is rotation(0) ribbon at bottom
GREEN Penguin is rotation(1) ribbon at right
RED Penguin is rotation(2) ribbon at top
GRAY Penguin is rotation(3) ribbon at left

David.

A note on how I am seeing the display: The micro-ribbon is on the left side, landscape mode.

Here are the observations:

  1. Blue is on the right (portrait, right to left)
  2. Green is at the bottom (landscape, bottom to top)
  3. Red is on the left (portrait, left to right)
  4. Gray is on the top (landscape , top to bottom)

I am thinking everything is working fine. Looks like you are noticing some issue. I am just curious.

Regards,
Hari

You have chosen LANDSCAPE_REV i.e. rotation(3)

You obviously choose the rotation that best suits your application and best suits the USB cable.

I was intrigued by #4 and #5. i.e. different rotations.

David.

Thank you David for all your inputs and scrutiny. I highly appreciate this.

Best Regards,
Hari