找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 772|回复: 0
收起左侧

STM32单片机HAL库驱动OLED屏幕程序

[复制链接]
ID:1064538 发表于 2023-3-17 12:10 | 显示全部楼层 |阅读模式
IIC SPI接口都有,移植性适用

单片机源程序如下:
  1. /* USER CODE END Header */
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "tim.h"
  5. #include "usart.h"
  6. #include "gpio.h"

  7. /* Private includes ----------------------------------------------------------*/
  8. /* USER CODE BEGIN Includes */

  9. #include "lcd_init.h"    //OLED头文件
  10. #include "pic.h"         //图片头文件
  11. #include "lcd.h"

  12. /* USER CODE END Includes */

  13. /* Private typedef -----------------------------------------------------------*/
  14. /* USER CODE BEGIN PTD */

  15. //             OLED屏幕接口描述
  16. //              GND   电源地
  17. //              VCC   3.3v电源
  18. //              SCL   PA0(SCLK)
  19. //              SDA   PA1(MOSI)
  20. //              RES   PA2
  21. //              DC    PA3
  22. //              CS    PA4
  23. //              BLK   PA5


  24. /* USER CODE END PTD */

  25. /* Private define ------------------------------------------------------------*/
  26. /* USER CODE BEGIN PD */
  27. /* USER CODE END PD */

  28. /* Private macro -------------------------------------------------------------*/
  29. /* USER CODE BEGIN PM */

  30. void HAL_Delay_us(uint32_t Delay_us);

  31. /* USER CODE END PM */

  32. /* Private variables ---------------------------------------------------------*/

  33. /* USER CODE BEGIN PV */

  34. /* USER CODE END PV */

  35. /* Private function prototypes -----------------------------------------------*/
  36. void SystemClock_Config(void);
  37. /* USER CODE BEGIN PFP */

  38. /* USER CODE END PFP */

  39. /* Private user code ---------------------------------------------------------*/
  40. /* USER CODE BEGIN 0 */

  41. /* USER CODE END 0 */

  42. /**
  43.   * @brief  The application entry point.
  44.   * @retval int
  45.   */
  46. int main(void)
  47. {
  48.   /* USER CODE BEGIN 1 */

  49.   /* USER CODE END 1 */

  50.   /* MCU Configuration--------------------------------------------------------*/

  51.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  52.   HAL_Init();

  53.   /* USER CODE BEGIN Init */
  54.    
  55.         float t=0;

  56.   /* USER CODE END Init */

  57.   /* Configure the system clock */
  58.   SystemClock_Config();

  59.   /* USER CODE BEGIN SysInit */

  60.   /* USER CODE END SysInit */

  61.   /* Initialize all configured peripherals */
  62.   MX_GPIO_Init();
  63.   MX_TIM2_Init();
  64.   MX_USART1_UART_Init();
  65.   /* USER CODE BEGIN 2 */
  66.   
  67.           LCD_Init();                         //LCD初始化
  68.         LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
  69.   

  70.   /* USER CODE END 2 */

  71.   /* Infinite loop */
  72.   /* USER CODE BEGIN WHILE */
  73.   while (1)
  74.   {
  75.     /* USER CODE END WHILE */
  76.       
  77.                 LCD_ShowChinese(40,0,"中景园电子",RED,WHITE,16,0);
  78.                 LCD_ShowString(10,20,"LCD_W:",RED,WHITE,16,0);
  79.                 LCD_ShowIntNum(58,20,LCD_W,3,RED,WHITE,16);
  80.                 LCD_ShowString(10,40,"LCD_H:",RED,WHITE,16,0);
  81.                 LCD_ShowIntNum(58,40,LCD_H,3,RED,WHITE,16);
  82.                 LCD_ShowFloatNum1(10,60,t,4,RED,WHITE,16);
  83.                 t+=0.11;
  84.                 LCD_ShowPicture(100,20,40,40,gImage_1);

  85.     /* USER CODE BEGIN 3 */
  86.   }
  87.   /* USER CODE END 3 */
  88. }

  89. /**
  90.   * @brief System Clock Configuration
  91.   * @retval None
  92.   */
  93. void SystemClock_Config(void)
  94. {
  95.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  96.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  97.   /** Initializes the RCC Oscillators according to the specified parameters
  98.   * in the RCC_OscInitTypeDef structure.
  99.   */
  100.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  101.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  102.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  103.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  104.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  105.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  106.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  107.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  108.   {
  109.     Error_Handler();
  110.   }

  111.   /** Initializes the CPU, AHB and APB buses clocks
  112.   */
  113.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  114.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  115.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  116.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  117.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  118.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  119.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  120.   {
  121.     Error_Handler();
  122.   }
  123. }

  124. /* USER CODE BEGIN 4 */

  125. // Core\Src\delay.c
  126. void HAL_Delay_us(uint32_t Delay_us)
  127. {

  128.                 __HAL_TIM_SetCounter(&htim2, 0);

  129.                 __HAL_TIM_ENABLE(&htim2);

  130.                 while(__HAL_TIM_GetCounter(&htim2) < Delay_us);
  131.                 /* Disable the Peripheral */
  132.                 __HAL_TIM_DISABLE(&htim2);


  133. }

  134. /* USER CODE END 4 */

  135. /**
  136.   * @brief  This function is executed in case of error occurrence.
  137.   * @retval None
  138.   */
  139. void Error_Handler(void)
  140. {
  141.   /* USER CODE BEGIN Error_Handler_Debug */
  142.   /* User can add his own implementation to report the HAL error return state */
  143.   __disable_irq();
  144.   while (1)
  145.   {
  146.   }
  147.   /* USER CODE END Error_Handler_Debug */
  148. }

  149. #ifdef  USE_FULL_ASSERT
  150. /**
  151.   * @brief  Reports the name of the source file and the source line number
  152.   *         where the assert_param error has occurred.
  153.   * @param  file: pointer to the source file name
  154.   * @param  line: assert_param error line source number
  155.   * @retval None
  156.   */
  157. void assert_failed(uint8_t *file, uint32_t line)
  158. {
  159.   /* USER CODE BEGIN 6 */
  160.   /* User can add his own implementation to report the file name and line number,
  161.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  162.   /* USER CODE END 6 */
  163. }
  164. #endif /* USE_FULL_ASSERT */
复制代码

HAL_OLED_SPI.7z

5.19 MB, 下载次数: 27, 下载积分: 黑币 -5

HAL_OLED_IIC.7z

5.19 MB, 下载次数: 33, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表