[STM32L5] 【STM32L562 DK试用】驱动LCD

[复制链接]
 楼主| lulugl 发表于 2025-5-16 19:47 | 显示全部楼层 |阅读模式
【前言】
由于touchGFX没有适配stm32l562这款开发板,所以需要手工驱动LCD。在bsp工程中已经有了LCD的示例。这篇就是根据示例来驱动。
1、在前面printf的工程中,在stm32cubeMX中开启FMC(如果不开启FMC那莫要手工去.conf中开启宏,有点麻烦)
8615568272293a5f27.png
然后在代码生成中,选择不生成:
61000682723a49f50a.png
2、复制STM32Cube_FW_L5_V1.5.1\Drivers\BSP到工程中。
19886682723f330f46.png
3、在mdk工程把LCD->st7789的驱动添加进工程:
9385168272476217f6.png
4、把bsp_lcd驱动添加进工程:
29932682724991be8c.png
5、添加画点画线、字符的库:
8374682724bec402f.png
6、添加lcd.c到工程,具体实现一个综合的界面,这里算单的使用bsp的示例:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"

  3. /** @addtogroup STM32L5xx_HAL_Examples
  4.   * @{
  5.   */

  6. /** @addtogroup BSP
  7.   * @{
  8.   */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* Private define ------------------------------------------------------------*/
  11. #define LCD_FEATURES_NUM        3

  12. /* Private macro -------------------------------------------------------------*/
  13. /* Private variables ---------------------------------------------------------*/
  14. static uint8_t LCD_Feature;

  15. /* Private function prototypes -----------------------------------------------*/
  16. static void LCD_SetHint(void);
  17. static void LCD_Show_Feature(uint8_t feature);

  18. /* Private functions ---------------------------------------------------------*/

  19. /**
  20.   * [url=home.php?mod=space&uid=247401]@brief[/url]  LCD demo
  21.   * @param  None
  22.   * @retval None
  23.   */
  24. void LCD_demo(void)
  25. {
  26.   LCD_SetHint();
  27.   LCD_Feature = 0;
  28.   LCD_Show_Feature (LCD_Feature);


  29.   
  30. }

  31. /**
  32.   * @brief  Display LCD demo hint
  33.   * @param  None
  34.   * @retval None
  35.   */
  36. static void LCD_SetHint(void)
  37. {
  38.   /* Clear the LCD */
  39.   UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

  40.   /* Set LCD Demo description */
  41.   UTIL_LCD_FillRect(0, 0, 240, 80, UTIL_LCD_COLOR_BLUE);
  42.   UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_WHITE);
  43.   UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_BLUE);
  44.   UTIL_LCD_SetFont(&Font24);
  45.   UTIL_LCD_DisplayStringAt(0, 0, (uint8_t *)"LCD", CENTER_MODE);
  46.   UTIL_LCD_SetFont(&Font12);
  47.   UTIL_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows the different", CENTER_MODE);
  48.   UTIL_LCD_DisplayStringAt(0, 45, (uint8_t *)"LCD Features, use User push-button", CENTER_MODE);
  49.   UTIL_LCD_DisplayStringAt(0, 60, (uint8_t *)"to display next page", CENTER_MODE);

  50.   UTIL_LCD_DrawRect(10, 90, 220, 140, UTIL_LCD_COLOR_BLUE);
  51.   UTIL_LCD_DrawRect(11, 91, 218, 138, UTIL_LCD_COLOR_BLUE);
  52. }

  53. /**
  54.   * @brief  Show LCD Features
  55.   * @param  feature : feature index
  56.   * @retval None
  57.   */
  58. static void LCD_Show_Feature(uint8_t feature)
  59. {
  60.   Point Points[] = {{20, 150}, {50, 150}, {50, 200}};
  61.   Point Points2[] = {{60, 150}, {90, 150}, {90, 200}};

  62.   UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_WHITE);
  63.   UTIL_LCD_FillRect(12, 92, 216, 136, UTIL_LCD_COLOR_WHITE);
  64.   UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_BLACK);

  65.   switch (feature)
  66.   {
  67.     case 0:
  68.       /* Text Feature */

  69.       UTIL_LCD_DisplayStringAt(14, 100, (uint8_t *)"Left aligned Text", LEFT_MODE);
  70.       UTIL_LCD_DisplayStringAt(0, 115, (uint8_t *)"Center aligned Text", CENTER_MODE);
  71.       UTIL_LCD_DisplayStringAt(14, 130, (uint8_t *)"Right aligned Text", RIGHT_MODE);
  72.       UTIL_LCD_SetFont(&Font24);
  73.       UTIL_LCD_DisplayStringAt(14, 150, (uint8_t *)"Font24", LEFT_MODE);
  74.       UTIL_LCD_SetFont(&Font20);
  75.       UTIL_LCD_DisplayStringAt(14, 180, (uint8_t *)"Font20", LEFT_MODE);
  76.       UTIL_LCD_SetFont(&Font16);
  77.       UTIL_LCD_DisplayStringAt(14, 210, (uint8_t *)"Font16", LEFT_MODE);
  78.       break;

  79.     case 1:

  80.       /* Draw misc. Shapes */
  81.       UTIL_LCD_DrawRect(20, 100, 30 , 40, UTIL_LCD_COLOR_BLACK);
  82.       UTIL_LCD_FillRect(60, 100, 30 , 40, UTIL_LCD_COLOR_BLACK);

  83.       UTIL_LCD_DrawCircle(130, 120, 20, UTIL_LCD_COLOR_GRAY);
  84.       UTIL_LCD_FillCircle(195, 120, 20, UTIL_LCD_COLOR_GRAY);

  85.       UTIL_LCD_DrawPolygon(Points, 3, UTIL_LCD_COLOR_GREEN);
  86.       UTIL_LCD_FillPolygon(Points2, 3, UTIL_LCD_COLOR_GREEN);

  87.       UTIL_LCD_DrawEllipse(130, 170, 30, 20, UTIL_LCD_COLOR_RED);
  88.       UTIL_LCD_FillEllipse(195, 170, 30, 20, UTIL_LCD_COLOR_RED);

  89.       UTIL_LCD_DrawHLine(20, 210, 30, UTIL_LCD_COLOR_BLACK);
  90.       UTIL_LCD_DrawLine (100, 220, 220, 190, UTIL_LCD_COLOR_BLACK);
  91.       UTIL_LCD_DrawLine (100, 190, 220, 220, UTIL_LCD_COLOR_BLACK);
  92.       break;

  93.     case 2:


  94.       break;
  95.     default :
  96.       break;
  97.   }
  98. }
7、添加头文件到工程中:
9717268272533e140f.png
8、在main.c中添加初始化等代码:
  1. if (LcdInitialized != SET)
  2.   {
  3.     LCD_UTILS_Drv_t lcdDrv;

  4.     /* Initialize the LCD */
  5.     if (BSP_LCD_Init(0, LCD_ORIENTATION_PORTRAIT) != BSP_ERROR_NONE)
  6.     {
  7.       Error_Handler();
  8.     }

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

  22.     /* Clear the LCD */
  23.     UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

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

  29.     LcdInitialized = SET;
  30.   }
  31.        
  32.         Display_DemoDescription();
实现效果:
15273682725b777b3d.jpg
36969682725bf6efce.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

180

主题

830

帖子

12

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