본문 바로가기

공부/STM32F4

7.[STM32F429] TFT LCD -LTDC 사용

반응형

1. STM32CubeMX 설정

RCC 설정
ST-LINK 설정
SPI5 설정
LTDC 설정

여기서 중요!!!

LTDC를 설정하면 핀설정이 아래와 같은데 핀설정이 실제 핀 설정이랑 틀리므로 변경 해줘야 한다.

LTDC 핀 설정 전
설정 후

 

2. TrueSTUDIO 설정

이젠 개발 환경 설정을 합시다~!

일단 BSP를 이용하여 ili9341를 제어할겁니다.

그러므로 BSP폴더를 추가해주세요

 

Step 1. BSP 폴더 추가하기

BSP.zip
0.02MB

Step 2. HAL Driver 추가하기

Mx 에서 생성하면 우리는 i2c 를 체크하지 않았기 때문에 에러가 생겨요. 그래서 HAL Driver를 직접 추가해야되요~

stm32f4xx_hal_i2c.h
0.03MB
stm32f4xx_hal_i2c_ex.h
0.00MB
stm32f4xx_hal_i2c.c
0.17MB
stm32f4xx_hal_i2c_ex.c
0.01MB

 

이렇게 추가해주면되요.

그리고~stm32f4xx_hal_conf.h 에서 

#define HAL_I2C_MODULE_ENABLED

define 주석을 풀어주세요.

STM32F4xx_hal_conf.h

step 3. st_logo1.h st_logo2.h 파일 추가하기

Test할 그림 파일을 추가해요~!

st_logo1.h
0.31MB
st_logo2.h
0.31MB

 

step 4. symbols 설정하기

 

 

 

 

3. Main.c

main.c
0.01MB

/* USER CODE BEGIN Header */ 
/** 
  ****************************************************************************** 
  * @file           : main.c 
  * @brief          : Main program body 
  ****************************************************************************** 
  ** This notice applies to any and all portions of this file 
  * that are not between comment pairs USER CODE BEGIN and 
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools 
  * are owned by their respective copyright owners. 
  * 
  * COPYRIGHT(c) 2019 STMicroelectronics 
  * 
  * Redistribution and use in source and binary forms, with or without modification, 
  * are permitted provided that the following conditions are met: 
  *   1. Redistributions of source code must retain the above copyright notice, 
  *      this list of conditions and the following disclaimer. 
  *   2. Redistributions in binary form must reproduce the above copyright notice, 
  *      this list of conditions and the following disclaimer in the documentation 
  *      and/or other materials provided with the distribution. 
  *   3. Neither the name of STMicroelectronics nor the names of its contributors 
  *      may be used to endorse or promote products derived from this software 
  *      without specific prior written permission. 
  * 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  * 
  ****************************************************************************** 
  */ 
/* USER CODE END Header */ 

/* Includes ------------------------------------------------------------------*/ 
#include "main.h" 

/* Private includes ----------------------------------------------------------*/ 
/* USER CODE BEGIN Includes */ 
#include "st_logo1.h" 
#include "st_logo2.h" 
/* USER CODE END Includes */ 

/* Private typedef -----------------------------------------------------------*/ 
/* USER CODE BEGIN PTD */ 

/* 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 ---------------------------------------------------------*/ 
LTDC_HandleTypeDef hltdc; 

SPI_HandleTypeDef hspi5; 

/* USER CODE BEGIN PV */ 
LTDC_HandleTypeDef LtdcHandle; 
/* USER CODE END PV */ 

/* Private function prototypes -----------------------------------------------*/ 
void SystemClock_Config(void); 
static void MX_GPIO_Init(void); 
static void MX_SPI5_Init(void); 
static void MX_LTDC_Init(void); 
/* USER CODE BEGIN PFP */ 
static void LCD_Config(void); 
/* 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 */ 

  /* 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_SPI5_Init(); 
  MX_LTDC_Init(); 
  /* USER CODE BEGIN 2 */ 
  LCD_Config(); 
  /* USER CODE END 2 */ 

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

    /* USER CODE BEGIN 3 */ 
  } 
  /* USER CODE END 3 */ 
} 

/** 
  * @brief System Clock Configuration 
  * @retval None 
  */ 
