当前位置:   article > 正文

STM32(HAL库)软件IIC驱动OLED_stm32 hal iic oled

stm32 hal iic oled

目录

1、简介

2、CubeMX初始化配置

2.1 基础配置

2.1.1 SYS配置

 2.1.2 RCC配置

2.2 软件IIC引脚配置

2.3 项目生成

 3、KEIL端程序整合

3.1 OLED驱动添加

3.3 主函数代

3.4 效果展示


1、简介

本文通过STM32F103C8T6单片机(HAL库)通过软件IIC方式对OLED进行驱动。

2、CubeMX初始化配置

2.1 基础配置

2.1.1 SYS配置

 2.1.2 RCC配置

2.2 软件IIC引脚配置

2.3 项目生成

 3、KEIL端程序整合

3.1 OLED驱动添加

首先在建立Hardware文件,添加OLED.c如下图所示:

 接着对OLED.H进行添加如下图所示: 

OLED驱动链接:

OLED https://www.aliyundrive.com/s/BM82mufCiQc 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

3.3 主函数代码

首先引用头文件,如下所示:

#include "OLED.h"

接着初始化OLED,如下所示:

OLED_Init();  //初始化OLED

最后进行数据读取打印,如下所示:

OLED_ShowString(1, 1, "1");

main.c如下所示:

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "OLED.h"
  27. #include "SHT3x.h"
  28. uint8_t adc_mq2;
  29. float Temp,Hum; //声明变量存放温湿度数据
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39. /* USER CODE END PM */
  40. /* Private variables ---------------------------------------------------------*/
  41. /* USER CODE BEGIN PV */
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. /* USER CODE BEGIN PFP */
  46. /* USER CODE END PFP */
  47. /* Private user code ---------------------------------------------------------*/
  48. /* USER CODE BEGIN 0 */
  49. /* USER CODE END 0 */
  50. /**
  51. * @brief The application entry point.
  52. * @retval int
  53. */
  54. int main(void)
  55. {
  56. /* USER CODE BEGIN 1 */
  57. /* USER CODE END 1 */
  58. /* MCU Configuration--------------------------------------------------------*/
  59. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  60. HAL_Init();
  61. /* USER CODE BEGIN Init */
  62. /* USER CODE END Init */
  63. /* Configure the system clock */
  64. SystemClock_Config();
  65. /* USER CODE BEGIN SysInit */
  66. /* USER CODE END SysInit */
  67. /* Initialize all configured peripherals */
  68. MX_GPIO_Init();
  69. MX_ADC1_Init();
  70. MX_USART1_UART_Init();
  71. /* USER CODE BEGIN 2 */
  72. OLED_Init(); //初始化OLED
  73. SHT3x_Init(); //初始化SHT3x
  74. HAL_ADCEx_Calibration_Start(&hadc1);//ADC采样校准
  75. /* USER CODE END 2 */
  76. /* Infinite loop */
  77. /* USER CODE BEGIN WHILE */
  78. while (1)
  79. {
  80. OLED_ShowString(1, 1, "1");
  81. /* USER CODE END WHILE */
  82. /* USER CODE BEGIN 3 */
  83. }
  84. /* USER CODE END 3 */
  85. }
  86. /**
  87. * @brief System Clock Configuration
  88. * @retval None
  89. */
  90. void SystemClock_Config(void)
  91. {
  92. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  93. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  94. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  95. /** Initializes the RCC Oscillators according to the specified parameters
  96. * in the RCC_OscInitTypeDef structure.
  97. */
  98. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  99. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  100. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  101. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  102. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  103. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  104. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  105. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  106. {
  107. Error_Handler();
  108. }
  109. /** Initializes the CPU, AHB and APB buses clocks
  110. */
  111. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  112. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  113. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  114. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  115. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  116. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  117. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  118. {
  119. Error_Handler();
  120. }
  121. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  122. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  123. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  124. {
  125. Error_Handler();
  126. }
  127. }
  128. /* USER CODE BEGIN 4 */
  129. /* USER CODE END 4 */
  130. /**
  131. * @brief This function is executed in case of error occurrence.
  132. * @retval None
  133. */
  134. void Error_Handler(void)
  135. {
  136. /* USER CODE BEGIN Error_Handler_Debug */
  137. /* User can add his own implementation to report the HAL error return state */
  138. __disable_irq();
  139. while (1)
  140. {
  141. }
  142. /* USER CODE END Error_Handler_Debug */
  143. }
  144. #ifdef USE_FULL_ASSERT
  145. /**
  146. * @brief Reports the name of the source file and the source line number
  147. * where the assert_param error has occurred.
  148. * @param file: pointer to the source file name
  149. * @param line: assert_param error line source number
  150. * @retval None
  151. */
  152. void assert_failed(uint8_t *file, uint32_t line)
  153. {
  154. /* USER CODE BEGIN 6 */
  155. /* User can add his own implementation to report the file name and line number,
  156. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  157. /* USER CODE END 6 */
  158. }
  159. #endif /* USE_FULL_ASSERT */

3.4 效果展示

上文如有错误,恳请各位大佬指正。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号