使用STM32 LTDC驱动LCD屏幕的完整指南

注:本文基于stm32使用FMC驱动SDRAM(IS42S32800G-6BLI)工程继续开发
本例使用安富莱的H743XIH板子驱动LTDC点亮7寸LCD
硬件接线:RGB888


一、cubemx配置

1、LTDC配置





注意此引脚应于上面的硬件接线图一致

2、配置DMA2D


3、背光引脚和触摸引脚

4、时钟配置


5、定时器配置

定时器6用来做精确延时

TIM7配置成1ms中断一次


触摸先不予弄
生成代码…

二、代码编写

1、移植文件至工程中,可以去下载完整工程中获取

字体文件
ascii_32.h|ascii_32.c
AsciiFonts.h
fonts_32.c|fonts_32.h
LCD文件
lcd_driver.c|lcd_driver.h
lcd_h7_driver.c|lcd_h7_driver.h
精确延时文件
delay_driver.c|delay_driver.h
触摸文件
bsp_i2c.c|bsp_i2c.h
GT911_driver.c|GT911_driver.h
touch_driver.c|touch_driver.h

2、更改精确延时文件delay_driver.c中得接口dDelayTIM和dDelayTIM_Handle为定时器6

3、bsp.c文件中初始化硬件,编写1m中断执行函数

/**********************************************************************
*file:板级支持包文件
*author:残梦
*versions:V1.0
*date:2023.08.10
*note:
**********************************************************************/
#include "bsp.h"
#include "common_driver.h"
#include "tim.h"
#include "lcd_driver.h"
#include "bsp_i2c.h"
#include "touch_driver.h"

/****************************************
@function:板硬件初始化
@param:void
@return:小于0--失败,0--成功
@note:
****************************************/
int32_t bsp_init(void)
{
    bsp_i2c_Init();
    touch_init();
    LCD_InitHard();
    HAL_TIM_Base_Start_IT(&htim7);
    return 0;
}

void bsp_Task_1ms(void)
{
    static uint16_t ms = 0;

    ms++;
    if(ms >= 1000)
    {
        ms = 0;
        //printf("1s到了\n");
    }
    touch_1ms();
}

bsp.h

#ifndef _bsp_H_
#define _bsp_H_
#ifdef __Cplusplus
#extern "C" {
#endif
#include "stdint.h"

int32_t bsp_init(void);
void bsp_Task_1ms(void);

#ifdef __Cplusplus
}
#endif
#endif

4、tim.c文件中添加定时器回调

/* USER CODE BEGIN 0 */
#include "bsp.h"
/* USER CODE END 0 */
/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if(htim->Instance == TIM7)
  {
    bsp_Task_1ms();
  }
}
/* USER CODE END 1 */

5、main.c中主函数添加触摸测试函数

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma2d.h"
#include "ltdc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "fmc.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp.h"
#include "common_driver.h"
#include "sdram_driver.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#include "touch_driver.h"
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MPU Configuration--------------------------------------------------------*/
  MPU_Config();

  /* Enable I-Cache---------------------------------------------------------*/
  SCB_EnableICache();

  /* Enable D-Cache---------------------------------------------------------*/
  SCB_EnableDCache();

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_FMC_Init();
  MX_USART1_UART_Init();
  MX_DMA2D_Init();
  MX_LTDC_Init();
  MX_TIM6_Init();
  MX_TIM7_Init();
  /* USER CODE BEGIN 2 */
  if(bsp_init() < 0){printf("error:bsp_init()\r\n");Error_Handler();}

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    touch_Test();
    HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

编译下载即可…
触摸屏幕随手指触摸轨迹画圆圈
屏幕800*480

三、完整工程下载

链接:https://pan.baidu.com/s/1PQfEsbaoNqUvyevT0YbDgA
提取码:p7j6

四、笔记

1、如何验证 LTDC 的时序配置是否正确

ltdc.c文件中void MX_LTDC_Init(void)内

下载程序,此处开启背光,测试背景色是否正常,如果背景层可以正常显示绿色,说明引脚和时序配置都是没有问题的
◆首先要清楚一点,当前的配置是否成功与 SDRAM 没有任何关系,因为背景层还用不到 SDRAM,图层 1 和图层 2 才需要 SDRAM 做显存使用。
◆ 从硬件着手检查,保证 STM32H7 芯片焊接没问题,TFT 接口一定要牢固,防止接触不良,特别是使用 FPC 软排线的时候,测试阶段,软排线越短越好。有时候也可能是显示屏有问题,最好可以备两个显示屏测试。
◆ 从软件配置着手检查,查看 LTDC 涉及到的所有引脚是否配置,引脚时钟是否使能。有时候无法显示也有可能是板子硬件设计不规范导致干扰较大造成的,此时,可以降低 LTDC 所涉及到 GPIO 的速度等级。
◆ 如果显示了,但是显示的位置不正确,可以重新调整时序参数即可。

物联沃分享整理
物联沃-IOTWORD物联网 » 使用STM32 LTDC驱动LCD屏幕的完整指南

发表评论