R4 + TFT LCD - running examples ?

I'm trying to make a LCD TFT work with my UNO R4 WIFI.
The screen is from WHADDA : WHADDA

Specifications:
Resolution: 240 RGB (H) x 320 (V)
Display driver: ILI9341 V0.7
Colour depth: 262 000 colours
System interface:
8-bits, 9-bits, 16-bits, 18-bits interface with 8080-I /8080-II series MCU
6-bits, 16-bits, 18-bits RGB interface with graphic controller
3-line / 4-line serial interface
Display mode:
Full colour mode (idle mode OFF): 262 000 colour (selectable colour depth mode by software)
Reduced colour mode (idle mode ON): 8-colour
Operating temperature: -40...85°C

I have downloaded code and library from their site (WMA412) , but it's from 2016, so not supporting the R4. They include several examples, e.g. example 01: Simple Test for UNO:

//Technical support:support@openplatform.cc
// Breakout/Arduino UNO pin usage:
// LCD Data Bit :   7   6   5   4   3   2   1   0
// Uno dig. pin :   7   6   5   4   3   2   9   8
// Uno port/pin : PD7 PD6 PD5 PD4 PD3 PD2 PB1 PB0
// Mega dig. pin:  29  28  27  26  25  24  23  22
#define LCD_RD   A0
#define LCD_WR   A1     
#define LCD_RS   A2        
#define LCD_CS   A3       
#define LCD_REST A4

void Lcd_Writ_Bus(unsigned char d)
{
 PORTD = (PORTD & B00000011) | ((d) & B11111100); 
 PORTB = (PORTB & B11111100) | ((d) & B00000011); 
 *(portOutputRegister(digitalPinToPort(LCD_WR))) &=  ~digitalPinToBitMask(LCD_WR);
 *(portOutputRegister(digitalPinToPort(LCD_WR)))|=  digitalPinToBitMask(LCD_WR);
}


void Lcd_Write_Com(unsigned char VH)  
{   
  *(portOutputRegister(digitalPinToPort(LCD_RS))) &=  ~digitalPinToBitMask(LCD_RS);//LCD_RS=0;
  Lcd_Writ_Bus(VH);
}

void Lcd_Write_Data(unsigned char VH)
{
  *(portOutputRegister(digitalPinToPort(LCD_RS)))|=  digitalPinToBitMask(LCD_RS);//LCD_RS=1;
  Lcd_Writ_Bus(VH);
}
void Lcd_Write_Data_16(unsigned int dat)
{
  *(portOutputRegister(digitalPinToPort(LCD_RS)))|=  digitalPinToBitMask(LCD_RS);//LCD_RS=1;
  Lcd_Writ_Bus(dat>>8);
  Lcd_Writ_Bus(dat);
}

void Lcd_Write_Com_Data(unsigned char com,unsigned char dat)
{
  Lcd_Write_Com(com);
  Lcd_Write_Data(dat);
}
void LCD_WriteReg(unsigned char com,unsigned int dat)
{
  Lcd_Write_Com(com);
  Lcd_Write_Data_16(dat);
 }
void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
  Lcd_Write_Com(0x50);
	Lcd_Write_Data_16(x1);
  
  Lcd_Write_Com(0x51);
	Lcd_Write_Data_16(x2);
        
  Lcd_Write_Com(0x52);
	Lcd_Write_Data_16(y1);
  
	Lcd_Write_Com(0x53);
	Lcd_Write_Data_16(y2);

   Lcd_Write_Com(0x20);
  Lcd_Write_Data_16(x1);
  
  Lcd_Write_Com(0x21);
  Lcd_Write_Data_16(y1);
	Lcd_Write_Com(0x22); 							 
}

