Problem to use both the touch panel function of the LCD and the built-in SD card function

I am trying to use both the touch panel function of the LCD and the built-in SD card function at the same time.

Device used
Arduino: Mega2560
TFT LCD: MAR3953 (IC ST7796S)

The problem is that when the SD card is activated, the Touch value is recognized as 65536 incorrectly.
If the SD card is not activated in the code, the touch value comes out normally.
Is the above TFT LCD unable to use the SD card function and the Touch Panel function at the same time?
Or is it because I don't know how to handle it?
Please let me know how to solve it.

#include <LCDWIKI_GUI.h> //Core graphics library
#include <LCDWIKI_KBV.h> //Hardware-specific library
#include <LCDWIKI_TOUCH.h> //touch screen library
#include <SD.h> // SD card
#include <SPI.h> // for BMP
#define SD_CS_PIN 48

LCDWIKI_KBV my_lcd(ST7796S,40,38,39,43,41); //model,cs,cd,wr,rd,reset
LCDWIKI_TOUCH my_touch(53,52,50,51,44); //tcs,tclk,tdout,tdin,tirq

uint16_t px,py;

#define GREEN 0x07E0
#define YELLOW 0xFFE0

void setup()
{
   pinMode(SD_CS_PIN, OUTPUT);
   Serial. begin(9600);
   my_lcd.Init_LCD();
   my_touch.TP_Init(1,my_lcd.Get_Display_Width(),my_lcd.Get_Display_Height());
   my_lcd.Set_Rotation(3);
   my_touch.TP_Set_Rotation(1);

// if (!SD.begin(SD_CS_PIN)) {
// Serial.println("initialization failed!");
// while(1);
// }

   my_lcd. Fill_Screen(GREEN);
   my_lcd.Fill_Rect(0, 280, 480, 320,YELLOW);
}