void SystemClock_Config(void) 
{ 
  RCC_ClkInitTypeDef RCC_ClkInitStruct; 
  RCC_OscInitTypeDef RCC_OscInitStruct; 
  RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct; 

  /* Enable Power Control clock */ 
  __HAL_RCC_PWR_CLK_ENABLE(); 

  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */ 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); 

  /*##-1- System Clock Configuration #########################################*/ 
  /* Enable HSE Oscillator and activate PLL with HSE as source */ 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 
  RCC_OscInitStruct.HSEState = RCC_HSE_ON; 
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 
  RCC_OscInitStruct.PLL.PLLM = 8; 
  RCC_OscInitStruct.PLL.PLLN = 360; 
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; 
  RCC_OscInitStruct.PLL.PLLQ = 7; 
  HAL_RCC_OscConfig(&RCC_OscInitStruct); 

  /* Activate the Over-Drive mode */ 
  HAL_PWREx_EnableOverDrive(); 

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */ 
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); 
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); 

  /*##-2- LTDC Clock Configuration ###########################################*/ 
  /* LCD clock configuration */ 
  /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 MHz */ 
  /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN = 192 MHz */ 
  /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAIR = 192/4 = 48 MHz */ 
  /* LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDIVR_8 = 48/8 = 6 MHz */ 
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC; 
  PeriphClkInitStruct.PLLSAI.PLLSAIN = 192; 
  PeriphClkInitStruct.PLLSAI.PLLSAIR = 4; 
  PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_8; 
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); 
} 

/** 
  * @brief LTDC Initialization Function 
  * @param None 
  * @retval None 
  */ 
static void MX_LTDC_Init(void) 
{ 

  /* USER CODE BEGIN LTDC_Init 0 */ 

  /* USER CODE END LTDC_Init 0 */ 

  LTDC_LayerCfgTypeDef pLayerCfg = {0}; 

  /* USER CODE BEGIN LTDC_Init 1 */ 

  /* USER CODE END LTDC_Init 1 */ 
  hltdc.Instance = LTDC; 
  hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL; 
  hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL; 
  hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL; 
  hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC; 
  hltdc.Init.HorizontalSync = 9; 
  hltdc.Init.VerticalSync = 1; 
  hltdc.Init.AccumulatedHBP = 29; 
  hltdc.Init.AccumulatedVBP = 3; 
  hltdc.Init.AccumulatedActiveW = 269; 
  hltdc.Init.AccumulatedActiveH = 323; 
  hltdc.Init.TotalWidth = 279; 
  hltdc.Init.TotalHeigh = 327; 
  hltdc.Init.Backcolor.Blue = 0; 
  hltdc.Init.Backcolor.Green = 0; 
  hltdc.Init.Backcolor.Red = 0; 
  if (HAL_LTDC_Init(&hltdc) != HAL_OK) 
  { 
    Error_Handler(); 
  } 
  pLayerCfg.WindowX0 = 0; 
  pLayerCfg.WindowX1 = 240; 
  pLayerCfg.WindowY0 = 0; 
  pLayerCfg.WindowY1 = 320; 
  pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565; 
  pLayerCfg.Alpha = 255; 
  pLayerCfg.Alpha0 = 0; 
  pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA; 
  pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA; 
  pLayerCfg.FBStartAdress = 0; 
  pLayerCfg.ImageWidth = 240; 
  pLayerCfg.ImageHeight = 320; 
  pLayerCfg.Backcolor.Blue = 0; 
  pLayerCfg.Backcolor.Green = 0; 
  pLayerCfg.Backcolor.Red = 0; 
  if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK) 
  { 
    Error_Handler(); 
  } 
  /* USER CODE BEGIN LTDC_Init 2 */ 

  /* USER CODE END LTDC_Init 2 */ 

} 

/** 
  * @brief SPI5 Initialization Function 
  * @param None 
  * @retval None 
  */ 
static void MX_SPI5_Init(void) 
{ 

  /* USER CODE BEGIN SPI5_Init 0 */ 

  /* USER CODE END SPI5_Init 0 */ 

  /* USER CODE BEGIN SPI5_Init 1 */ 

  /* USER CODE END SPI5_Init 1 */ 
  /* SPI5 parameter configuration*/ 
  hspi5.Instance = SPI5; 
  hspi5.Init.Mode = SPI_MODE_MASTER; 
  hspi5.Init.Direction = SPI_DIRECTION_2LINES; 
  hspi5.Init.DataSize = SPI_DATASIZE_8BIT; 
  hspi5.Init.CLKPolarity = SPI_POLARITY_LOW; 
  hspi5.Init.CLKPhase = SPI_PHASE_1EDGE; 
  hspi5.Init.NSS = SPI_NSS_SOFT; 
  hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; 
  hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB; 
  hspi5.Init.TIMode = SPI_TIMODE_DISABLE; 
  hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; 
  hspi5.Init.CRCPolynomial = 10; 
  if (HAL_SPI_Init(&hspi5) != HAL_OK) 
  { 
    Error_Handler(); 
  } 
  /* USER CODE BEGIN SPI5_Init 2 */ 

  /* USER CODE END SPI5_Init 2 */ 

} 

