[STM32C0] 【NUCLEO-C092RC】驱动OLED显示屏OLED 1306

[复制链接]
 楼主| meiyaolei 发表于 2025-5-26 19:32 | 显示全部楼层 |阅读模式
本帖最后由 meiyaolei 于 2025-5-26 19:34 编辑

使用NUCLEO-C092RC开发板驱动SSD1306 OLED显示屏,通过I2C或SPI接口实现,以下为关键步骤和代码示例:

硬件
引脚对应关系(I2C接口示例)
SCL → STM32C092的I2C时钟引脚PB8对应I2C1_SCL
SDA → STM32C092的I2C数据引脚PB9对应I2C1_SDA
VCC → 5V
GND → 地
SSD1306的I2C地址通常为0x3C(部分型号为0x3D),实际代码中是用的0x78。

硬件准备
I2C上拉电阻:SCL和SDA需接4.7kΩ外部上拉电阻。


原理图
9549268344a9e8a569.png

软件配置
使用STM32CubeMX配置进行IIC配置,把IIC配置成快速模式(Fast Mode),快速模式的最大数据传输速率为400 kbps.

505768344e769f481.png
代码:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]  i2c.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]  This file provides code for the configuration
  6.   *          of the I2C instances.
  7.   ******************************************************************************
  8.   * [url=home.php?mod=space&uid=93590]@Attention[/url]  *
  9.   * Copyright (c) 2025 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 "i2c.h"  // 包含I2C相关的头文件

  21. /* USER CODE BEGIN 0 */

  22. /* USER CODE END 0 */

  23. I2C_HandleTypeDef hi2c1;  // 定义I2C1的句柄,用于管理和控制I2C1外设

  24. /* I2C1 init function */
  25. void MX_I2C1_Init(void)
  26. {

  27.   /* USER CODE BEGIN I2C1_Init 0 */

  28.   /* USER CODE END I2C1_Init 0 */

  29.   /* USER CODE BEGIN I2C1_Init 1 */

  30.   /* USER CODE END I2C1_Init 1 */
  31.   hi2c1.Instance = I2C1;  // 指定使用的I2C实例为I2C1
  32.   hi2c1.Init.Timing = 0x0090194B;  // 设置I2C的时序参数,用于配置SCL线的时间特性
  33.   hi2c1.Init.OwnAddress1 = 0;  // 设置I2C设备的主地址(7位地址模式),此处为0表示不启用
  34.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;  // 设置地址模式为7位地址
  35.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;  // 禁用双地址模式
  36.   hi2c1.Init.OwnAddress2 = 0;  // 设置第二个地址(如果双地址模式启用),此处为0
  37.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;  // 设置第二个地址的掩码,此处为无掩码
  38.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;  // 禁用通用呼叫模式
  39.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;  // 禁用时钟拉伸模式
  40.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)  // 初始化I2C1
  41.   {
  42.     Error_Handler();  // 如果初始化失败,调用错误处理函数
  43.   }

  44.   /** Configure Analogue filter
  45.   */
  46.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)  // 启用模拟滤波器
  47.   {
  48.     Error_Handler();  // 如果配置失败,调用错误处理函数
  49.   }

  50.   /** Configure Digital filter
  51.   */
  52.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)  // 配置数字滤波器,此处为禁用
  53.   {
  54.     Error_Handler();  // 如果配置失败,调用错误处理函数
  55.   }
  56.   /* USER CODE BEGIN I2C1_Init 2 */

  57.   /* USER CODE END I2C1_Init 2 */

  58. }

  59. /**
  60.   * [url=home.php?mod=space&uid=247401]@brief[/url] I2C MSP Initialization
  61.   *        This function configures the hardware resources used in this example:
  62.   *           - Peripheral's clock enable
  63.   *           - Peripheral's GPIO Configuration
  64.   * @param i2cHandle: I2C handle pointer
  65.   * @retval None
  66.   */
  67. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  68. {

  69.   GPIO_InitTypeDef GPIO_InitStruct = {0};  // 定义GPIO初始化结构体
  70.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};  // 定义外设时钟初始化结构体
  71.   if(i2cHandle->Instance==I2C1)  // 检查是否为I2C1实例
  72.   {
  73.   /* USER CODE BEGIN I2C1_MspInit 0 */

  74.   /* USER CODE END I2C1_MspInit 0 */

  75.   /** Initializes the peripherals clocks
  76.   */
  77.     PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;  // 选择I2C1时钟
  78.     PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;  // 设置I2C1时钟源为APB1时钟
  79.     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)  // 配置外设时钟
  80.     {
  81.       Error_Handler();  // 如果配置失败,调用错误处理函数
  82.     }

  83.     __HAL_RCC_GPIOB_CLK_ENABLE();  // 启用GPIOB时钟
  84.     /**I2C1 GPIO Configuration
  85.     PB8     ------> I2C1_SCL
  86.     PB9     ------> I2C1_SDA
  87.     */
  88.     GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;  // 设置GPIO引脚为PB8和PB9
  89.     GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;  // 设置GPIO模式为复用开漏输出(I2C需要开漏输出)
  90.     GPIO_InitStruct.Pull = GPIO_NOPULL;  // 不启用上拉或下拉
  91.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;  // 设置GPIO速度为低速
  92.     GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;  // 设置GPIO复用功能为I2C1
  93.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);  // 初始化GPIO

  94.     /* I2C1 clock enable */
  95.     __HAL_RCC_I2C1_CLK_ENABLE();  // 启用I2C1时钟
  96.   /* USER CODE BEGIN I2C1_MspInit 1 */

  97.   /* USER CODE END I2C1_MspInit 1 */
  98.   }
  99. }

  100. /**
  101.   * @brief I2C MSP De-Initialization
  102.   *        This function freeze the hardware resources used in this example:
  103.   *          - Disable the Peripheral's clock
  104.   *          - Revert GPIO configuration to their default state
  105.   * @param i2cHandle: I2C handle pointer
  106.   * @retval None
  107.   */
  108. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  109. {

  110.   if(i2cHandle->Instance==I2C1)  // 检查是否为I2C1实例
  111.   {
  112.   /* USER CODE BEGIN I2C1_MspDeInit 0 */

  113.   /* USER CODE END I2C1_MspDeInit 0 */
  114.     /* Peripheral clock disable */
  115.     __HAL_RCC_I2C1_CLK_DISABLE();  // 禁用I2C1时钟

  116.     /**I2C1 GPIO Configuration
  117.     PB8     ------> I2C1_SCL
  118.     PB9     ------> I2C1_SDA
  119.     */
  120.     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);  // 反初始化PB8引脚

  121.     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);  // 反初始化PB9引脚

  122.   /* USER CODE BEGIN I2C1_MspDeInit 1 */

  123.   /* USER CODE END I2C1_MspDeInit 1 */
  124.   }
  125. }

  126. /* USER CODE BEGIN 1 */

  127. /* USER CODE END 1 */
