1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| #include <stdio.h>
#include "esp_log.h"
#include "esp_err.h"
#include "driver/spi_master.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_st7789.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_io_spi.h" // 你给的头文件
#include "driver/gpio.h"
#define BL_GPIO 15
#define LCD_H_RES 320
#define LCD_V_RES 480
static const char *TAG = "st7789_demo";
void app_main(void)
{
// 先配置背光引脚为输出并拉高,打开背光
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << BL_GPIO),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io_conf);
gpio_set_level(BL_GPIO, 1); // 1表示打开背光
// 1. SPI 总线配置
spi_bus_config_t bus_config = {
.miso_io_num = -1, // ST7789 不需要读数据,设置为 -1
.mosi_io_num = 10, // MOSI 引脚,根据实际接线修改
.sclk_io_num = 11, // CLK 引脚,根据实际接线修改
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 320 * 240 * 2 + 8,
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &bus_config, SPI_DMA_CH_AUTO));
// 2. LCD 面板 IO 配置(SPI 方式)
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = 13,
.cs_gpio_num = 12,
.pclk_hz = 40 * 1000 * 1000,
.spi_mode = 0,
.trans_queue_depth = 10,
.lcd_cmd_bits = 8, // 通常命令是8位
.lcd_param_bits = 8, // 参数也是8位
.cs_ena_pretrans = 1, // 可选,根据硬件调整
.cs_ena_posttrans = 1, // 可选,根据硬件调整
.flags = {
.dc_high_on_cmd = 0,
.dc_low_on_data = 0,
.dc_low_on_param = 0,
.octal_mode = 0,
.quad_mode = 0,
.sio_mode = 0,
.lsb_first = 0,
.cs_high_active = 0,
},
.on_color_trans_done = NULL,
.user_ctx = NULL,
};
esp_lcd_panel_io_handle_t io_handle = NULL;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &io_handle));
// 3. LCD 面板设备配置
esp_lcd_panel_dev_config_t panel_dev_config = {
.reset_gpio_num = 14,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
.data_endian = LCD_RGB_DATA_ENDIAN_BIG, // 默认即可
.bits_per_pixel = 16,
.flags = {
.reset_active_high = 0,
},
.vendor_config = NULL
};
// 4. 创建 ST7789 面板实例
esp_lcd_panel_handle_t panel_handle = NULL;
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_dev_config, &panel_handle));
// 5. 复位屏幕,初始化显示
ESP_LOGI(TAG, "复位 LCD");
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_LOGI(TAG, "初始化 LCD");
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
// 6. 设置屏幕方向,镜像和偏移
ESP_LOGI(TAG, "设置镜像和偏移");
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, false));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, true));
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, 0, 0));
ESP_LOGI(TAG, "准备绘图");
uint16_t *buf = heap_caps_malloc(240 * 320 * sizeof(uint16_t), MALLOC_CAP_DMA);
for (int i = 0; i < 240 * 320; i++) {
buf[i] = 0xf800; // 红色
}
esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, 240, 320, buf);
ESP_LOGI(TAG, "ST7789 LCD 初始化完成");
}
|