void Lcd_Init(void)
{
  digitalWrite(LCD_REST,HIGH);
  delay(50); 
  digitalWrite(LCD_REST,LOW);
  delay(150);
  digitalWrite(LCD_REST,HIGH);
  delay(150);

  digitalWrite(LCD_CS,HIGH);
  digitalWrite(LCD_WR,HIGH);
  digitalWrite(LCD_CS,LOW);  //CS

  LCD_WriteReg(0x0000,0x0001);
  delay(100); //at least 100ms
  LCD_WriteReg( 0x0001,0x0100); // set SS and SM bit
  LCD_WriteReg(0x0010,0x1790);
  LCD_WriteReg(0x0060,0xA700);
  LCD_WriteReg(0x0061,0x0001);
  LCD_WriteReg( 0x0046,0x0002);
  LCD_WriteReg(0x0013,0x8010);
  LCD_WriteReg(0x0012,0x80fe);
  LCD_WriteReg(0x0002,0x0500);
  LCD_WriteReg(0x0003,0x1030);
  
  LCD_WriteReg(0x0030,0x0303);
  LCD_WriteReg(0x0031,0x0303);
  LCD_WriteReg(0x0032,0x0303);
  LCD_WriteReg(0x0033,0x0300);
  LCD_WriteReg(0x0034,0x0003);
  LCD_WriteReg(0x0035,0x0303);
  LCD_WriteReg(0x0036,0x0014);
  LCD_WriteReg(0x0037,0x0303);
  LCD_WriteReg(0x0038,0x0303);
  LCD_WriteReg(0x0039,0x0303);
  LCD_WriteReg(0x003a,0x0300);
  LCD_WriteReg(0x003b,0x0003);
  LCD_WriteReg(0x003c,0x0303);
  LCD_WriteReg(0x003d,0x1400);
  
  LCD_WriteReg(0x0092,0x0200);
  LCD_WriteReg(0x0093,0x0303);
  LCD_WriteReg(0x0090,0x080d);
  
  
  LCD_WriteReg(0x0001, 0x0100); // set SS and SM bit 0100
  LCD_WriteReg( 0x0002, 0x0700); // set 1 line inversion
  LCD_WriteReg(0x0003, 0x1030); // set GRAM write direction and BGR=1.
  LCD_WriteReg(0x0004, 0x0000); // Resize register
  LCD_WriteReg(0x0008, 0x0302); // set the back porch and front porch
  LCD_WriteReg(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0]
  LCD_WriteReg(0x000A, 0x0000);// FMARK function
  LCD_WriteReg(0x000C, 0x0000); // RGB interface setting
  LCD_WriteReg(0x000D, 0x0000); // Frame marker Position
  LCD_WriteReg(0x000F, 0x0000); // RGB interface polarity
  delay(120);
  
  LCD_WriteReg(0x0030,0x0303);
  LCD_WriteReg(0x0031,0x0303);
  LCD_WriteReg(0x0032,0x0303);
  LCD_WriteReg( 0x0033,0x0300);
  LCD_WriteReg(0x0034,0x0003);
  LCD_WriteReg(0x0035,0x0303);
  LCD_WriteReg(0x0036,0x0014);
  LCD_WriteReg(0x0037,0x0303);
  LCD_WriteReg(0x0038,0x0303);
  LCD_WriteReg(0x0039,0x0303);
  LCD_WriteReg(0x003a,0x0300);
  LCD_WriteReg(0x003b,0x0003);
  LCD_WriteReg(0x003c,0x0303);
  LCD_WriteReg(0x003d,0x1400);
  
  LCD_WriteReg(0x0060, 0xA700); // Gate Scan 
  LCD_WriteReg(0x0061, 0x0001); // NDL,VLE, REV
  LCD_WriteReg( 0x006A, 0x0000); // set scrolling line
  LCD_WriteReg(0x0090, 0x0033);
  LCD_WriteReg(0x0095, 0x0110);
  
  LCD_WriteReg(0x00FF, 0x0001);
  LCD_WriteReg(0x00FF, 0x000C);
  LCD_WriteReg(0x00FF, 0x0000);
  delay(100);
  LCD_WriteReg(0x0003,0x1030); // set GRAM write direction and BGR=1.
  LCD_WriteReg(0x0007,0x0173);
  
  delay(50);         
  
}

void H_line(unsigned int x, unsigned int y, unsigned int l, unsigned int c)                   
{	
  unsigned int i,j;
  Lcd_Write_Com(0x02c); //write_memory_start
  digitalWrite(LCD_RS,HIGH);
  digitalWrite(LCD_CS,LOW);
  l=l+x;
  Address_set(x,y,l,y);
  j=l*2;
  for(i=1;i<=j;i++)
  {
    Lcd_Write_Data(c);
  }
  digitalWrite(LCD_CS,HIGH);   
}

void V_line(unsigned int x, unsigned int y, unsigned int l, unsigned int c)                   
{	
  unsigned int i,j;
  Lcd_Write_Com(0x02c); //write_memory_start
  digitalWrite(LCD_RS,HIGH);
  digitalWrite(LCD_CS,LOW);
  l=l+y;
  Address_set(x,y,x,l);
  j=l*2;
  for(i=1;i<=j;i++)
  { 
    Lcd_Write_Data(c);
  }
  digitalWrite(LCD_CS,HIGH);   
}

void Rect(unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int c)
{
  H_line(x  , y  , w, c);
  H_line(x  , y+h, w, c);
  V_line(x  , y  , h, c);
  V_line(x+w, y  , h, c);
}

void Rectf(unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int c)
{
  unsigned int i;
  for(i=0;i<h;i++)
  {
    H_line(x  , y  , w, c);
    H_line(x  , y+i, w, c);
  }
}
int RGB(int r,int g,int b)
{return r << 16 | g << 8 | b;
}
void LCD_Clear(unsigned int j)                   
{	
  unsigned int i,m;
 Address_set(0,0,240-1,320-1);
  //Lcd_Write_Com(0x02c); //write_memory_start
  //digitalWrite(LCD_RS,HIGH);
  digitalWrite(LCD_CS,LOW);


  for(i=0;i<320;i++)
    for(m=0;m<240;m++)
    {
      Lcd_Write_Data(j>>8);
      Lcd_Write_Data(j);

    }
  digitalWrite(LCD_CS,HIGH);   
}

