用標準庫的代碼可以正常驅動ILI9341,把代碼粘貼到HAL的工程里就不行了,這是為什么?那位大佬幫忙看下
ILI9341.7z
(252.02 KB, 下載次數: 2)
2021-5-24 15:32 上傳
點擊文件名下載附件
//8位模式接線:8位模式9341模塊接高8位總線,即:
//液晶屏LCD_D0對應單片機PA0
//液晶屏LCD_D1對應單片機PA1
//液晶屏LCD_D2對應單片機PA2
//液晶屏LCD_D3對應單片機PA3
//液晶屏LCD_D4對應單片機PA4
//液晶屏LCD_D5對應單片機PA5
//液晶屏LCD_D6對應單片機PA6
//液晶屏LCD_D7對應單片機PA7
//LCD_RST 接PB9 //復位信號
//LCD_CS 接PB8 //片選信號
//LCD_RS 接PB7 //寄存器/數據選擇信號
//LCD_WR 接PB6 //寫信號
//LCD_RD 接PB5 //讀信號
MAIN.C
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 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 "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- void LCD_Init(void);
- void LCD_SetParam(void);
- void LCD_RESET(void);
- void LCD_WR_REG(uint8_t data);
- void LCD_WR_DATA(uint16_t data);
- void LCD_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t color);
- void LCD_SetWindows(uint16_t xStar, uint16_t yStar,uint16_t xEnd,uint16_t yEnd);
- void LCD_WriteRAM_Prepare(void);
- void LCD_Clear(uint16_t Color);
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- #define WHITE 0xFFFF
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define BRED 0XF81F
- #define GRED 0XFFE0
- #define GBLUE 0X07FF
- #define RED 0xF800
- #define MAGENTA 0xF81F
- #define GREEN 0x07E0
- #define CYAN 0x7FFF
- #define YELLOW 0xFFE0
- #define BROWN 0XBC40 //棕色
- #define BRRED 0XFC07 //棕紅色
- #define GRAY 0X8430 //灰色
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- #define LCD_CS_SET GPIOB->BSRR=1<<8 //片選端口 PB8
- #define LCD_RS_SET GPIOB->BSRR=1<<7 //數據/命令 PB7
- #define LCD_WR_SET GPIOB->BSRR=1<<6 //寫數據 PB6
- #define LCD_RD_SET GPIOB->BSRR=1<<5 //讀數據 PB5
- #define LCD_RST_SET GPIOB->BSRR=1<<9 //復位 PB9
-
- #define LCD_CS_CLR GPIOB->BRR=1<<8 //片選端口 PB8
- #define LCD_RS_CLR GPIOB->BRR=1<<7 //數據/命令 PB7
- #define LCD_WR_CLR GPIOB->BRR=1<<6 //寫數據 PB6
- #define LCD_RD_CLR GPIOB->BRR=1<<5 //讀數據 PB5
- #define LCD_RST_CLR GPIOB->BRR=1<<9 //復位 PB9
- #define DATAOUT(x) GPIOA->ODR=x>>8; //數據輸出
- #define DATAIN GPIOA->IDR; //數據輸入
- #define USE_HORIZONTAL 1 //定義是否使用橫屏 0,不使用.1,使用.
- #define LCD_USE8BIT_MODEL 1 //定義數據總線是否使用8位模式 0,使用16位模式.1,使用8位模式
- //LCD重要參數集
- typedef struct
- {
- uint16_t width; //LCD 寬度
- uint16_t height; //LCD 高度
- uint16_t id; //LCD ID
- uint8_t dir; //橫屏還是豎屏控制:0,豎屏;1,橫屏。
- uint16_t wramcmd; //開始寫gram指令
- uint16_t setxcmd; //設置x坐標指令
- uint16_t setycmd; //設置y坐標指令
- }_lcd_dev;
- //LCD參數
- _lcd_dev lcddev; //管理LCD重要參數
- #define LCD_W 320
- #define LCD_H 240
- //TFTLCD部分外要調用的函數
- extern uint16_t POINT_COLOR;//默認紅色
- extern uint16_t BACK_COLOR; //背景顏色.默認為白色
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_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 */
- /* 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_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- LCD_Init(); //液晶屏初始化
- HAL_Delay(120);
- LCD_Clear(RED);
- /* 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_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- void LCD_Init()
- {
- LCD_RESET();
- HAL_Delay(120);
- //************* Start Initial Sequence **********//
- LCD_WR_REG(0xCF);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0xC1);
- LCD_WR_DATA(0X30);
- LCD_WR_REG(0xED);
- LCD_WR_DATA(0x64);
- LCD_WR_DATA(0x03);
- LCD_WR_DATA(0X12);
- LCD_WR_DATA(0X81);
- LCD_WR_REG(0xE8);
- LCD_WR_DATA(0x85);
- LCD_WR_DATA(0x10);
- LCD_WR_DATA(0x7A);
- LCD_WR_REG(0xCB);
- LCD_WR_DATA(0x39);
- LCD_WR_DATA(0x2C);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x34);
- LCD_WR_DATA(0x02);
- LCD_WR_REG(0xF7);
- LCD_WR_DATA(0x20);
- LCD_WR_REG(0xEA);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_REG(0xC0); //Power control
- LCD_WR_DATA(0x1B); //VRH[5:0]
- LCD_WR_REG(0xC1); //Power control
- LCD_WR_DATA(0x01); //SAP[2:0];BT[3:0]
- LCD_WR_REG(0xC5); //VCM control
- LCD_WR_DATA(0x30); //3F
- LCD_WR_DATA(0x30); //3C
- LCD_WR_REG(0xC7); //VCM control2
- LCD_WR_DATA(0XB7);
- LCD_WR_REG(0x36); // Memory Access Control
- LCD_WR_DATA(0x48);
- LCD_WR_REG(0x3A);
- LCD_WR_DATA(0x55);
- LCD_WR_REG(0xB1);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x1A);
- LCD_WR_REG(0xB6); // Display Function Control
- LCD_WR_DATA(0x0A);
- LCD_WR_DATA(0xA2);
- LCD_WR_REG(0xF2); // 3Gamma Function Disable
- LCD_WR_DATA(0x00);
- LCD_WR_REG(0x26); //Gamma curve selected
- LCD_WR_DATA(0x01);
- LCD_WR_REG(0xE0); //Set Gamma
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x2A);
- LCD_WR_DATA(0x28);
- LCD_WR_DATA(0x08);
- LCD_WR_DATA(0x0E);
- LCD_WR_DATA(0x08);
- LCD_WR_DATA(0x54);
- LCD_WR_DATA(0XA9);
- LCD_WR_DATA(0x43);
- LCD_WR_DATA(0x0A);
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_REG(0XE1); //Set Gamma
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x15);
- LCD_WR_DATA(0x17);
- LCD_WR_DATA(0x07);
- LCD_WR_DATA(0x11);
- LCD_WR_DATA(0x06);
- LCD_WR_DATA(0x2B);
- LCD_WR_DATA(0x56);
- LCD_WR_DATA(0x3C);
- LCD_WR_DATA(0x05);
- LCD_WR_DATA(0x10);
- LCD_WR_DATA(0x0F);
- LCD_WR_DATA(0x3F);
- LCD_WR_DATA(0x3F);
- LCD_WR_DATA(0x0F);
- LCD_WR_REG(0x2B);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x01);
- LCD_WR_DATA(0x3f);
- LCD_WR_REG(0x2A);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0x00);
- LCD_WR_DATA(0xef);
- LCD_WR_REG(0x11); //Exit Sleep
- HAL_Delay(120);
- LCD_WR_REG(0x29); //display on
-
- LCD_SetParam();
- }
- void LCD_SetParam(void)
- {
- lcddev.wramcmd=0x2C;
- lcddev.dir=1;//橫屏
- lcddev.width=LCD_W;
- lcddev.height=LCD_H;
- lcddev.setxcmd=0x2A;
- lcddev.setycmd=0x2B;
- }
- void LCD_RESET(void)
- {
- LCD_RST_CLR;
- HAL_Delay(150);
- LCD_RST_SET;
- HAL_Delay(50);
- }
- void LCD_WR_REG(uint8_t data)
- {
- LCD_RS_CLR;
- LCD_CS_CLR;
- DATAOUT(data<<8);
- LCD_WR_CLR;
- LCD_WR_SET;
- LCD_CS_SET;
- }
- void LCD_WR_DATA(uint16_t data)
- {
- LCD_RS_SET;
- LCD_CS_CLR;
- DATAOUT(data<<8);
- LCD_WR_CLR;
- LCD_WR_SET;
- LCD_CS_SET;
- }
- void LCD_SetWindows(uint16_t xStar, uint16_t yStar,uint16_t xEnd,uint16_t yEnd)
- {
- LCD_WR_REG(lcddev.setxcmd);
- LCD_WR_DATA(xStar>>8);
- LCD_WR_DATA(0x00FF&xStar);
- LCD_WR_DATA(xEnd>>8);
- LCD_WR_DATA(0x00FF&xEnd);
- LCD_WR_REG(lcddev.setycmd);
- LCD_WR_DATA(yStar>>8);
- LCD_WR_DATA(0x00FF&yStar);
- LCD_WR_DATA(yEnd>>8);
- LCD_WR_DATA(0x00FF&yEnd);
- LCD_WriteRAM_Prepare(); //開始寫入GRAM
- }
- void LCD_WriteRAM_Prepare(void)
- {
- LCD_WR_REG(lcddev.wramcmd);
- }
- void LCD_Clear(uint16_t Color)
- {
- uint32_t index=0;
- LCD_SetWindows(0,0,lcddev.width-1,lcddev.height-1);
- LCD_RS_SET;//寫數據
- LCD_CS_CLR;
- for(index=0;index<lcddev.width*lcddev.height;index++)
- {
- DATAOUT(Color);
- LCD_WR_CLR;
- LCD_WR_SET;
-
- DATAOUT(Color<<8);
- LCD_WR_CLR;
- LCD_WR_SET;
- }
- LCD_CS_SET;
- }
- /* 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 */
- __disable_irq();
- while (1)
- {
- }
- /* 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,
- ex: 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****/
復制代碼
GPIO.C
- /**
- ******************************************************************************
- * @file gpio.c
- * @brief This file provides code for the configuration
- * of all used GPIO pins.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 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
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "gpio.h"
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /*----------------------------------------------------------------------------*/
- /* Configure GPIO */
- /*----------------------------------------------------------------------------*/
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, LCD_D0_Pin|LCD_D1_Pin|LCD_D2_Pin|LCD_D3_Pin
- |LCD_D4_Pin|LCD_D5_Pin|LCD_D6_Pin|LCD_D7_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, LCD_RD_Pin|LCD_WR_Pin|LCD_RS_Pin|LCD_CS_Pin
- |LCD_RST_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pins : PAPin PAPin PAPin PAPin
- PAPin PAPin PAPin PAPin */
- GPIO_InitStruct.Pin = LCD_D0_Pin|LCD_D1_Pin|LCD_D2_Pin|LCD_D3_Pin
- |LCD_D4_Pin|LCD_D5_Pin|LCD_D6_Pin|LCD_D7_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : PBPin PBPin PBPin PBPin
- PBPin */
- GPIO_InitStruct.Pin = LCD_RD_Pin|LCD_WR_Pin|LCD_RS_Pin|LCD_CS_Pin
- |LCD_RST_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復制代碼 |