注:本文基于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>© 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 的速度等級。
◆ 如果顯示了,但是顯示的位置不正確,可以重新調整時序參數即可。