ESP32S3学习——spi_lcd_ILI9341

ESP32SPI驱动LCD显示动画及初始化详解

芯片:esp32

开发环境:espidfv4.4

一、例程分析

1)readme中并没有很多信息,解码jpg后显示,经过计算显示一个动画效果,分析了spi配置和lcd驱动,解码部分没看明白

2)spi相关头文件:

  • \ #include "driver/spi_master.h"

  • \ #include "driver/gpio.h"

3)实现效果:一个动图

/* SPI Master example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"

#include "pretty_effect.h"

/*
 This code displays some fancy graphics on the 320x240 LCD on an ESP-WROVER_KIT board.
 This example demonstrates the use of both spi_device_transmit as well as
 spi_device_queue_trans/spi_device_get_trans_result and pre-transmit callbacks.

 Some info about the ILI9341/ST7789V: It has an C/D line, which is connected to a GPIO here. It expects this
 line to be low for a command and high for data. We use a pre-transmit callback here to control that
 line: every transaction has as the user-definable argument the needed state of the D/C line and just
 before the transaction is sent, the callback will set this line to the correct state.
*/


#define LCD_HOST    HSPI_HOST

#define PIN_NUM_MISO 25
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK  19
#define PIN_NUM_CS   22

#define PIN_NUM_DC   21
#define PIN_NUM_RST  18
#define PIN_NUM_BCKL 5



//To speed up transfers, every SPI transfer sends a bunch of lines. This define specifies how many. More means more memory use,
//but less overhead for setting up / finishing transfers. Make sure 240 is dividable by this.
//为了加速传输,每个spi传输都发送一些线,这定义了数量,更多意味着更多的内存
//但是用于传输和完成传输的开销跟梢,确保240可以被整除
#define PARALLEL_LINES 16

/*
 The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct.
*/
// 屏幕信息结构体,命令和数据结构体
typedef struct {
    uint8_t cmd;
    uint8_t data[16];
    uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds.
} lcd_init_cmd_t;

typedef enum {
    LCD_TYPE_ILI = 1,
    LCD_TYPE_ST,
    LCD_TYPE_MAX,
} type_lcd_t;

