基于 ESP32 的 LCD 模块开发
硬件连接
ESP32-DEVKIT-C | LCD 引脚 | 备注 |
---|---|---|
GPIO 25 | 无 | 无 |
GPIO 23 | SDA | 数据 |
GPIO 19 | SCL | 时钟 |
GPIO 22 | CS | 使能 |
GPIO 21 | DC | 数据/命令选择 |
GPIO 18 | RES | 重置 |
GPIO 5 | BLK | 控制背光 |
以上引脚定义也是
ESP-IDF
中SPI
例程中所使用的引脚定义。
通信协议
协议:
SPI
通信芯片:
ST7789V
初始化命令
DRAM_ATTR static const uint8_t LCD_ORIENTATION_VALUES[]={
0x00, // PORTRAIT
0xC0, // PORTRAIT_INVERTED
0x70, // LANDSCAPE
0xA0 // LANDSCAPE_INVERTED
};
static const char *LCD_ORIENTATION_TAGS[]={
"PORTRAIT", // 纵向
"PORTRAIT_INVERTED", // 纵向(颠倒)
"LANDSCAPE", // 横向
"LANDSCAPE_INVERTED" // 横向(颠倒)
};
#define LCD_WIDTH 240 // LCD 宽度像素
#define LCD_HEIGHT 240 // LCD 高度像素
#define VIEW_ORIENTATION 0 // LCD 方向 取值范围 0-3
typedef struct {
uint8_t cmd;
uint8_t data[16];
uint8_t databytes;
} lcd_init_cmd_t;
DRAM_ATTR static const lcd_init_cmd_t LCD_INIT_CMDS[]={
//
{0x36, {LCD_ORIENTATION_VALUES[VIEW_ORIENTATION]}, 1},
/* Interface Pixel Format, 16bits/pixel for RGB/MCU interface */
{0x3A, {0x05}, 1},
/* Porch Setting */
{0xB2, {0x0c, 0x0c, 0x00, 0x33, 0x33}, 5},
/* Gate Control, Vgh=13.65V, Vgl=-10.43V */
{0xB7, {0x35}, 1},
/* VCOM Setting, VCOM=1.35V */
{0xBB, {0x32}, 1},
/* VDV and VRH Command Enable, enable=1 */
{0xC2, {0x01}, 1},
/* VRH Set, Vap=4.4+... */
{0xC3, {0x15}, 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,0x08,0x0E,0x09,0x09,0x05,0x31,0x33,0x48,0x17,0x14,0x15,0x31,0x34}, 14},
/* Negative Voltage Gamma Control */
{0xE1, {0xD0,0x08,0x0E,0x09,0x09,0x15,0x31,0x33,0x48,0x17,0x14,0x15,0x31,0x34}, 14},
/* Sleep Out */
{0x11, {0}, 0x80},
/* Display On */
{0x29, {0}, 0x80},
{0, {0}, 0xff}
};
其他
其他的代码基本上可以直接采用网上的例程。但是在写颜色填充时,发现必须要对16bit
的颜色数据取反后才可以正确的显示颜色。猜测是配置表出问题了。如果有哪位知道,请于下方留言,不胜感激。
/**
* @brief 填充颜色
*
* @param spi Param doc
* @param start_x Param doc
* @param start_y Param doc
* @param end_x Param doc
* @param end_y Param doc
* @param color Param doc
*/
void lcd_fill(spi_device_handle_t spi,uint16_t start_x,uint16_t start_y,uint16_t end_x,uint16_t end_y,uint16_t color){
uint16_t i,j;
uint8_t data[2];
data[0]=(~color)>>8;
data[1]=~color;
lcd_set_address(spi,start_x,start_y,end_x,end_y);//设置显示范围
for(i=start_y;i<end_y;i++)
{
for(j=start_x;j<end_x;j++)
{
lcd_data(spi,data,2);
}
}
}