ESP32-S3-极客液晶屏编程

问题描述 投票:0回答:1

我正在尝试对 ESP32-S3-GEEK 的 LCD 屏幕进行编程,该屏幕具有集成显示屏。不幸的是,即使我密切关注文档,我也无法让它工作。当我将代码刷新到 ESP32 时,它进入循环,但 LCD 屏幕保持空白。

相关原理图

这里是 ESP32-S3-GEEK 原理图的链接,其中提供了引脚映射:ESP32-S3-GEEK 原理图。

https://files.waveshare.com/wiki/ESP32-S3-GEEK/ESP32-S3-GEEK-Schematic1.pdf

代码

这是我用于液晶屏的代码:

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_system.h"

// LCD-related headers

#include "esp_lcd_panel_io.h"      // For panel I/O functions like esp_lcd_new_panel_io_spi
#include "esp_lcd_panel_vendor.h"  // For vendor-specific panels like esp_lcd_new_panel_st7789
#include "esp_lcd_types.h"         // For LCD-specific types (like esp_lcd_panel_handle_t)
#include "hal/lcd_types.h"         // For color space and other LCD-related definitions
#include "driver/gpio.h"
#include "esp_lcd_panel_rgb.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h" // note: this was the mistake i needed this library

// Define the pins based on your provided connections
#define LCD_HOST    SPI2_HOST      // Assuming SPI2 for LCD
#define PIN_NUM_MOSI 16            // GPIO11 for MOSI
#define PIN_NUM_CLK  17            // GPIO12 for Clock
#define PIN_NUM_CS   15            // GPIO10 for Chip Select
#define PIN_NUM_DC   13             // GPIO08 for Data/Command
#define PIN_NUM_RST  14             // GPIO09 for Reset
#define PIN_NUM_BL   12             // GPIO07 for Backlight


void app_main(void)
{

    printf("Initializing LCD...\n"); // print test message

    // SPI bus configuration
    spi_bus_config_t buscfg = {
        .mosi_io_num = PIN_NUM_MOSI,
        .miso_io_num = -1,         // Not used for LCD
        .sclk_io_num = PIN_NUM_CLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        //.max_transfer_sz = EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t), // transfer 80 lines of pixels (assume pixel is RGB565) at most in one SPI transaction
    };
    ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO)); // Enable the DMA feature

    // SPI LCD panel configuration
    esp_lcd_panel_io_handle_t io_handle = NULL;
    esp_lcd_panel_io_spi_config_t io_config = {
        .dc_gpio_num = PIN_NUM_DC,
        .cs_gpio_num = PIN_NUM_CS,
        .pclk_hz = 500 * 1000,  // 1MHz, adjust if needed
        .spi_mode = 0,
        .trans_queue_depth = 10,
    };
    // Attach the LCD to the SPI bus
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));


    // LCD panel device configuration
    esp_lcd_panel_handle_t panel_handle = NULL;
    esp_lcd_panel_dev_config_t panel_config = {
        .reset_gpio_num = PIN_NUM_RST,
        .color_space = ESP_LCD_COLOR_SPACE_RGB,
        .bits_per_pixel = 16,
    };
    // Create LCD panel handle for ST7789, with the SPI IO device handle
    ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));

    // Reset and initialize the LCD panel
    ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));

    // Turn on the backlight
    gpio_set_direction(PIN_NUM_BL, GPIO_MODE_OUTPUT);
    gpio_set_level(PIN_NUM_BL, 1);  // Set to 1 to turn on backlight

    // Dimensions for the red rectangle
    #define RECT_X_START  0    // Starting X coordinate
    #define RECT_Y_START  0    // Starting Y coordinate
    #define RECT_WIDTH    500  // Width of the rectangle
    #define RECT_HEIGHT   500  // Height of the rectangle
    
    // Create a buffer to hold the red rectangle
    size_t num_pixels = RECT_WIDTH * RECT_HEIGHT;
    uint16_t *rectangle_buffer = malloc(num_pixels * sizeof(uint16_t));
    if (rectangle_buffer == NULL) {
        printf("Failed to allocate memory for rectangle buffer\n");
        return; // Handle allocation error
    }

    // Fill the buffer with the color red (RGB565 format)
    for (size_t i = 0; i < num_pixels; i++) {
        rectangle_buffer[i] = 0xF800; // Red color
    }

    // Draw the red rectangle on the LCD
    ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, RECT_X_START, RECT_Y_START, RECT_WIDTH, RECT_HEIGHT, rectangle_buffer));

    printf("LCD Initialization Completed\n");

    vTaskDelay(5000 / portTICK_PERIOD_MS);  // Wait for 1 second

    esp_restart();

}

采取的故障排除步骤

根据原理图验证引脚映射。 尝试更改引脚分配。 查看了文档:ESP-IDF LCD 文档。

这里是我使用的文档:https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/lcd.html#_CPPv425esp_lcd_panel_draw_bitmap22esp_lcd_panel_handle_tiiiiPKv

c esp32 lcd
1个回答
0
投票

ST7789 显示器在重置后有时需要额外的延迟。在调用 esp_lcd_panel_init() 之前添加延迟:

ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
vTaskDelay(100 / portTICK_PERIOD_MS);  // 100 ms delay after reset
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));

还要检查 SPI 时钟速度、内存分配、绘图尺寸等。 如果您需要有关在 Arduino IDE 中以数据模式和 I2C 模式使用 ESP32 和 16x2 LCD 的信息,您可以在此处查看:https://www.theengineeringprojects.com/2022/03/interface-16x2-lcd-with-esp32 -module.html

© www.soinside.com 2019 - 2024. All rights reserved.