[STM32L5] 【STM32L562E_DK开发板】--4.LCD显示

[复制链接]
 楼主| dirty123 发表于 2025-3-2 21:25 | 显示全部楼层 |阅读模式
<
本帖最后由 dirty123 于 2025-3-2 21:44 编辑

#申请原创#      
      本篇讲述驱动LCD显示。
一.原理
      开发板使用204*240 RGB LCD。原理如下
1_LCD原理图.png

二.代码准备
1.GPIO引脚配置如下,主要是复位引脚、背光引脚、电源控制引脚。
2_LCD GPIO配置.png
2.LCD FMC接口配置
3.LCD FMC配置.png
      至此,实现硬件引脚配置。后面以STM32Cube MCU Packages安装包下的BSP库为基础代码展开。
3.LCD初始化。在SystemHardwareInit函数内实现,包含GPIO引脚初始化与设置
  1. static void SystemHardwareInit(void)
  2. {
  3.         /*
  4.         ......
  5.         */
  6.         LCD_UTILS_Drv_t lcdDrv;

  7.         /* Set UTIL_LCD functions */
  8.         lcdDrv.DrawBitmap = BSP_LCD_DrawBitmap;
  9.         lcdDrv.FillRGBRect = BSP_LCD_FillRGBRect;
  10.         lcdDrv.DrawHLine = BSP_LCD_DrawHLine;
  11.         lcdDrv.DrawVLine = BSP_LCD_DrawVLine;
  12.         lcdDrv.FillRect = BSP_LCD_FillRect;
  13.         lcdDrv.GetPixel = BSP_LCD_ReadPixel;
  14.         lcdDrv.SetPixel = BSP_LCD_WritePixel;
  15.         lcdDrv.GetXSize = BSP_LCD_GetXSize;
  16.         lcdDrv.GetYSize = BSP_LCD_GetYSize;
  17.         lcdDrv.SetLayer = BSP_LCD_SetActiveLayer;
  18.         lcdDrv.GetFormat = BSP_LCD_GetFormat;
  19.         UTIL_LCD_SetFuncDriver(&lcdDrv);

  20.         /* Clear the LCD */
  21.         UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

  22.         /* Set the display on */
  23.         if (BSP_LCD_DisplayOn(0) != BSP_ERROR_NONE)
  24.         {
  25.         Error_Handler();
  26.         }

  27. }
4.LCD刷屏显示红、绿、蓝
  1. UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_RED);
  2.   HAL_Delay(1000);
  3.   UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_GREEN);
  4.   HAL_Delay(1000);
  5.   UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_BLUE);
  6.   HAL_Delay(1000);
5.LCD显示字符串
  1. UTIL_LCD_SetFont(&Font16);
  2.   /* Set the LCD Text Color */
  3.   UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_LIGHTMAGENTA);
  4.   UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_BLUE);
  5.   UTIL_LCD_FillRect(0, 0, 240, 30, UTIL_LCD_COLOR_LIGHTMAGENTA);
  6.   /* Display LCD messages */
  7.   UTIL_LCD_DisplayStringAt(0, 10, (uint8_t *)"STM32L562E-DK Board", CENTER_MODE);
6.LCD显示图像
(1)制作200*200像素图片,使用Image2Lcd工具加载设置如下图所示,生成图像数组。
4_制作图像镜像.png
(2)编写画图函数
  1. void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t column,uint16_t row,uint32_t *pic)
  2. {
  3.     uint16_t m,h;
  4.     uint32_t *data=(uint32_t*)pic;
  5.     for(h=0+y;h<row+y;h++) //60
  6.     {
  7.         for(m=0+x;m<column+x;m++) //180
  8.         {
  9.             // LCD_Fast_DrawPoint(m,h,*data++);
  10.             UTIL_LCD_SetPixel(m,h,*data++);
  11.         }
  12.     }
  13. }
7.main函数
  1. int main(void)
  2. {

  3.   /* STM32L5xx HAL library initialization:
  4.        - Systick timer is configured by default as source of time base, but user
  5.              can eventually implement his proper time base source (a general purpose
  6.              timer for example or other time source), keeping in mind that Time base
  7.              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  8.              handled in milliseconds basis.
  9.        - Set NVIC Group Priority to 3
  10.        - Low Level Initialization
  11.      */
  12.   HAL_Init();

  13.   /* Configure the System clock to have a frequency of 110 MHz */
  14.   SystemClock_Config();

  15.   /* For better performances, enable the instruction cache in 1-way direct mapped mode */
  16.   HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
  17.   if (HAL_ICACHE_Enable() != HAL_OK)
  18.   {
  19.     /* Initialization Error */
  20.     Error_Handler();
  21.   }

  22.   /* System common Hardware components initialization (Leds, button, joystick and LCD) */
  23.   SystemHardwareInit();


  24.   UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_RED);
  25.   HAL_Delay(5000);
  26.   UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_GREEN);
  27.   HAL_Delay(5000);
  28.   UTIL_LCD_FillRect(0, 0, 240, 240, UTIL_LCD_COLOR_BLUE);
  29.   HAL_Delay(5000);

  30.   /* Clear the LCD */
  31.   UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

  32.   UTIL_LCD_SetFont(&Font16);
  33.   /* Set the LCD Text Color */
  34.   UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_WHITE);
  35.   UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_BLACK);//UTIL_LCD_COLOR_BLACK//UTIL_LCD_COLOR_BLUE
  36.   // UTIL_LCD_FillRect(0, 0, 240, 30, UTIL_LCD_COLOR_LIGHTMAGENTA);
  37.   /* Display LCD messages */
  38.   UTIL_LCD_DisplayStringAt(0, 10, (uint8_t *)"STM32L562E-DK Board", CENTER_MODE);

  39.   // UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_LIGHTMAGENTA);
  40.   // UTIL_LCD_FillRect(0, 18, 240, 19, UTIL_LCD_COLOR_LIGHTMAGENTA);
  41.   UTIL_LCD_DrawHLine(0, 25,240,UTIL_LCD_COLOR_LIGHTMAGENTA);
  42.   
  43.   //UTIL_LCD_DrawBitmap(0, 0, (uint8_t *)gImage_1);
  44.   LCD_ShowPicture(20,40,200,200,(uint32_t *)gImage_1);
  45.   HAL_Delay(100);
  46.   /* Infinite loop */
  47.   while (1)
  48.   {
  49.   }
  50. }


三.测试         
      编译烧录后。LCD分别显示红绿蓝刷品,显示字符串及图像。效果如下。
5_LCD_Red.png 6_LCD_Green.png
7_LCD_Blue.png
8_LCD_str_image.png



ps:资源图片

200*200像素

200*200像素






您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

135

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部