本帖最后由 dirty123 于 2025-3-2 21:44 编辑
#申请原创#
本篇讲述驱动LCD显示。
一.原理
开发板使用204*240 RGB LCD。原理如下
二.代码准备
1.GPIO引脚配置如下,主要是复位引脚、背光引脚、电源控制引脚。
2.LCD FMC接口配置
至此,实现硬件引脚配置。后面以STM32Cube MCU Packages安装包下的BSP库为基础代码展开。
3.LCD初始化。在SystemHardwareInit函数内实现,包含GPIO引脚初始化与设置
static void SystemHardwareInit(void)
{
/*
......
*/
LCD_UTILS_Drv_t lcdDrv;
/* Set UTIL_LCD functions */
lcdDrv.DrawBitmap = BSP_LCD_DrawBitmap;
lcdDrv.FillRGBRect = BSP_LCD_FillRGBRect;
lcdDrv.DrawHLine = BSP_LCD_DrawHLine;
lcdDrv.DrawVLine = BSP_LCD_DrawVLine;
lcdDrv.FillRect = BSP_LCD_FillRect;
lcdDrv.GetPixel = BSP_LCD_ReadPixel;
lcdDrv.SetPixel = BSP_LCD_WritePixel;
lcdDrv.GetXSize = BSP_LCD_GetXSize;
lcdDrv.GetYSize = BSP_LCD_GetYSize;
lcdDrv.SetLayer = BSP_LCD_SetActiveLayer;
lcdDrv.GetFormat = BSP_LCD_GetFormat;
UTIL_LCD_SetFuncDriver(&lcdDrv);
/* Clear the LCD */
UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);
/* Set the display on */
if (BSP_LCD_DisplayOn(0) != BSP_ERROR_NONE)
{
Error_Handler();
}
}
4.LCD刷屏显示红、绿、蓝
UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_RED);
HAL_Delay(1000);
UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_GREEN);
HAL_Delay(1000);
UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_BLUE);
HAL_Delay(1000);
5.LCD显示字符串
UTIL_LCD_SetFont(&Font16);
/* Set the LCD Text Color */
UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_LIGHTMAGENTA);
UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_BLUE);
UTIL_LCD_FillRect(0, 0, 240, 30, UTIL_LCD_COLOR_LIGHTMAGENTA);
/* Display LCD messages */
UTIL_LCD_DisplayStringAt(0, 10, (uint8_t *)"STM32L562E-DK Board", CENTER_MODE);
6.LCD显示图像
(1)制作200*200像素图片,使用Image2Lcd工具加载设置如下图所示,生成图像数组。
(2)编写画图函数
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t column,uint16_t row,uint32_t *pic)
{
uint16_t m,h;
uint32_t *data=(uint32_t*)pic;
for(h=0+y;h<row+y;h++) //60
{
for(m=0+x;m<column+x;m++) //180
{
// LCD_Fast_DrawPoint(m,h,*data++);
UTIL_LCD_SetPixel(m,h,*data++);
}
}
}
7.main函数
int main(void)
{
/* STM32L5xx HAL library initialization:
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 3
- Low Level Initialization
*/
HAL_Init();
/* Configure the System clock to have a frequency of 110 MHz */
SystemClock_Config();
/* For better performances, enable the instruction cache in 1-way direct mapped mode */
HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
if (HAL_ICACHE_Enable() != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* System common Hardware components initialization (Leds, button, joystick and LCD) */
SystemHardwareInit();
UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_RED);
HAL_Delay(5000);
UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_GREEN);
HAL_Delay(5000);
UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_BLUE);
HAL_Delay(5000);
/* Clear the LCD */
UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);
UTIL_LCD_SetFont(&Font16);
/* Set the LCD Text Color */
UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_WHITE);
UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_BLACK);//UTIL_LCD_COLOR_BLACK//UTIL_LCD_COLOR_BLUE
// UTIL_LCD_FillRect(0, 0, 240, 30, UTIL_LCD_COLOR_LIGHTMAGENTA);
/* Display LCD messages */
UTIL_LCD_DisplayStringAt(0, 10, (uint8_t *)"STM32L562E-DK Board", CENTER_MODE);
// UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_LIGHTMAGENTA);
// UTIL_LCD_FillRect(0, 18, 240, 19, UTIL_LCD_COLOR_LIGHTMAGENTA);
UTIL_LCD_DrawHLine(0, 25,240,UTIL_LCD_COLOR_LIGHTMAGENTA);
//UTIL_LCD_DrawBitmap(0, 0, (uint8_t *)gImage_1);
LCD_ShowPicture(20,40,200,200,(uint32_t *)gImage_1);
HAL_Delay(100);
/* Infinite loop */
while (1)
{
}
}
三.测试
编译烧录后。LCD分别显示红绿蓝刷品,显示字符串及图像。效果如下。
ps:资源图片
|
|