Skip to main content
Explorer
June 24, 2024
Question

Interface stm32F302r8 with SD card & TFT display (using Arduino IDE)

  • June 24, 2024
  • 11 replies
  • 10881 views

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);
}

 

    This topic has been closed for replies.

    11 replies

    Graduate II
    June 26, 2024

    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

    SharanAuthor
    Explorer
    June 28, 2024

    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
    }
    Graduate II
    July 1, 2024

    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.

     

    SharanAuthor
    Explorer
    July 1, 2024

    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                                                                                                            

    Graduate II
    July 1, 2024

    Yes, did you try? 

    Graduate II
    July 2, 2024

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

    Graduate II
    July 15, 2024

    did you try this suggestion?

    SharanAuthor
    Explorer
    July 15, 2024

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

    SharanAuthor
    Explorer
    July 15, 2024

    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 

    Graduate II
    July 15, 2024

    try single stepping, first find which functions you are above to step-over.. and pin point where it gets terminated. Then step into the function and identify the illegal instruction... do code read to that area of code.. initially try only to write characters to the screen, then try other advance features. Did you abandon the idea of displaying image from SD card?

    To start with the Arduino path may be easier since the libraries are well tested in most of the cases!!!

    SharanAuthor
    Explorer
    July 16, 2024

    sure i will try ,i want to make some user interface so i thought of using stm32ide as  it has touch gfx

    SharanAuthor
    Explorer
    July 15, 2024

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

    Graduate II
    July 22, 2024

    you can create a .h file with uint16_t array of pixels corresponding R5G6B5 or the needed format for your display(refer to datasheet). There are software such as lcd-image-converter.

     

    void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {

    if((x >= _width) || (y >= _height)) return;

    if((x + w - 1) >= _width) return;

    if((y + h - 1) >= _height) return;

     

    ST7735_Select();

    ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);

    ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);

    ST7735_Unselect();

    }

    SharanAuthor
    Explorer
    July 31, 2024

    hi i am able to display the image ,now i am working on the touch part and i was referring to a youtube video https://youtu.be/g1siKaPox88?si=GkSLNO9t5GLuSTK5  from this video they had given a code and i was able to display but it is showing in gray scale but the touch is working ,in the video they have made a number increment and decrement using buttons in touchgfx designer

    SharanAuthor
    Explorer
    July 31, 2024

    GitHub - maudeve-it/ILI9XXX-XPT2046-STM32: A set of function handling SPI, TFT LED 480x320 or 320x240 touch display controlled by an ILI9488 or ILI9341+XPT2046                                                                                                       this is the github link for the code

     

    SharanAuthor
    Explorer
    November 13, 2024

    how to integrate qt with stm32cubeide for stm32mp157d-dk1 

    (beause when a button is clicked in qt it has to write sine values to dac)

    Graduate II
    November 13, 2024

    Where do you run qt? If it's on pc, you can send message via uart to start the day to display stored sinewave.