/** 
  * @brief GPIO Initialization Function 
  * @param None 
  * @retval None 
  */ 
static void MX_GPIO_Init(void) 
{ 

  /* GPIO Ports Clock Enable */ 
  __HAL_RCC_GPIOF_CLK_ENABLE(); 
  __HAL_RCC_GPIOH_CLK_ENABLE(); 
  __HAL_RCC_GPIOA_CLK_ENABLE(); 
  __HAL_RCC_GPIOB_CLK_ENABLE(); 
  __HAL_RCC_GPIOE_CLK_ENABLE(); 
  __HAL_RCC_GPIOD_CLK_ENABLE(); 
  __HAL_RCC_GPIOC_CLK_ENABLE(); 

} 

/* USER CODE BEGIN 4 */ 
static void LCD_Config(void) 
{ 
  LTDC_LayerCfgTypeDef pLayerCfg; 
  LTDC_LayerCfgTypeDef pLayerCfg1; 

  /* Initialization of ILI9341 component*/ 
  ili9341_Init(); 

/* LTDC Initialization -------------------------------------------------------*/ 

  /* Polarity configuration */ 
  /* Initialize the horizontal synchronization polarity as active low */ 
  LtdcHandle.Init.HSPolarity = LTDC_HSPOLARITY_AL; 
  /* Initialize the vertical synchronization polarity as active low */ 
  LtdcHandle.Init.VSPolarity = LTDC_VSPOLARITY_AL; 
  /* Initialize the data enable polarity as active low */ 
  LtdcHandle.Init.DEPolarity = LTDC_DEPOLARITY_AL; 
  /* Initialize the pixel clock polarity as input pixel clock */ 
  LtdcHandle.Init.PCPolarity = LTDC_PCPOLARITY_IPC; 

  /* Timing configuration  (Typical configuration from ILI9341 datasheet) 
      HSYNC=10 (9+1) 
      HBP=20 (29-10+1) 
      ActiveW=240 (269-20-10+1) 
      HFP=10 (279-240-20-10+1) 

      VSYNC=2 (1+1) 
      VBP=2 (3-2+1) 
      ActiveH=320 (323-2-2+1) 
      VFP=4 (327-320-2-2+1) 
  */ 

  /* Timing configuration */ 
  /* Horizontal synchronization width = Hsync - 1 */ 
  LtdcHandle.Init.HorizontalSync = 9; 
  /* Vertical synchronization height = Vsync - 1 */ 
  LtdcHandle.Init.VerticalSync = 1; 
  /* Accumulated horizontal back porch = Hsync + HBP - 1 */ 
  LtdcHandle.Init.AccumulatedHBP = 29; 
  /* Accumulated vertical back porch = Vsync + VBP - 1 */ 
  LtdcHandle.Init.AccumulatedVBP = 3; 
  /* Accumulated active width = Hsync + HBP + Active Width - 1 */ 
  LtdcHandle.Init.AccumulatedActiveH = 323; 
  /* Accumulated active height = Vsync + VBP + Active Heigh - 1 */ 
  LtdcHandle.Init.AccumulatedActiveW = 269; 
  /* Total height = Vsync + VBP + Active Heigh + VFP - 1 */ 
  LtdcHandle.Init.TotalHeigh = 327; 
  /* Total width = Hsync + HBP + Active Width + HFP - 1 */ 
  LtdcHandle.Init.TotalWidth = 279; 

  /* Configure R,G,B component values for LCD background color */ 
  LtdcHandle.Init.Backcolor.Blue = 0; 
  LtdcHandle.Init.Backcolor.Green = 0; 
  LtdcHandle.Init.Backcolor.Red = 0; 

  LtdcHandle.Instance = LTDC; 

/* Layer1 Configuration ------------------------------------------------------*/ 

  /* Windowing configuration */ 
  pLayerCfg.WindowX0 = 0; 
  pLayerCfg.WindowX1 = 240; 
  pLayerCfg.WindowY0 = 0; 
  pLayerCfg.WindowY1 = 160; 

  /* Pixel Format configuration*/ 
  pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565; 

  /* Start Address configuration : frame buffer is located at FLASH memory */ 
  pLayerCfg.FBStartAdress = (uint32_t)&ST_LOGO_1; 

  /* Alpha constant (255 totally opaque) */ 
  pLayerCfg.Alpha = 255; 

  /* Default Color configuration (configure A,R,G,B component values) */ 
  pLayerCfg.Alpha0 = 0; 
  pLayerCfg.Backcolor.Blue = 0; 
  pLayerCfg.Backcolor.Green = 0; 
  pLayerCfg.Backcolor.Red = 0; 

  /* Configure blending factors */ 
  pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA; 
  pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA; 

  /* Configure the number of lines and number of pixels per line */ 
  pLayerCfg.ImageWidth = 240; 
  pLayerCfg.ImageHeight = 160; 

/* Layer2 Configuration ------------------------------------------------------*/ 

  /* Windowing configuration */ 
  pLayerCfg1.WindowX0 = 0; 
  pLayerCfg1.WindowX1 = 240; 
  pLayerCfg1.WindowY0 = 160; 
  pLayerCfg1.WindowY1 = 320; 

  /* Pixel Format configuration*/ 
  pLayerCfg1.PixelFormat = LTDC_PIXEL_FORMAT_RGB565; 

  /* Start Address configuration : frame buffer is located at FLASH memory */ 
  pLayerCfg1.FBStartAdress = (uint32_t)&ST_LOGO_2; 

  /* Alpha constant (255 totally opaque) */ 
  pLayerCfg1.Alpha = 200; 

  /* Default Color configuration (configure A,R,G,B component values) */ 
  pLayerCfg1.Alpha0 = 0; 
  pLayerCfg1.Backcolor.Blue = 0; 
  pLayerCfg1.Backcolor.Green = 0; 
  pLayerCfg1.Backcolor.Red = 0; 

  /* Configure blending factors */ 
  pLayerCfg1.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA; 
  pLayerCfg1.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA; 

  /* Configure the number of lines and number of pixels per line */ 
  pLayerCfg1.ImageWidth = 240; 
  pLayerCfg1.ImageHeight = 160; 

  /* Configure the LTDC */ 
  if(HAL_LTDC_Init(&LtdcHandle) != HAL_OK) 
  { 
    /* Initialization Error */ 
    Error_Handler(); 
  } 

  /* Configure the Background Layer*/ 
  if(HAL_LTDC_ConfigLayer(&LtdcHandle, &pLayerCfg, 0) != HAL_OK) 
  { 
    /* Initialization Error */ 
    Error_Handler(); 
  } 

  /* Configure the Foreground Layer*/ 
  if(HAL_LTDC_ConfigLayer(&LtdcHandle, &pLayerCfg1, 1) != HAL_OK) 
  { 
    /* Initialization Error */ 
    Error_Handler(); 
  } 
} 
/* USER CODE END 4 */ 

