STM32F1基于HAL库DMA方式驱动ST7735 1.8‘‘屏幕demo

STM32F1基于HAL库DMA方式驱动ST7735 1.8’'屏幕demo


  • 相关篇《STM32F1基于STM32CubeMX配置硬件SPI驱动1.8寸TFT LCD128X160 ST7735S屏幕》

  • 相关ST7735驱动代码来源于基于STM32CubeIDE项目:https://github.com/ScarsFun/STM32F103C8T6_cubeIDE_ST7735_1.8_DMA

  • demo驱动效果:

配置如下:

  • SPI1配置如下:
    STM32F1基于HAL库DMA方式驱动ST7735 1.8‘‘屏幕demo_第1张图片

  • SPI其他自定义引脚配置
    STM32F1基于HAL库DMA方式驱动ST7735 1.8‘‘屏幕demo_第2张图片

  • DMA传输方式设置:
    STM32F1基于HAL库DMA方式驱动ST7735 1.8‘‘屏幕demo_第3张图片

添加相关驱动文件到工程

  • 添加驱动文件到对应的User目录下。
    STM32F1基于HAL库DMA方式驱动ST7735 1.8‘‘屏幕demo_第4张图片

做好了相关源文件添加后,就无需再补充其他地方了。

数据传输方式和屏幕切换说明

  • 在工程st7735_cfg.h文件中,修改SPI接口以及是否启用DMA传输以及屏幕型号切换
#define ST7735_SPI_PORT hspi1	//hspi1, hspi2, hspi3...
#define USE_SPI_DMA			//if used DMA for SPI bus

#define ST7735_1_8_DEFAULT_ORIENTATION	// AliExpress/eBay 1.8" display, default orientation 合宙的1.8"选择这个
//#define ST7735S_1_8_DEFAULT_ORIENTATION 	// WaveShare ST7735S-based 1.8" display, default orientation这是微雪的
//#define ST7735_1_44_DEFAULT_ORIENTATION 	// 1.44" display, default orientation
//#define ST7735_MINI_DEFAULT_ORIENTATION 	// mini 160x80 display (it's unlikely you want the default orientation)

  • main.c文件内容
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * 

© Copyright (c) 2019 STMicroelectronics. * All rights reserved.

* * 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 "dma.h" #include "spi.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "st7735.h" #include "fonts.h" #include "testimg.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ uint8_t r = 0; /* 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); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void demoTFT(void) { ST7735_SetRotation(r); ST7735_FillScreen(ST7735_BLACK); for(int x = 0; x < ST7735_GetWidth(); x++) { ST7735_DrawPixel(x, 0, ST7735_WHITE); ST7735_DrawPixel(x, ST7735_GetHeight() - 1, ST7735_WHITE); } for(int y = 0; y < ST7735_GetHeight(); y++) { ST7735_DrawPixel(0, y, ST7735_WHITE); ST7735_DrawPixel(ST7735_GetWidth() - 1, y, ST7735_WHITE); } ST7735_DrawLine(0, 0, ST7735_GetWidth(), ST7735_GetHeight(), ST7735_WHITE); ST7735_DrawLine(ST7735_GetWidth(), 0, 0, ST7735_GetHeight(), ST7735_WHITE); HAL_Delay(2000); ST7735_FillScreen(ST7735_BLACK); for (int i = 0; i < ST7735_GetHeight(); i += 4) { ST7735_DrawFastHLine(0, i, ST7735_GetWidth() - 1, ST7735_WHITE); } for (int i = 0; i < ST7735_GetWidth(); i += 4) { ST7735_DrawFastVLine(i, 0, ST7735_GetHeight() - 1, ST7735_WHITE); } HAL_Delay(2000); // Check fonts ST7735_FillScreen(ST7735_BLACK); ST7735_DrawString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK); ST7735_DrawString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK); ST7735_DrawString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK); HAL_Delay(2000); // Check colors ST7735_FillScreen(ST7735_BLACK); ST7735_DrawString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_BLUE); ST7735_DrawString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE); HAL_Delay(500); ST7735_FillScreen(ST7735_RED); ST7735_DrawString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED); HAL_Delay(500); ST7735_FillScreen(ST7735_GREEN); ST7735_DrawString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN); HAL_Delay(500); ST7735_FillScreen(ST7735_CYAN); ST7735_DrawString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN); HAL_Delay(500); ST7735_FillScreen(ST7735_MAGENTA); ST7735_DrawString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA); HAL_Delay(500); ST7735_FillScreen(ST7735_YELLOW); ST7735_DrawString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW); HAL_Delay(500); ST7735_FillScreen(ST7735_WHITE); ST7735_DrawString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE); HAL_Delay(500); // Draw circles ST7735_FillScreen(ST7735_BLACK); for (int i = 0; i < ST7735_GetHeight() / 2; i += 2) { ST7735_DrawCircle(ST7735_GetWidth() / 2, ST7735_GetHeight() / 2, i, ST7735_YELLOW); } HAL_Delay(1000); ST7735_FillScreen(ST7735_BLACK); ST7735_FillTriangle(0, 0, ST7735_GetWidth() / 2, ST7735_GetHeight(), ST7735_GetWidth(), 0, ST7735_RED); HAL_Delay(1000); ST7735_FillScreen(ST7735_BLACK); ST7735_DrawImage(0, 0, 128, 128, (uint16_t*) test_img_128x128); HAL_Delay(3000); r++; } /* 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_DMA_Init(); MX_SPI1_Init(); /* USER CODE BEGIN 2 */ ST7735_Init(); ST7735_Backlight_On(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { demoTFT(); /* 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 */ /* 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 */

驱动工程源码

链接:https://pan.baidu.com/s/1mJGcKRXdi-0Sj9nf2q5Jgw 
提取码:qw7g

你可能感兴趣的:(stm32,ST7735)