void setup()
{
  for(int p=0;p<10;p++)
  {
    pinMode(p,OUTPUT);
  }
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
  pinMode(A4,OUTPUT);
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  digitalWrite(A4, HIGH);
  Lcd_Init();
 //LCD_Clear(0xf800);
}

void loop()
{  
   LCD_Clear(0xffff);
   LCD_Clear(0x0000);
   LCD_Clear(0xf800);
   LCD_Clear(0x07E0);
   LCD_Clear(0x001F);
  /*   
  for(int i=0;i<1000;i++)
  {
    Rect(random(300),random(300),random(300),random(300),random(65535)); // rectangle at x, y, with, hight, color
  }*/
  
//  LCD_Clear(0xf800);
}

I get the compile error: Compilation error: 'PORTD' was not declared in this scope

It make sense due to the old code. But what can I do? Reading around and watching YouTube tells me that it might not be that easy to make the code work for the R4, since a lot is made for the older models. I don't have knowledge to make it from the bottom, This is my first try with Arduino and coding/building, so I need something running that I can modify. My IDE works, I can download examples for modifying the on-board LED's. Can someone point me to some examples that will work on my set-up with the LCD/TCT panel ?

You've got code written for an ATmega328P. That's not going to work on a Renesas RA4M1 processor.

Your choices are:

  1. You can buy an R3 and use that.
  2. You can rewrite the library to work with an R4.

If you're just starting out, I would strongly suggest option 1.

thanks, I'm not specifically interested in the code examples that came with the display, and that's written for R3, I just want any code example that will test the LCD with R4, so if someone have made this work - I'm interested !

Maybe reading related post https://forum.arduino.cc/t/r4-tft-example-not-compiling/1185231 would help.

Ok, you bought a new display, so creating a new topic is justified.
Example recommended: https://github.com/moononournation/Arduino_GFX/tree/master/examples/PDQgraphicstest

1 Like

Thanks @ZinggJM , Yes I went along with your recommendation in the other post and bought a new LCD display for UNO :slight_smile:
I have tried the PDQgraphicstest... and it compiles and downloads ! Learning curve is steep here... finding my way around board manager and library manager and finally starting up Serial Monitor, rebooting board and I get some output - it's alive:

Benchmark	micro-secs
Screen fill	626943
Text	75502
Pixels	2545926
Lines	2284984
Horiz/Vert Lines	53607
Rectangles (filled)	1302972
Rectangles (outline)	36541
Triangles (filled)	485285
Triangles (outline)	129310
Circles (filled)	229696
Circles (outline)	213745
Arcs (filled)	163814
Arcs (outline)	321619
Rounded rects (filled)	1333455
Rounded rects (outline)	107334
Done!

Now I did expect to see something on the LCD screen, but it just lights up white. Any suggestions as to where to go from here ?

I am amazed by the spirit and help in this community/forum - great motivation for a beginner :hugs:

@kaslan2023, thank you for the feedback! You're welcome.

You could post the constructors you used for the data bus and display.
Are you sure about the controller ILI9341 used in the display?
If you have an Arduino UNO R3, you could check with library MCUFriend_kbv.

I am busy with some new e-paper displays. I will update my fork afterwards, and then re-test.
But you can see the way I use to configure examples with Arduino_GFX here:
https://github.com/ZinggJM/Arduino_GFX/tree/known_tests/examples/PDQgraphicstest
e.g. in known_good.h
I like this library, as it supports most TFT displays I have, and some more I'd like to discover.
And the whole selection of display and configuration can be done in the example or application.

Good Luck!

2 Likes

YES YES YES ! Thanks @ZinggJM , I'm not sure exactly what I'm doing, but the display now works with the R4 !
I opened the PDQgraphicstest in IDE
Opened the

#include "Arduino_GFX_dev_device.h"

Uncommedted this (guess I should make a new #define)

#define ESP32_LCDKIT_SPI

Seeing that the existing code didn't compile, I replaced it with your lines for bus and gfx:

#elif defined(ESP32_LCDKIT_SPI)
#define GFX_DEV_DEVICE ESP32_LCDKIT_SPI
#define GFX_BL 23
//Arduino_DataBus *bus = new Arduino_ESP32SPI(19 /* DC */, 5 /* CS */, 22 /* SCK */, 21 /* MOSI */, 27 /* MISO */);
//Arduino_GFX *gfx = new Arduino_ILI9341(bus, 18 /* RST */, 1 /* rotation */);
Arduino_DataBus *bus = new Arduino_UNOPAR8;
Arduino_GFX *gfx = new Arduino_ILI9341(bus, A4);

An now it works ! now I have a working platform to modify. 1000-thanks.

1 Like

@lkat, please do not hijack a topic that is declared solved! (unless related to the solution).

Please fix the formatting of the code you posted, and then flag your post for moderators to split it to a new topic.
-jz-

Does it work also the Touch function? I'm not able to work with touch panel with R4