21、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中断执行函数

c 复制代码
/**********************************************************************
*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

c 复制代码
#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文件中添加定时器回调

c 复制代码
/* USER CODE BEGIN 0 */
#include "bsp.h"
/* USER CODE END 0 */
c 复制代码
/* 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中主函数添加触摸测试函数

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 的速度等级。

◆ 如果显示了,但是显示的位置不正确,可以重新调整时序参数即可。

相关推荐
偶像你挑的噻9 小时前
11-Linux驱动开发-I2C子系统–mpu6050简单数据透传驱动
linux·驱动开发·stm32·嵌入式硬件
Chat_zhanggong34511 小时前
K4A8G165WC-BITD产品推荐
人工智能·嵌入式硬件·算法
强化学习与机器人控制仿真11 小时前
RSL-RL:开源人形机器人强化学习控制研究库
开发语言·人工智能·stm32·神经网络·机器人·强化学习·模仿学习
bai54593611 小时前
STM32 PWM驱动LED呼吸灯
stm32·单片机·嵌入式硬件
智者知已应修善业12 小时前
【51单片机普通延时奇偶灯切换】2023-4-4
c语言·经验分享·笔记·嵌入式硬件·51单片机
btzhy17 小时前
STM32单片机:基本定时器应用:单脉冲模式(STM32L4xx)
stm32·单片机·嵌入式硬件·基本定时器应用:单脉冲模式
文sir.19 小时前
温湿度采集系统(stm32+mqtt+Onenet云平台+esp8266)
stm32·单片机·嵌入式硬件·mqtt·onenet·云平台·esp8266
DIY机器人工房19 小时前
嵌入式面试题:你有没有用过直接基于TCP协议,不借助HTTP、MQTT这类上层协议的项目经历?
stm32·单片机·嵌入式硬件·面试题·diy机器人工房
zhmc21 小时前
Cortex M0单片机指令执行周期
单片机·嵌入式硬件
编程小白菜12321 小时前
STM32L051单片机485Ymode升级后程序不能正常运行问题
stm32·嵌入式硬件