mirror of
https://github.com/No-Chicken/Power-Pico.git
synced 2026-04-03 13:02:36 +08:00
更新LCD刷新模式
This commit is contained in:
@@ -15,7 +15,7 @@ extern osSemaphoreId_t DMA_SemaphoreHandle;
|
||||
void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
|
||||
{
|
||||
u16 i,j;
|
||||
LCD_Address_Set(xsta+OFFSET_X, ysta+OFFSET_Y,xend-1+OFFSET_X, yend-1+OFFSET_Y);//设置显示范围
|
||||
LCD_Address_Set(xsta, ysta,xend-1, yend-1);//设置显示范围
|
||||
for(i=ysta;i<yend;i++)
|
||||
{
|
||||
for(j=xsta;j<xend;j++)
|
||||
@@ -32,23 +32,49 @@ void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
|
||||
color pixel map
|
||||
返回值: 无
|
||||
******************************************************************************/
|
||||
void LCD_Color_Render(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 *color_p)
|
||||
void LCD_Color_Render_Async(u16 xsta, u16 ysta, u16 xend, u16 yend, u16 *color_p)
|
||||
{
|
||||
u16 i,j,width,height;
|
||||
width = xend-xsta+1;
|
||||
height = yend-ysta+1;
|
||||
uint32_t size = width * height;
|
||||
// 1. 设置地址 (命令是 8 位的,此时 SPI 必须是 8 位模式)
|
||||
LCD_Address_Set(xsta, ysta, xend, yend);
|
||||
// 计算总数据量
|
||||
uint32_t count = (xend - xsta + 1) * (yend - ysta + 1);
|
||||
// 2. 安全切换到 16-bit 模式
|
||||
// 必须先关 SPI,改寄存器,再开 SPI
|
||||
__HAL_SPI_DISABLE(&hspi2);
|
||||
hspi2.Instance->CR1 |= SPI_CR1_DFF; // 置位 DFF,启用 16 位数据帧
|
||||
__HAL_SPI_ENABLE(&hspi2);
|
||||
// 3. 启动 DMA 传输
|
||||
HAL_SPI_Transmit_DMA(&hspi2, (uint8_t*)color_p, count);
|
||||
}
|
||||
|
||||
LCD_Address_Set(xsta+OFFSET_X, ysta+OFFSET_Y, xend+OFFSET_X, yend+OFFSET_Y);
|
||||
static void *lcd_ready_cb = NULL;
|
||||
|
||||
hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
|
||||
hspi2.Instance->CR1|=SPI_CR1_DFF;
|
||||
HAL_SPI_Transmit_DMA(&hspi2,(uint8_t*)color_p,size);
|
||||
while(__HAL_DMA_GET_COUNTER(&hdma_spi2_tx)!=0);
|
||||
// 设置通知的回调函数
|
||||
void LCD_Set_Flush_Complete_Callback(void *cb)
|
||||
{
|
||||
// 保存回调指针
|
||||
lcd_ready_cb = cb;
|
||||
}
|
||||
|
||||
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi2.Instance->CR1&=~SPI_CR1_DFF;
|
||||
// DMA 传输完成中断回调
|
||||
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
if (hspi->Instance == SPI2)
|
||||
{
|
||||
// 1. 死等 SPI 总线发完最后 1 bit
|
||||
while (hspi->Instance->SR & SPI_FLAG_BSY);
|
||||
|
||||
// 2. 安全切回 8-bit 模式 (为下一次发送命令做准备)
|
||||
__HAL_SPI_DISABLE(hspi);
|
||||
hspi->Instance->CR1 &= ~SPI_CR1_DFF; // 清除 DFF,回退到 8 位
|
||||
__HAL_SPI_ENABLE(hspi);
|
||||
|
||||
// 3. 通知 LVGL 渲染结束,执行回调函数
|
||||
if (lcd_ready_cb) {
|
||||
LCD_CallbackFunc_t func = (LCD_CallbackFunc_t)lcd_ready_cb;
|
||||
func();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
#define __LCD_H
|
||||
#include "sys.h"
|
||||
|
||||
typedef void (*LCD_CallbackFunc_t)(void); // 定义LCD完成DMA刷新回调函数类型
|
||||
|
||||
void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//指定区域填充颜色
|
||||
void LCD_Color_Render(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 *color);
|
||||
void LCD_Set_Flush_Complete_Callback(void *cb);
|
||||
void LCD_Color_Render_Async(u16 xsta, u16 ysta, u16 xend, u16 yend, u16 *color_p);
|
||||
void LCD_DrawPoint(u16 x,u16 y,u16 color);//在指定位置画一个点
|
||||
void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//在指定位置画一条线
|
||||
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//在指定位置画一个矩形
|
||||
|
||||
@@ -112,13 +112,13 @@ void LCD_WR_REG(u8 dat)
|
||||
******************************************************************************/
|
||||
void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2)
|
||||
{
|
||||
LCD_WR_REG(0x2a);//列地址设置
|
||||
LCD_WR_DATA(x1);
|
||||
LCD_WR_DATA(x2);
|
||||
LCD_WR_REG(0x2b);//行地址设置
|
||||
LCD_WR_DATA(y1);
|
||||
LCD_WR_DATA(y2);
|
||||
LCD_WR_REG(0x2c);//储存器写
|
||||
LCD_WR_REG(0x2a);//列地址设置
|
||||
LCD_WR_DATA(x1 + OFFSET_X);
|
||||
LCD_WR_DATA(x2 + OFFSET_X);
|
||||
LCD_WR_REG(0x2b);//行地址设置
|
||||
LCD_WR_DATA(y1 + OFFSET_Y);
|
||||
LCD_WR_DATA(y2 + OFFSET_Y);
|
||||
LCD_WR_REG(0x2c);//储存器写
|
||||
}
|
||||
|
||||
|
||||
@@ -230,14 +230,20 @@ void LCD_Init(void)
|
||||
LCD_RES_Set();
|
||||
HAL_Delay(100);
|
||||
|
||||
// 1. 退出睡眠模式 (Exit Sleep Mode)
|
||||
LCD_WR_REG(0x11);
|
||||
HAL_Delay(120);
|
||||
LCD_WR_REG(0x36);
|
||||
|
||||
// 2. 设置显示方向 (Memory Data Access Control)
|
||||
LCD_SetRotation(LCD_ROTATION);
|
||||
|
||||
// 3. 设置像素格式 (Interface Pixel Format)
|
||||
// 0x55 表示 16位色 (RGB565)。如果设置为0x66 (18位色),颜色会不正确。
|
||||
LCD_WR_REG(0x3A);
|
||||
LCD_WR_DATA8(0x05);
|
||||
LCD_WR_DATA8(0x55);
|
||||
|
||||
// 4. Porch 设置 (时序控制)
|
||||
// 控制行扫描的前后肩时间,通常不需要修改。
|
||||
LCD_WR_REG(0xB2);
|
||||
LCD_WR_DATA8(0x0C);
|
||||
LCD_WR_DATA8(0x0C);
|
||||
@@ -245,31 +251,49 @@ void LCD_Init(void)
|
||||
LCD_WR_DATA8(0x33);
|
||||
LCD_WR_DATA8(0x33);
|
||||
|
||||
// 5. Gate 控制 (Gate Control)
|
||||
// 【色彩关键】设置VGH和VGL电压,影响屏幕的对比度和上下视角。
|
||||
// 0x35是一个比较通用的值。如果屏幕有垂直方向的串扰或闪烁,可以尝试修改这个值。
|
||||
LCD_WR_REG(0xB7);
|
||||
LCD_WR_DATA8(0x35);
|
||||
|
||||
// 6. VCOM 设置 (VCOM Setting)
|
||||
// 【色彩关键】这是非常重要的参数,直接影响显示质量,特别是对比度和闪烁。
|
||||
// 0x19 是一个参考值,可以在 0x10 到 0x30 之间尝试,找到最稳定的值。
|
||||
LCD_WR_REG(0xBB);
|
||||
LCD_WR_DATA8(0x19);
|
||||
|
||||
// 7. LCM 控制 (LCM Control)
|
||||
LCD_WR_REG(0xC0);
|
||||
LCD_WR_DATA8(0x2C);
|
||||
|
||||
// 8. VDV 和 VRH 命令使能
|
||||
LCD_WR_REG(0xC2);
|
||||
LCD_WR_DATA8(0x01);
|
||||
|
||||
// 9. VRH 设置 (VRH Set)
|
||||
// 【色彩关键】影响驱动电压,可以微调屏幕的整体亮度和功耗。
|
||||
LCD_WR_REG(0xC3);
|
||||
LCD_WR_DATA8(0x12);
|
||||
|
||||
// 10. VDV 设置 (VDV Set)
|
||||
// 通常保持默认值。
|
||||
LCD_WR_REG(0xC4);
|
||||
LCD_WR_DATA8(0x20);
|
||||
|
||||
// 11. 帧率控制 (Frame Rate Control)
|
||||
// 0x0F 通常对应 60Hz 左右的刷新率。如果屏幕闪烁,可以尝试减小这个值(例如0x14),但会增加功耗。
|
||||
LCD_WR_REG(0xC6);
|
||||
LCD_WR_DATA8(0x0F);
|
||||
|
||||
// 12. 电源控制 (Power Control)
|
||||
LCD_WR_REG(0xD0);
|
||||
LCD_WR_DATA8(0xA4);
|
||||
LCD_WR_DATA8(0xA6);
|
||||
LCD_WR_DATA8(0xA1);
|
||||
|
||||
// 13. 正 Gamma 校正 (Positive Voltage Gamma Control)
|
||||
// 【色彩关键】这是调整颜色和灰阶表现最核心的部分!
|
||||
// 这14个字节定义了从暗到亮的灰阶曲线。修改这些值可以显著改变色彩的饱和度、对比度和过渡平滑度。
|
||||
LCD_WR_REG(0xE0);
|
||||
LCD_WR_DATA8(0xD0);
|
||||
LCD_WR_DATA8(0x04);
|
||||
@@ -286,6 +310,8 @@ void LCD_Init(void)
|
||||
LCD_WR_DATA8(0x1F);
|
||||
LCD_WR_DATA8(0x23);
|
||||
|
||||
// 14. 负 Gamma 校正 (Negative Voltage Gamma Control)
|
||||
// 【色彩关键】与正Gamma同样重要,两者需要配合调整以达到最佳效果。
|
||||
LCD_WR_REG(0xE1);
|
||||
LCD_WR_DATA8(0xD0);
|
||||
LCD_WR_DATA8(0x04);
|
||||
@@ -302,8 +328,10 @@ void LCD_Init(void)
|
||||
LCD_WR_DATA8(0x20);
|
||||
LCD_WR_DATA8(0x23);
|
||||
|
||||
// 15. 开启颜色反转 (Display Inversion On)
|
||||
LCD_WR_REG(0x21);
|
||||
|
||||
// 16. 开启显示 (Display On)
|
||||
LCD_WR_REG(0x29);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,9 @@ static void disp_flush(lv_display_t * disp, const lv_area_t * area, uint8_t * px
|
||||
* STATIC VARIABLES
|
||||
**********************/
|
||||
|
||||
// 1. 定义一个静态变量来保存当前的 driver 句柄
|
||||
static lv_display_t *g_current_disp_drv = NULL;
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
@@ -66,6 +69,7 @@ void lv_port_disp_init(void)
|
||||
* -----------------------------------*/
|
||||
lv_display_t * disp = lv_display_create(MY_DISP_HOR_RES, MY_DISP_VER_RES);
|
||||
lv_display_set_flush_cb(disp, disp_flush);
|
||||
g_current_disp_drv = disp;
|
||||
#define BUFFER_METHOD 1
|
||||
#if BUFFER_METHOD == 1
|
||||
/* Example 1
|
||||
@@ -103,6 +107,13 @@ void lv_port_disp_init(void)
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
void lcd_flush_ready_callback(void)
|
||||
{
|
||||
if (g_current_disp_drv != NULL) {
|
||||
lv_display_flush_ready(g_current_disp_drv);
|
||||
}
|
||||
}
|
||||
|
||||
/*Initialize your display and the required peripherals.*/
|
||||
static void disp_init(void)
|
||||
{
|
||||
@@ -110,6 +121,7 @@ static void disp_init(void)
|
||||
LCD_Init();
|
||||
LCD_Fill(0,0, LCD_W, LCD_H, BLACK);
|
||||
LCD_Close_Light();
|
||||
LCD_Set_Flush_Complete_Callback(lcd_flush_ready_callback);
|
||||
}
|
||||
|
||||
volatile bool disp_flush_enabled = true;
|
||||
@@ -142,12 +154,12 @@ static void disp_flush(lv_display_t * disp_drv, const lv_area_t * area, uint8_t
|
||||
last_rotation = current_rotation; // 更新上次状态
|
||||
}
|
||||
if(disp_flush_enabled) {
|
||||
LCD_Color_Render(area->x1,area->y1,area->x2,area->y2, (uint16_t *)px_map);
|
||||
LCD_Color_Render_Async(area->x1,area->y1,area->x2,area->y2, (uint16_t *)px_map);
|
||||
}
|
||||
|
||||
/*IMPORTANT!!!
|
||||
*Inform the graphics library that you are ready with the flushing*/
|
||||
lv_display_flush_ready(disp_drv);
|
||||
// lv_display_flush_ready(disp_drv);
|
||||
}
|
||||
|
||||
#else /*Enable this file at the top*/
|
||||
|
||||
@@ -54,7 +54,7 @@ Page_t pages[] = {
|
||||
*/
|
||||
static void main_timer_cb(lv_timer_t * timer)
|
||||
{
|
||||
// 1s 刷新一次全屏,防止有时候不知什么原因的LCD刷新错误(需要后续找到问题从根本解决)
|
||||
// 1s 刷新一次全屏,防止有时候不知什么原因的LCD刷新错误(需要后续找到问题从根本解决不知是不是屏幕本身问题)
|
||||
ui_full_screen_refresh(lv_screen_active());
|
||||
ui_GetElapsedTime_HMS(NULL, NULL, NULL); // 更新经过时间计数器
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user