【NUCLEO-C092RC】驱动OLED显示屏OLED 1306
本帖最后由 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 */
/**
******************************************************************************
* @filei2c.c
* @briefThis file provides code for the configuration
* of the I2C instances.
******************************************************************************
* @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 "i2c.h"// 包含I2C相关的头文件
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
I2C_HandleTypeDef hi2c1;// 定义I2C1的句柄,用于管理和控制I2C1外设
/* I2C1 init function */
void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;// 指定使用的I2C实例为I2C1
hi2c1.Init.Timing = 0x0090194B;// 设置I2C的时序参数,用于配置SCL线的时间特性
hi2c1.Init.OwnAddress1 = 0;// 设置I2C设备的主地址(7位地址模式),此处为0表示不启用
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;// 设置地址模式为7位地址
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;// 禁用双地址模式
hi2c1.Init.OwnAddress2 = 0;// 设置第二个地址(如果双地址模式启用),此处为0
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;// 设置第二个地址的掩码,此处为无掩码
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;// 禁用通用呼叫模式
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;// 禁用时钟拉伸模式
if (HAL_I2C_Init(&hi2c1) != HAL_OK)// 初始化I2C1
{
Error_Handler();// 如果初始化失败,调用错误处理函数
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)// 启用模拟滤波器
{
Error_Handler();// 如果配置失败,调用错误处理函数
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)// 配置数字滤波器,此处为禁用
{
Error_Handler();// 如果配置失败,调用错误处理函数
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example:
* - Peripheral's clock enable
* - Peripheral's GPIO Configuration
* @param i2cHandle: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};// 定义GPIO初始化结构体
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};// 定义外设时钟初始化结构体
if(i2cHandle->Instance==I2C1)// 检查是否为I2C1实例
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;// 选择I2C1时钟
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;// 设置I2C1时钟源为APB1时钟
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)// 配置外设时钟
{
Error_Handler();// 如果配置失败,调用错误处理函数
}
__HAL_RCC_GPIOB_CLK_ENABLE();// 启用GPIOB时钟
/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;// 设置GPIO引脚为PB8和PB9
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;// 设置GPIO模式为复用开漏输出(I2C需要开漏输出)
GPIO_InitStruct.Pull = GPIO_NOPULL;// 不启用上拉或下拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;// 设置GPIO速度为低速
GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;// 设置GPIO复用功能为I2C1
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);// 初始化GPIO
/* I2C1 clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();// 启用I2C1时钟
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
/**
* @brief I2C MSP De-Initialization
* This function freeze the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO configuration to their default state
* @param i2cHandle: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
if(i2cHandle->Instance==I2C1)// 检查是否为I2C1实例
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
/* USER CODE END I2C1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();// 禁用I2C1时钟
/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);// 反初始化PB8引脚
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);// 反初始化PB9引脚
/* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */主函数代码:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : 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 */
/**
* @briefThe 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 */
/**
* @briefThis 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 */
}
#ifdefUSE_FULL_ASSERT
/**
* @briefReports the name of the source file and the source line number
* where the assert_param error has occurred.
* @paramfile: pointer to the source file name
* @paramline: 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 */显示效果
可以用硬件IIC驱动显示屏 可以使用现成的 SSD1306 驱动库
页:
[1]