[STM32L5] 【STM32L562 DK试用】基础外设体验

[复制链接]
1561|0
 楼主| lishuihua 发表于 2025-3-8 11:34 | 显示全部楼层 |阅读模式
开发板自带了一块240*240分辨率的触摸屏:
8073667cb03f152499.png

本次基于官方demo进行修改,从而在屏幕上显示相应的文字。首先进行系统和时钟的初始化:
  1. HAL_StatusTypeDef HAL_Init(void)
  2. {
  3.   HAL_StatusTypeDef  status = HAL_OK;

  4.   /* Set Interrupt Group Priority */
  5.   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_3);

  6.   /* Insure time base clock coherency */
  7.   SystemCoreClockUpdate();

  8.   /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is MSI) */
  9.   if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  10.   {
  11.     status = HAL_ERROR;
  12.   }
  13.   else
  14.   {
  15.     /* Init the low level hardware */
  16.     HAL_MspInit();
  17.   }

  18.   /* Return function status */
  19.   return status;
  20. }
然后进行相关HMI设备和LED等的初始化:
  1. static void SystemHardwareInit(void)
  2. {
  3.   /* Init LEDs  */
  4.   if (LedInitialized != SET)
  5.   {
  6.     if (BSP_LED_Init(LED9) != BSP_ERROR_NONE)
  7.     {
  8.       Error_Handler();
  9.     }
  10.     if (BSP_LED_Init(LED10) != BSP_ERROR_NONE)
  11.     {
  12.       Error_Handler();
  13.     }
  14.     LedInitialized = SET;
  15.   }

  16.   /* Init User push-button in EXTI Mode */
  17.   if (ButtonInitialized != SET)
  18.   {
  19.     if (BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI) != BSP_ERROR_NONE)
  20.     {
  21.       Error_Handler();
  22.     }
  23.     ButtonInitialized = SET;
  24.   }

  25.   /* Initialize the LCD */
  26.   if (LcdInitialized != SET)
  27.   {
  28.     LCD_UTILS_Drv_t lcdDrv;

  29.     /* Initialize the LCD */
  30.     if (BSP_LCD_Init(0, LCD_ORIENTATION_PORTRAIT) != BSP_ERROR_NONE)
  31.     {
  32.       Error_Handler();
  33.     }

  34.     /* Set UTIL_LCD functions */
  35.     lcdDrv.DrawBitmap  = BSP_LCD_DrawBitmap;
  36.     lcdDrv.FillRGBRect = BSP_LCD_FillRGBRect;
  37.     lcdDrv.DrawHLine   = BSP_LCD_DrawHLine;
  38.     lcdDrv.DrawVLine   = BSP_LCD_DrawVLine;
  39.     lcdDrv.FillRect    = BSP_LCD_FillRect;
  40.     lcdDrv.GetPixel    = BSP_LCD_ReadPixel;
  41.     lcdDrv.SetPixel    = BSP_LCD_WritePixel;
  42.     lcdDrv.GetXSize    = BSP_LCD_GetXSize;
  43.     lcdDrv.GetYSize    = BSP_LCD_GetYSize;
  44.     lcdDrv.SetLayer    = BSP_LCD_SetActiveLayer;
  45.     lcdDrv.GetFormat   = BSP_LCD_GetFormat;
  46.     UTIL_LCD_SetFuncDriver(&lcdDrv);

  47.     /* Clear the LCD */
  48.     UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

  49.     /* Set the display on */
  50.     if (BSP_LCD_DisplayOn(0) != BSP_ERROR_NONE)
  51.     {
  52.       Error_Handler();
  53.     }

  54.     LcdInitialized = SET;
  55.   }

  56.   /* Initialize the TouchScreen */
  57.   if (TsInitialized != SET)
  58.   {
  59.     TS_Init_t TsInit;

  60.     /* Initialize the TouchScreen */
  61.     TsInit.Width       = 240;
  62.     TsInit.Height      = 240;
  63.     TsInit.Orientation = TS_ORIENTATION_PORTRAIT;
  64.     TsInit.Accuracy    = 10;
  65.     if (BSP_TS_Init(0, &TsInit) != BSP_ERROR_NONE)
  66.     {
  67.       Error_Handler();
  68.     }

  69.     /* Configure TS interrupt */
  70.     if (BSP_TS_EnableIT(0) != BSP_ERROR_NONE)
  71.     {
  72.       Error_Handler();
  73.     }

  74.     TsInitialized = SET;
  75.   }
  76. }


调用屏幕API进行显示:
  1. static void Display_DemoDescription(void)
  2. {
  3.   char desc[60];

  4.   /* Set font */
  5.   UTIL_LCD_SetFont(&Font20);

  6.   /* Clear the LCD */
  7.   UTIL_LCD_Clear(UTIL_LCD_COLOR_WHITE);

  8.   /* Set the LCD Text Color */
  9.   UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_DARKBLUE);
  10.   UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_WHITE);

  11.   /* Display LCD messages */
  12.   UTIL_LCD_DisplayStringAt(0, 10, (uint8_t *)"STM32L562E-DK BSP", CENTER_MODE);
  13.   UTIL_LCD_DisplayStringAt(0, 35, (uint8_t *)"drivers example", CENTER_MODE);

  14.   /* Draw Bitmap */
  15.   UTIL_LCD_DrawBitmap(80, 65, (uint8_t *)st**);

  16.   UTIL_LCD_SetFont(&Font8);
  17.   UTIL_LCD_DisplayStringAt(0, 220, (uint8_t *)"Copyright (c) STMicroelectronics 2019", CENTER_MODE);

  18.   UTIL_LCD_SetFont(&Font12);
  19.   UTIL_LCD_FillRect(0, 145, 240, 50, UTIL_LCD_COLOR_BLUE);
  20.   UTIL_LCD_SetTextColor(UTIL_LCD_COLOR_WHITE);
  21.   UTIL_LCD_SetBackColor(UTIL_LCD_COLOR_BLUE);
  22.   UTIL_LCD_DisplayStringAt(0, 135, (uint8_t *)"Hello 21IC", CENTER_MODE);
  23.   UTIL_LCD_DisplayStringAt(0, 150, (uint8_t *)"Press User push-button", CENTER_MODE);
  24.   UTIL_LCD_DisplayStringAt(0, 165, (uint8_t *)"to start :", CENTER_MODE);
  25.   sprintf(desc,"%s example", BSP_examples[DemoIndex].DemoName);
  26.   UTIL_LCD_DisplayStringAt(0, 180, (uint8_t *)desc, CENTER_MODE);
  27. }


显示效果:
1026367cb03a409b02.png

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

本版积分规则

9

主题

31

帖子

0

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