//Place data into DRAM. Constant data gets placed into DROM by default, which is not accessible by DMA.
//将数据放到dram,常量数据默认情况会被放到drom,drom不可以使用dma
DRAM_ATTR static const lcd_init_cmd_t st_init_cmds[]={
    /* Memory Data Access Control, MX=MV=1, MY=ML=MH=0, RGB=0 */
    {0x36, {(1<<5)|(1<<6)}, 1},
    /* Interface Pixel Format, 16bits/pixel for RGB/MCU interface */
    {0x3A, {0x55}, 1},
    /* Porch Setting */
    {0xB2, {0x0c, 0x0c, 0x00, 0x33, 0x33}, 5},
    /* Gate Control, Vgh=13.65V, Vgl=-10.43V */
    {0xB7, {0x45}, 1},
    /* VCOM Setting, VCOM=1.175V */
    {0xBB, {0x2B}, 1},
    /* LCM Control, XOR: BGR, MX, MH */
    {0xC0, {0x2C}, 1},
    /* VDV and VRH Command Enable, enable=1 */
    {0xC2, {0x01, 0xff}, 2},
    /* VRH Set, Vap=4.4+... */
    {0xC3, {0x11}, 1},
    /* VDV Set, VDV=0 */
    {0xC4, {0x20}, 1},
    /* Frame Rate Control, 60Hz, inversion=0 */
    {0xC6, {0x0f}, 1},
    /* Power Control 1, AVDD=6.8V, AVCL=-4.8V, VDDS=2.3V */
    {0xD0, {0xA4, 0xA1}, 1},
    /* Positive Voltage Gamma Control */
    {0xE0, {0xD0, 0x00, 0x05, 0x0E, 0x15, 0x0D, 0x
ESP32-S3 是一款功能强大的微控制器,支持多种显示接口,包括 SPI、I2C 和并行接口。ILI9341 是一款广泛使用的 TFT LCD 控制器,支持 16 位和 24 位色深显示。在 ESP32-S3 上使用 ILI9341 实现 24 位色深显示需要配置 SPI 接口以及 ILI9341 的寄存器以启用 24 位色深模式。 以下是一个基于 ESP-IDF 的代码示例,展示如何配置 ESP32-S3ILI9341 显示屏配合使用,并启用 24 位色深显示: ```c #include <stdio.h> #include "driver/spi_master.h" #include "esp_lcd_panel_io.h" #include "esp_lcd_panel_ops.h" #include "esp_lcd_panel_vendor.h" #include "esp_log.h" static const char *TAG = "ILI9341_24BIT"; #define LCD_HOST SPI2_HOST #define PIN_NUM_MISO 19 #define PIN_NUM_MOSI 23 #define PIN_NUM_CLK 18 #define PIN_NUM_CS 5 #define PIN_NUM_DC 21 #define PIN_NUM_RST 22 #define PIN_NUM_BCKL 4 void app_main(void) { spi_bus_config_t buscfg = { .mosi_io_num = PIN_NUM_MOSI, .miso_io_num = PIN_NUM_MISO, .sclk_io_num = PIN_NUM_CLK, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 0, }; spi_device_interface_config_t devcfg = { .command_bits = 8, .address_bits = 0, .dummy_bits = 0, .mode = 0, .duty_cycle_pos = 0, .cs_ena_pretrans = 0, .cs_ena_posttrans = 0, .clock_speed_hz = 40 * 1000 * 1000, .input_delay_ns = 0, .spics_io_num = PIN_NUM_CS, .flags = 0, .queue_size = 1, }; // 初始化 SPI 总线 ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO)); // 添加 SPI 设备 spi_device_handle_t spi; ESP_ERROR_CHECK(spi_bus_add_device(LCD_HOST, &devcfg, &spi)); // 配置面板 IO esp_lcd_panel_io_handle_t io_handle = NULL; esp_lcd_panel_io_spi_config_t io_config = { .spi_dev = spi, .dc_gpio_num = PIN_NUM_DC, .cs_gpio_num = PIN_NUM_CS, .pclk_hz = 40 * 1000 * 1000, .lcd_cmd_bits = 8, .lcd_param_bits = 8, .flags = { .dc_low_on_data = 0, .non_dma_mode = 0, }, .on_color_trans_done = NULL, .user_ctx = NULL, }; ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle)); // 创建 ILI9341 面板 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 = 24, // 启用 24 位色深 }; ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle)); // 重置和初始化面板 ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); // 设置显示方向 ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false)); // 设置亮度 ledc_timer_config_t timer_conf = { .speed_mode = LEDC_LOW_SPEED_MODE, .timer_num = LEDC_TIMER_0, .duty_resolution = LEDC_TIMER_13_BIT, .freq_hz = 5000, .clk_src = LEDC_CLK_AUTO, }; ESP_ERROR_CHECK(ledc_timer_config(&timer_conf)); ledc_channel_config_t channel_conf = { .gpio_num = PIN_NUM_BCKL, .speed_mode = LEDC_LOW_SPEED_MODE, .channel = LEDC_CHANNEL_0, .timer_sel = LEDC_TIMER_0, .duty = 4096, // 初始亮度 .hpoint = 0, }; ESP_ERROR_CHECK(ledc_channel_config(&channel_conf)); ESP_LOGI(TAG, "Display initialized successfully"); } ``` ### 说明: 1. **SPI 配置**:使用 SPI2 总线,并配置 MOSI、SCLK、CS、DC、RST 和背光控制引脚。 2. **面板 IO 配置**:通过 `esp_lcd_panel_io_spi_config_t` 结构体配置 SPI 面板 IO,指定时钟频率、命令和参数位数。 3. **ILI9341 面板初始化**:调用 `esp_lcd_new_panel_ili9341` 创建 ILI9341 面板对象,并设置 `bits_per_pixel` 为 24,启用 24 位色深。 4. **背光控制**:使用 LEDC 驱动程序控制背光亮度。 ### 注意事项: - 确保 ILI9341 支持 24 位色深模式,部分 ILI9341 显示屏可能只支持 16 位色深。 - 24 位色深需要更多的内存和带宽,确保 SPI 时钟频率足够高以支持流畅的显示效果。 - 如果使用的是 RGB 接口而非 SPI 接口,需要使用 `esp_lcd_panel_io_rgb_config_t` 进行相应的配置。 ### 相关问题: 1. ESP32-S3 如何通过 RGB 接口驱动 ILI9341 显示屏? 2. 如何在 ESP-IDF 中配置 ILI9341 的帧率? 3. ILI9341 是否支持 32 位色深?如何配置? 4. 使用 ESP32-S3 驱动 ILI9341 时如何优化内存使用? 5. 如何在 ESP32-S3 上使用 LVGL 库驱动 ILI9341 显示屏? [^1]: [TFT_eSPI 官方文档](https://github.com/Bodmer/TFT_eSPI) [^2]: [ESP-IDF LCD 驱动文档](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-peripherals/lcd.html)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值