打印
[STM32L5]

【STM32L562 DK试用】驱动LCD

[复制链接]
199|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
lulugl|  楼主 | 2025-5-16 19:47 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
【前言】
由于touchGFX没有适配stm32l562这款开发板,所以需要手工驱动LCD。在bsp工程中已经有了LCD的示例。这篇就是根据示例来驱动。
1、在前面printf的工程中,在stm32cubeMX中开启FMC(如果不开启FMC那莫要手工去.conf中开启宏,有点麻烦)

然后在代码生成中,选择不生成:

2、复制STM32Cube_FW_L5_V1.5.1\Drivers\BSP到工程中。

3、在mdk工程把LCD->st7789的驱动添加进工程:

4、把bsp_lcd驱动添加进工程:

5、添加画点画线、字符的库:

6、添加lcd.c到工程,具体实现一个综合的界面,这里算单的使用bsp的示例:
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup STM32L5xx_HAL_Examples
  * @{
  */

/** @addtogroup BSP
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define LCD_FEATURES_NUM        3

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static uint8_t LCD_Feature;

/* Private function prototypes -----------------------------------------------*/
static void LCD_SetHint(void);
static void LCD_Show_Feature(uint8_t feature);

/* Private functions ---------------------------------------------------------*/

/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]  LCD demo
  * @param  None
  * @retval None
  */
void LCD_demo(void)
{
  LCD_SetHint();
  LCD_Feature = 0;
  LCD_Show_Feature (LCD_Feature);


  
}

/**
  * @brief  Display LCD demo hint
  * @param  None
  * @retval None
  */
static void LCD_SetHint(void)
{
  /* Clear the LCD */
  UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

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

  UTIL_LCD_DrawRect(10, 90, 220, 140, UTIL_LCD_COLOR_BLUE);
  UTIL_LCD_DrawRect(11, 91, 218, 138, UTIL_LCD_COLOR_BLUE);
}

/**
  * @brief  Show LCD Features
  * @param  feature : feature index
  * @retval None
  */
static void LCD_Show_Feature(uint8_t feature)
{
  Point Points[] = {{20, 150}, {50, 150}, {50, 200}};
  Point Points2[] = {{60, 150}, {90, 150}, {90, 200}};

  UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_WHITE);
  UTIL_LCD_FillRect(12, 92, 216, 136, UTIL_LCD_COLOR_WHITE);
  UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_BLACK);

  switch (feature)
  {
    case 0:
      /* Text Feature */

      UTIL_LCD_DisplayStringAt(14, 100, (uint8_t *)"Left aligned Text", LEFT_MODE);
      UTIL_LCD_DisplayStringAt(0, 115, (uint8_t *)"Center aligned Text", CENTER_MODE);
      UTIL_LCD_DisplayStringAt(14, 130, (uint8_t *)"Right aligned Text", RIGHT_MODE);
      UTIL_LCD_SetFont(&Font24);
      UTIL_LCD_DisplayStringAt(14, 150, (uint8_t *)"Font24", LEFT_MODE);
      UTIL_LCD_SetFont(&Font20);
      UTIL_LCD_DisplayStringAt(14, 180, (uint8_t *)"Font20", LEFT_MODE);
      UTIL_LCD_SetFont(&Font16);
      UTIL_LCD_DisplayStringAt(14, 210, (uint8_t *)"Font16", LEFT_MODE);
      break;

    case 1:

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

      UTIL_LCD_DrawCircle(130, 120, 20, UTIL_LCD_COLOR_GRAY);
      UTIL_LCD_FillCircle(195, 120, 20, UTIL_LCD_COLOR_GRAY);

      UTIL_LCD_DrawPolygon(Points, 3, UTIL_LCD_COLOR_GREEN);
      UTIL_LCD_FillPolygon(Points2, 3, UTIL_LCD_COLOR_GREEN);

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

      UTIL_LCD_DrawHLine(20, 210, 30, UTIL_LCD_COLOR_BLACK);
      UTIL_LCD_DrawLine (100, 220, 220, 190, UTIL_LCD_COLOR_BLACK);
      UTIL_LCD_DrawLine (100, 190, 220, 220, UTIL_LCD_COLOR_BLACK);
      break;

    case 2:


      break;
    default :
      break;
  }
}
7、添加头文件到工程中:

8、在main.c中添加初始化等代码:
 if (LcdInitialized != SET)
  {
    LCD_UTILS_Drv_t lcdDrv;

    /* Initialize the LCD */
    if (BSP_LCD_Init(0, LCD_ORIENTATION_PORTRAIT) != BSP_ERROR_NONE)
    {
      Error_Handler();
    }

    /* 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();
    }

    LcdInitialized = SET;
  }
       
        Display_DemoDescription();
实现效果:


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

177

主题

814

帖子

11

粉丝