void loop()
{
       my_touch.TP_Scan(0);
       if (my_touch.TP_Get_State()&TP_PRES_DOWN)
       {
           px = my_touch.x;
           py = my_touch.y;
           Serial.print("px = ");
           Serial. print(px);
           Serial.print(" py = ");
           Serial. println(py);
           delay(200);

You can't use this pins.

50, 51, 52 and 53 are SPI pins used by SD card.

Deleted

Thank you for answer.
TFT LCD is shield type for Mega2560.
So the NC PINs are 43, 44, 46, 47, and 48.
Is it possible to set my_touch as an NC PIN?

LCDWIKI_TOUCH my_touch(53,52,50,51,44); //tcs,tclk,tdout,tdin,tirq

Well, I was wrong, this model is different from others with 2x20 pin header.

Check the comments about the pinout in this code.

More informations and examples you can find here.

/***********************************************************************************
*This program is a demo of clearing screen to display red,green,blue.
*This demo was made for LCD modules with 8bit or 16bit data port.
*This program don't need to rely on any libraries and Can run directly.

* File                : simple_test_ST7796S.ino
* Hardware Environment: Arduino Mega2560
* Build Environment   : Arduino

*Set the pins to the correct ones for your development shield or breakout board.
*This demo use the BREAKOUT BOARD only and use these 16 data lines to the LCD,
//pin usage as follow:
//             CS  CD  WR  RD  RST  D0  D1  D2  D3  D4  D5  D6  D7  D8  D9  D10  D11  D12  D13  D14  D15 
//Arduino Mega 40  38  39  43  41   37  36  35  34  33  32  31  30  22  23  24   25   26   27   28   29
//             TP_IRQ  MOSI  MISO  TP_CS  EX_CLK
//Arduino Mega   44    51     50    53      52

//when using the BREAKOUT BOARD only and using these 8 data lines to the LCD,
//pin usage as follow:
//             CS  CD  WR  RD  RST  D0  D1  D2  D3  D4  D5  D6  D7  D8  D9  D10  D11  D12  D13  D14  D15 
//Arduino Mega 40  38  39  43  41   37  36  35  34  33  32  31  30  /   /    /    /    /    /    /    /
//             TP_IRQ  MOSI  MISO  TP_CS  EX_CLK
//Arduino Mega   44    51     50    53      52

*Remember to set the pins to suit your display module!
*
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
**********************************************************************************/
#define LCD_MODE 1    //1-8bit,0-16bit

#define LCD_RD   43
#define LCD_WR   39     
#define LCD_RS   38        
#define LCD_CS   40       
#define LCD_REST 41

void Lcd_Writ_Bus(unsigned int d)
{
  #if LCD_MODE
    //PORTC = d >> 8;
    PORTC = d;
  #else 
    PORTA = d >> 8;
    PORTC = d;
  #endif 	
 *(portOutputRegister(digitalPinToPort(LCD_WR))) &=  ~digitalPinToBitMask(LCD_WR);
 *(portOutputRegister(digitalPinToPort(LCD_WR)))|=  digitalPinToBitMask(LCD_WR);
}

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

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

void Lcd_Write_Com_Data(unsigned int com,unsigned int dat)
{
  Lcd_Write_Com(com);
  Lcd_Write_Data(dat);
}

void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
  Lcd_Write_Com(0x2a);
	Lcd_Write_Data(x1>>8);
	Lcd_Write_Data(x1);
	Lcd_Write_Data(x2>>8);
	Lcd_Write_Data(x2);
        Lcd_Write_Com(0x2b);
	Lcd_Write_Data(y1>>8);
	Lcd_Write_Data(y1);
	Lcd_Write_Data(y2>>8);
	Lcd_Write_Data(y2);
	Lcd_Write_Com(0x2c); 							 
}

void Lcd_Init(void)
{
  digitalWrite(LCD_REST,HIGH);
  delay(5); 
  digitalWrite(LCD_REST,LOW);
  delay(15);
  digitalWrite(LCD_REST,HIGH);
  delay(15);

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

    Lcd_Write_Com(0xF0);
  Lcd_Write_Data(0xC3);
  Lcd_Write_Com(0xF0);
  Lcd_Write_Data(0x96);
  Lcd_Write_Com(0x36);
  Lcd_Write_Data(0x68);  
  Lcd_Write_Com(0x3A);
  Lcd_Write_Data(0x05);  
  Lcd_Write_Com(0xB0);
  Lcd_Write_Data(0x80);  
  Lcd_Write_Com(0xB6);
  Lcd_Write_Data(0x20);
  Lcd_Write_Data(0x02);  
  Lcd_Write_Com(0xB5);
  Lcd_Write_Data(0x02);
  Lcd_Write_Data(0x03);
  Lcd_Write_Data(0x00);
  Lcd_Write_Data(0x04);
  Lcd_Write_Com(0xB1);
  Lcd_Write_Data(0x80);  
  Lcd_Write_Data(0x10);  
  Lcd_Write_Com(0xB4);
  Lcd_Write_Data(0x00);
  Lcd_Write_Com(0xB7);
  Lcd_Write_Data(0xC6);
  Lcd_Write_Com(0xC5);
  Lcd_Write_Data(0x24);
  Lcd_Write_Com(0xE4);
  Lcd_Write_Data(0x31);
  Lcd_Write_Com(0xE8);
  Lcd_Write_Data(0x40);
  Lcd_Write_Data(0x8A);
  Lcd_Write_Data(0x00);
  Lcd_Write_Data(0x00);
  Lcd_Write_Data(0x29);
  Lcd_Write_Data(0x19);
  Lcd_Write_Data(0xA5);
  Lcd_Write_Data(0x33);
  Lcd_Write_Com(0xC2);
  Lcd_Write_Com(0xA7);
  
  Lcd_Write_Com(0xE0);
  Lcd_Write_Data(0xF0);
  Lcd_Write_Data(0x09);
  Lcd_Write_Data(0x13);
  Lcd_Write_Data(0x12);
  Lcd_Write_Data(0x12);
  Lcd_Write_Data(0x2B);
  Lcd_Write_Data(0x3C);
  Lcd_Write_Data(0x44);
  Lcd_Write_Data(0x4B);
  Lcd_Write_Data(0x1B);
  Lcd_Write_Data(0x18);
  Lcd_Write_Data(0x17);
  Lcd_Write_Data(0x1D);
  Lcd_Write_Data(0x21);

  Lcd_Write_Com(0XE1);
  Lcd_Write_Data(0xF0);
  Lcd_Write_Data(0x09);
  Lcd_Write_Data(0x13);
  Lcd_Write_Data(0x0C);
  Lcd_Write_Data(0x0D);
  Lcd_Write_Data(0x27);
  Lcd_Write_Data(0x3B);
  Lcd_Write_Data(0x44);
  Lcd_Write_Data(0x4D);
  Lcd_Write_Data(0x0B);
  Lcd_Write_Data(0x17);
  Lcd_Write_Data(0x17);
  Lcd_Write_Data(0x1D);
  Lcd_Write_Data(0x21);

  Lcd_Write_Com(0X36);
  Lcd_Write_Data(0x08);
  Lcd_Write_Com(0xF0);
  Lcd_Write_Data(0xC3);
  Lcd_Write_Com(0xF0);
  Lcd_Write_Data(0x69);
  Lcd_Write_Com(0X13);
  Lcd_Write_Com(0X11);
  Lcd_Write_Com(0X29);
}

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,320,480);
  //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<480;m++)
    {
      //Lcd_Write_Data(j>>8);
      #if LCD_MODE
        Lcd_Write_Data(j>>8);
      #endif
      Lcd_Write_Data(j);
    }
  digitalWrite(LCD_CS,HIGH);   
}

