cancel
Showing results for 
Search instead for 
Did you mean: 

how to interface stm32F302r8 and SD card and tft display and i want to display image on the display using Ard ide

Sharan
Associate II

i have interfaced stm32 and Sd card performed different operations separately and i have interfaced stm32 and tft wave share(ili9486) display and able to display some graphics separately ,now i am trying to fetch a image from sd card and display it on the LCD but i am able to communicate with only one slave and if Sd card works display does not work and vice -versa and i am using Arduino ide for coding. Please help me to solve this

#include <spi.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Waveshare_ILI9486.h>

#define SD_MOSI_PIN PC12 // MOSI Pin
#define SD_MISO_PIN PC11 // MISO Pin
#define SD_SCLK_PIN PC10 // Clock Pin
#define SD_SS_PIN PB0 // Chip Select or Slave Select Pin

#define TFT_CS_PIN PB6
#define TFT_DC_PIN PA8
#define TFT_RST_PIN PA9

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define BUFFER_SIZE 50

char textBuffer[BUFFER_SIZE];

// Assuming the constructor in your library takes no arguments
Waveshare_ILI9486 tft;

uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << ‌‌ | ((g & 0xFC) << 3) | (b >> 3);
}

static int bufferIndex = 0;
File myFile;

void setup() {
Serial.begin(115200);
Serial.println("Initializing SD card...");

SPI.setMISO(SD_MISO_PIN);
SPI.setMOSI(SD_MOSI_PIN);
SPI.setSCLK(SD_SCLK_PIN);

if (!SD.begin(SD_SS_PIN)) {
Serial.println("SD card initialization failed!");
while (1);
}
Serial.println("SD card initialization done.");

myFile = SD.open("test6.txt");
if (myFile) {
while (myFile.available() && bufferIndex < BUFFER_SIZE - 1) {
char nextChar = myFile.read();
textBuffer[bufferIndex++] = nextChar;
}
textBuffer[bufferIndex] = '\0'; // Null-terminate the string
myFile.close();
} else {
Serial.println("Error opening test6.txt");
}

tft.begin();
}

void loop() {
tft.fillScreen(GREEN);
tft.setTextSize(2);

// Print the content of the buffer on the display
tft.print(textBuffer);
delay(1200);
}
16 REPLIES 16
Techn
Senior II

Do you use the same SPI port for both SD card and TFT? Do they agree to the same SPI type? There may be conflict. May be if possible use different SPI ports

Sharan
Associate II

No they use different spi port ,for tft i am using spi2 and for sd card i am using spi3 and i got the output i am able to read a text and store it in a array and the display it but the thing it is not possible to store image in a array as it is huge bytes of data

so i am thinking of using daisy chain configuration so that i can send data from my sd card to display, but i am getting sd card not initialized 

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Waveshare_ILI9486.h>

#define SD_MOSI_PIN PC12 // MOSI Pin
#define SD_MISO_PIN PC11 // MISO Pin
#define SD_SCLK_PIN PC10 // Clock Pin
#define SD_SS_PIN PB0 // Chip Select or Slave Select Pin

#define TFT_MOSI_PIN PB15 // MOSI Pin for TFT display
#define TFT_MISO_PIN PB14 // MISO Pin for TFT display
#define TFT_SCLK_PIN PB13 // Clock Pin for TFT display
#define TFT_CS_PIN PB6 // Chip Select for TFT display
#define TFT_DC_PIN PA8 // Data/Command Pin for TFT display
#define TFT_RST_PIN PA9 // Reset Pin for TFT display

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define BUFFER_SIZE 50

char textBuffer[BUFFER_SIZE];

// Assuming the constructor in your library takes no arguments
Waveshare_ILI9486 Waveshield;
Adafruit_GFX &tft = Waveshield;

uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << ‌‌ | ((g & 0xFC) << 3) | (b >> 3);
}

static int bufferIndex = 0;
File myFile;

void setup()
{
Serial.begin(115200);
Serial.println("Initializing SD card...");

// Set SPI pin modes for SD card

SPI.setMISO(SD_MISO_PIN);
SPI.setMOSI(SD_MOSI_PIN);
SPI.setSCLK(SD_SCLK_PIN);



// Initialize SD card
if (!SD.begin(SD_SS_PIN)) {
Serial.println("SD card initialization failed!");
while (1);
}
Serial.println("SD card initialization done.");

// Open the file on the SD card
myFile = SD.open("test6.txt");
if (myFile) {
while (myFile.available() && bufferIndex < BUFFER_SIZE) {
char nextChar = myFile.read();
textBuffer[bufferIndex++] = nextChar;
}
myFile.close();
} else {
Serial.println("Failed to open file.");
}



// Set SPI pin modes for TFT display
SPI.setMISO(TFT_MISO_PIN);
SPI.setMOSI(TFT_MOSI_PIN);
SPI.setSCLK(TFT_SCLK_PIN);

// Initialize the display
Waveshield.begin();

tft.fillScreen(BLACK);
tft.setTextSize(2);
tft.setRotation(2);

tft.print(textBuffer);
//delay(1200);
}

void loop()
{
// Fill the screen with green color
}
Techn
Senior II

you can try to set this pin( SD_SS_PIN) as output in the setup and enable it before calling the SD.begin() , since this is not the hardware SS pin for the specified SPI bus.  It may work straight out if you use spi1 pins for sd.

 

Sharan
Associate II

like this?

 


// Set SPI pin modes for SD card
pinMode(SD_SS_PIN, OUTPUT); // Set SD_SS_PIN as output
pinMode(TFT_CS_PIN, OUTPUT); // Set SD_SS_PIN as output
digitalWrite(SD_SS_PIN, LOW); //Disable SD card
digitalWrite(TFT_CS_PIN, LOW); //Disable SD card                                                                                                            

Yes, did you try? 

Techn
Senior II

From the schematics and code examples of the waveshare display, it looks like they are using shared spi, means same spi MISO, MOSI,SCLK, only separate chip select for  SD card and display. From your problem description also that is the inference..

Sharan
Associate II

hi i changed my display to ili9488 and now i am using smt32cubeide 

Sharan
Associate II

could u please help me with this code,

it is compiling but while debugging when i try to press on resume button it will terminate from debug session 

Sharan
Associate II

hi i am trying to interface stm32 with ili9488 please help me with the code