本帖最后由 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Ω外部上拉电阻。
原理图
软件配置
使用STM32CubeMX配置进行IIC配置,把IIC配置成快速模式(Fast Mode),快速模式的最大数据传输速率为400 kbps.
代码:
主函数代码:
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "i2c.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "oled/oled.h" // 包含OLED显示库的头文件
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* 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 */
- /* 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(); // 初始化HAL库
- /* 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(); // 初始化GPIO
- MX_USART2_UART_Init(); // 初始化USART2
- MX_I2C1_Init(); // 初始化I2C1
- /* USER CODE BEGIN 2 */
-
- OLED_Init(); // 初始化OLED显示屏
- OLED_Fill(0x00); // 清空OLED屏幕
- OLED_ShowStr(0,0,"STM32C092 BOARD",2); // 在OLED的第0行显示字符串
- OLED_ShowStr(0,4,"I2C OLED TEST",2); // 在OLED的第2行显示字符串
- //HAL_Delay(500);
- OLED_ShowStr(0,2,"bbs.21ic.com",2); // 在OLED的第4行显示字符串
- OLED_ShowStr(0,6,"2025-05-25",2); // 在OLED的第6行显示字符串
- /* 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};
- __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1); // 设置Flash延迟
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; // 使用外部高速时钟(HSE)
- RCC_OscInitStruct.HSEState = RCC_HSE_ON; // 开启HSE
- 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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE; // 系统时钟源为HSE
- RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; // 系统时钟不分频
- RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; // AHB总线时钟不分频
- RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; // APB1总线时钟不分频
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != 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 */
- __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 */
显示效果
|