主函数代码:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2025 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 "i2c.h"
  22. #include "usart.h"
  23. #include "gpio.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "oled/oled.h"  // 包含OLED显示库的头文件
  27. /* USER CODE END Includes */

  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */

  30. /* USER CODE END PTD */

  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */

  33. /* USER CODE END PD */

  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */

  36. /* USER CODE END PM */

  37. /* Private variables ---------------------------------------------------------*/

  38. /* USER CODE BEGIN PV */

  39. /* USER CODE END PV */

  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. /* USER CODE BEGIN PFP */

  43. /* USER CODE END PFP */

  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */

  46. /* USER CODE END 0 */

  47. /**
  48.   * @brief  The application entry point.
  49.   * @retval int
  50.   */
  51. int main(void)
  52. {

  53.   /* USER CODE BEGIN 1 */

  54.   /* USER CODE END 1 */

  55.   /* MCU Configuration--------------------------------------------------------*/

  56.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  57.   HAL_Init();  // 初始化HAL库

  58.   /* USER CODE BEGIN Init */

  59.   /* USER CODE END Init */

  60.   /* Configure the system clock */
  61.   SystemClock_Config();  // 配置系统时钟

  62.   /* USER CODE BEGIN SysInit */

  63.   /* USER CODE END SysInit */

  64.   /* Initialize all configured peripherals */
  65.   MX_GPIO_Init();  // 初始化GPIO
  66.   MX_USART2_UART_Init();  // 初始化USART2
  67.   MX_I2C1_Init();  // 初始化I2C1
  68.   /* USER CODE BEGIN 2 */
  69.         
  70.         OLED_Init();  // 初始化OLED显示屏
  71.         OLED_Fill(0x00);  // 清空OLED屏幕
  72.         OLED_ShowStr(0,0,"STM32C092 BOARD",2);  // 在OLED的第0行显示字符串
  73.         OLED_ShowStr(0,4,"I2C OLED TEST",2);  // 在OLED的第2行显示字符串
  74.         //HAL_Delay(500);
  75.         OLED_ShowStr(0,2,"bbs.21ic.com",2);  // 在OLED的第4行显示字符串
  76.         OLED_ShowStr(0,6,"2025-05-25",2);  // 在OLED的第6行显示字符串
  77.   /* USER CODE END 2 */

  78.   /* Infinite loop */
  79.   /* USER CODE BEGIN WHILE */
  80.   while (1)
  81.   {
  82.     /* USER CODE END WHILE */

  83.     /* USER CODE BEGIN 3 */
  84.   }
  85.   /* USER CODE END 3 */
  86. }

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

  95.   __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);  // 设置Flash延迟

  96.   /** Initializes the RCC Oscillators according to the specified parameters
  97.   * in the RCC_OscInitTypeDef structure.
  98.   */
  99.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;  // 使用外部高速时钟(HSE)
  100.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;  // 开启HSE
  101.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  102.   {
  103.     Error_Handler();  // 错误处理
  104.   }

  105.   /** Initializes the CPU, AHB and APB buses clocks
  106.   */
  107.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  108.                               |RCC_CLOCKTYPE_PCLK1;
  109.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;  // 系统时钟源为HSE
  110.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;  // 系统时钟不分频
  111.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;  // AHB总线时钟不分频
  112.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;  // APB1总线时钟不分频

  113.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  114.   {
  115.     Error_Handler();  // 错误处理
  116.   }
  117. }

  118. /* USER CODE BEGIN 4 */

  119. /* USER CODE END 4 */

  120. /**
  121.   * @brief  This function is executed in case of error occurrence.
  122.   * @retval None
  123.   */
  124. void Error_Handler(void)
  125. {
  126.   /* USER CODE BEGIN Error_Handler_Debug */
  127.   /* User can add his own implementation to report the HAL error return state */
  128.   __disable_irq();  // 禁用中断
  129.   while (1)
  130.   {
  131.   }
  132.   /* USER CODE END Error_Handler_Debug */
  133. }

  134. #ifdef  USE_FULL_ASSERT
  135. /**
  136.   * @brief  Reports the name of the source file and the source line number
  137.   *         where the assert_param error has occurred.
  138.   * @param  file: pointer to the source file name
  139.   * @param  line: assert_param error line source number
  140.   * @retval None
  141.   */
  142. void assert_failed(uint8_t *file, uint32_t line)
  143. {
  144.   /* USER CODE BEGIN 6 */
  145.   /* User can add his own implementation to report the file name and line number,
  146.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  147.   /* USER CODE END 6 */
  148. }
  149. #endif /* USE_FULL_ASSERT */
显示效果
7031468345128a6702.jpg 24236834512c6bbc9.jpg

daichaodai 发表于 2025-5-26 19:40 来自手机 | 显示全部楼层
可以用硬件IIC驱动显示屏
t60yz 发表于 2025-5-31 23:42 | 显示全部楼层
可以使用现成的 SSD1306 驱动库
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:工程师
简介:超越自我,为设计激发灵感和想象。

254

主题

825

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部
认证:工程师
简介:超越自我,为设计激发灵感和想象。

254

主题

825

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部