void setup()
{/*
  for(int p=0;p<10;p++)
  {
    pinMode(p,OUTPUT);
  }*/
  DDRA |= 0xFF;
  DDRC |= 0xFF;
  pinMode(38,OUTPUT);
  pinMode(39,OUTPUT);
  pinMode(40,OUTPUT);
  pinMode(41,OUTPUT);
  pinMode(43,OUTPUT);
  digitalWrite(38, HIGH);
  digitalWrite(39, HIGH);
  digitalWrite(40, HIGH);
  digitalWrite(41, HIGH);
  digitalWrite(43, HIGH);
  Lcd_Init();
 //LCD_Clear(0xf800);
}

void loop()
{  
   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);
}

Hi @jung-sunghwa!

Please test this example for SD card only and tell me what you can get on serial monitor.

#include <SdFat.h>

const byte SelectSlave_SD = 48;
const byte SelectSlave_TOUCH = 53;

SdFat SD;
SdFile file;


float temp;
float tempe;

void setup() {
  int16_t n;
  char buf[8];

  pinMode(SelectSlave_TOUCH, OUTPUT);
  digitalWrite(SelectSlave_TOUCH, HIGH);

  Serial.begin(9600);

  SD.begin(SelectSlave_SD, SPI_HALF_SPEED);

  while (!Serial) {
    ;  // wait for serial.
  }

  if (file.open("LOGTDIA.TXT", O_CREAT | O_APPEND | O_WRITE)) {
    Serial.println("Writing...");
    Serial.println();

    for (temp = 27.5; temp > 22.0; temp -= 0.15) {
      file.print(temp);
      file.write((uint8_t*)"\0", 1);
      file.write((uint8_t*)"\r\n", 2);
    }

    for (tempe = 22.5; tempe < 28.0; tempe += 0.15) {
      file.print(tempe);
      file.write((uint8_t*)"\0", 1);
      file.write((uint8_t*)"\r\n", 2);
    }
    file.close();
    Serial.println("Done!");
    Serial.println();
  } else {
    Serial.println("Can't open.");
  }

  if (file.open("LOGTDIA.TXT", O_READ)) {
    Serial.println("Reading...");
    Serial.println();
    delay(5000);
    while ((n = file.read(buf, sizeof(buf))) > 0) {
      Serial.println(buf);
    }
    file.close();
    Serial.println();
    Serial.print("Finished!");
  }
}
void loop() {
}

Best regards.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.