/** 
  * @brief  This function is executed in case of error occurrence. 
  * @retval None 
  */ 
void Error_Handler(void) 
{ 
  /* USER CODE BEGIN Error_Handler_Debug */ 
  /* User can add his own implementation to report the HAL error return state */ 

  /* USER CODE END Error_Handler_Debug */ 
} 

#ifdef  USE_FULL_ASSERT 
/** 
  * @brief  Reports the name of the source file and the source line number 
  *         where the assert_param error has occurred. 
  * @param  file: pointer to the source file name 
  * @param  line: assert_param error line source number 
  * @retval None 
  */ 
void assert_failed(uint8_t *file, uint32_t line) 
{  
  /* USER CODE BEGIN 6 */ 
  /* User can add his own implementation to report the file name and line number, 
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 
  /* USER CODE END 6 */ 
} 
#endif /* USE_FULL_ASSERT */ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 

 

 

https://hnydiy.tistory.com/216

 

16. [STM32F429] LCD + 터치 스크린 사용 방법

stm32f429 는 터치 스크린이 포함되어 있습니다. 스텝 으로 나눠서 차근차근 사용해보겠습니다. 도움이 되셨다면 댓글좀..달아주세요 ㅠㅠ 너므 외로워...😢😢😢😢 step1 여기 까지가 LCD 설정 입��

hnydiy.tistory.com

 

반응형