Programming SD-card on UNO and MEGA

Good morning ,
Looking for some guidance or manual info concerning the programming of a sketch for a SD-card .I use a TFT 2.4" or 3.5" and an UNO or MEGA.
As I like to study the matter I'm more interrested in some manual or info .

Marco , thanks for your info.

If the display and the SD card module are going to share the SPI bus, you will need to be careful which SD card module that you buy. Many SD card modules use a level shifter to interface the 3.3V card to the 5V Uno or Mega. They run the MISO line through the level shifter and as a result the MISO line is not released properly so those modules do not play well with others. Look for a SD module that has the MISO line that bypasses the level shifter like the Adafruit SD module.

The SD library has several example sketches to help learn how to use the library. The display library that works with your display should have examples, too.

thanks for your info .

The SD-card I have is just fine and OK .
That's not the problem . I had a lot of help from David and with his prg MCUFRIEND all's working just good .
But this prg is to big for me and I don't understand it like I should so I'll try to learn more about and therefore I'm looking for some instructions (teaching )manuals .
I like to write a simple sketch showing a picture with the help of a SD-card on a 2.4" and 3.5" tft screen.

Marco

As you are using a MCUfriend i am assuming an SD cardreader as part of the shield, try this code:

/*
  TS_Listfiles

 This example shows how print out the files in a
 directory on a SD card onto a TFT touchscreen

 The circuit:
 * SD card attached to SPI bus , CS - pin 10

 */


#include <Adafruit_GFX.h>    // Core graphics library

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;       // hard-wired for UNO shields anyway.
#include <TouchScreen.h>

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

 
#include <SPI.h>
#include <SD.h>

File root;
#define CS_PIN  10
uint32_t allbytes=0;
uint16_t allfiles=0;

// most mcufriend shields use these pins and Portrait mode:
uint8_t YP = A1;  // must be an analog pin, use "An" notation!
uint8_t XM = A2;  // must be an analog pin, use "An" notation!
uint8_t YM = 7;   // can be a digital pin
uint8_t XP = 6;   // can be a digital pin
uint8_t SwapXY = 1;

uint16_t TS_LEFT = 189;
uint16_t TS_RT = 920;
uint16_t TS_TOP = 215;
uint16_t TS_BOT = 912;


// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;

#define MINPRESSURE 20
#define MAXPRESSURE 1000

#define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}


uint16_t identifier;
uint8_t orientation = 1;    //Landscape
uint8_t linecounter=0;


// 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

uint16_t BgColor=BLACK;
uint16_t TextColor=WHITE;



void setup() {

  //---------------------------- Initialze Touchscreen -------------------------
  
  tft.begin(9600);
  tft.reset();
  identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(orientation);  // we use orientation here to set the TFT rotation
  tft.fillScreen(BgColor);
  tft.setTextColor(TextColor);
  TS_Rotation(orientation);  
  ts = TouchScreen(XP, YP, XM, YM, 300);     //  call the constructor  

  if (SdCardOpen(CS_PIN)) SdCardDirectory(); 
  
}

void loop() {

}

void printDirectory(File dir, uint8_t numtabs) {
  while (true) {
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    if (linecounter>19) {
      tft.setCursor(200,linecounter*10);
      tft.print("more ?");
      CheckTftButton(200,300,200,220,true); // wait for a press in that area
      linecounter=1;
      tft.fillRect(0, 20, 300,200 , BgColor);
    }
    linecounter++;
    tft.setCursor(numtabs*20,linecounter*10);
    tft.print(entry.name());
    if (entry.isDirectory()) {
      tft.print("/");
      printDirectory(entry, numtabs + 1); // it's recursive nesting function calling itself...
    } else {
      allfiles++;  // it's a file, files have sizes, directories do not
      tft.setCursor(100+numtabs*20,linecounter*10);
      allbytes=allbytes+entry.size();
      tft.print(entry.size(), DEC);
    }
    entry.close();
  }
}

void CheckTftButton(uint16_t minx, uint16_t maxx, uint16_t miny, uint16_t maxy, bool wait)
{
  uint16_t tsx,tsy;
  float multx,multy;
  bool once=!wait;  
  while (wait || once) {
    once=false;
    tp = ts.getPoint();
    pinMode(XM, OUTPUT);  // reset the pins after use.
    pinMode(YP, OUTPUT);
    pinMode(XP, OUTPUT);
    pinMode(YM, OUTPUT);
    if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue; // no press within range

    if (TS_LEFT>TS_RT) {
      tsx=tp.x-TS_RT;
      multx=(float)tsx / (float) (TS_LEFT-TS_RT);
      tsx= (uint16_t) (multx*320);
    }
    else {
      tsx=tp.x-TS_LEFT;
      multx=(float)tsx / (float) (TS_RT-TS_LEFT);
      tsx= 320- (uint16_t) (multx*320);
    }
    if (TS_BOT>TS_TOP) {
      tsy=tp.y-TS_TOP;
      multy=(float) tsy / (float) (TS_BOT-TS_TOP);
      tsy= 240-(uint16_t) (multy*240);
    }
    else {
      tsy=tp.y-TS_BOT;
      multy=(float) tsy / (float) (TS_TOP-TS_BOT);
      tsy= (uint16_t) (multy*240);
    }
    if (tsx > minx && tsx < maxx  && tsy > miny && tsy < maxy) break;
  }
}

void TS_Rotation(uint8_t rotation) { 
  uint16_t tmp; 
  switch (rotation) {      // adjust for different aspects
      case 0:   break;        // no change,  calibrated for PORTRAIT
      case 1:   tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp;  break;
      case 2:   SWAP(TS_LEFT, TS_RT);  SWAP(TS_TOP, TS_BOT); break;
      case 3:   tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp;  break;
  }
}

bool SdCardOpen(uint8_t cs_pin) {
  tft.print("Initializing SD card...      ");
  if (!SD.begin(CS_PIN)) {
    tft.println("initialization failed!");
    return false;
  }
  tft.println("initialization done.");
  linecounter=1;
  return true;
}

void SdCardDirectory() {
  root = SD.open("/");
  printDirectory(root, 0);
  linecounter=linecounter+2;
  tft.setCursor(20,linecounter*10);
  tft.print(allfiles, DEC);
  tft.print(" Files, ");
  tft.print(allbytes, DEC);
  tft.print(" Bytes");
}

void PrintCoordinatesAt(uint8_t x, uint8_t y, uint16_t cx, uint16_t cy) {
    tft.fillRect(x, y, x+70,y+20 , BgColor);
    tft.setCursor(x,y);
    tft.print("tp.x=" + String(cx) + "   ");
    tft.setCursor(x,y+10);
    tft.print("tp.y=" + String(cy) + "   ");
}

as a file lister, there are a few libs for SD card around, and on an UNO the combination of SD & TFT will make you run out of memory fairly soon. The libraries also provide good examples.

Thanks Deva for the info and help.
I'll try this out this evening and will come back

Marco

As I will not use ' touchscreen ' , I need to delete all what's about the function ' touchscreen ' , I think ?
Marco

in that case i would just look through all the SD library examples and print to the serial-port instead.