STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动SH1106 OLED屏幕

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动SH1106 OLED屏幕相关的知识,希望对你有一定的参考价值。

STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动SH1106 OLED屏幕


  • 📌相关篇《STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动ssd1306 OLED屏幕

  • 🔖驱动1.3寸SH1106 OLED屏幕其实和驱动0.96寸ssd1306 OLED屏幕驱动代码差不多,只需要修改几个偏移地址就可以了。

  • 🌴对于STM32F103VET6单片机有2个硬件I2C接口,分别是I2C1:PB6、PB7,I2C2:PB10、PB11

  • 🔰根据STM32F103VET6已有的外设资源,已经配置2个I2C接口。可以根据需求任意选择。在切换硬件I2C接口时,注意修改对应的oled相关驱动函数。

📑使用硬件I2C关键驱动函数

  • 📋主要是下面2个OLED 读写函数。
/**
 * @brief	OLED写入命令
 * @param cmd - 待写入命令
 * @note	移植时,请使用自己的底层API实现 
*/
static void OLED_Write_Cmd(uint8_t cmd)

	uint8_t buf[2];
	buf[0] = 0x00;	//control byte
	buf[1] = cmd;
	
	//使用HAL库的API实现
	HAL_I2C_Master_Transmit(&hi2c2, 0x78, buf, 2, 0xFFFF);//切换硬件I2C1:&hi2c1
//	HAL_I2C_Mem_Write(&hi2c2, 0x78, uint16_t MemAddress, 7, buf, 2, 0xFFFF);

/**
 * @brief	OLED写入数据
 * @param cmd - 待写入数据
 * @note	移植时,请使用自己的底层API实现 
*/
static void OLED_Write_Dat(uint8_t dat)

	uint8_t buf[2];
	buf[0] = 0x40; //control byte
	buf[1] = dat;
	
	//使用HAL库的API实现
	HAL_I2C_Master_Transmit(&hi2c2, 0x78, buf, 2, 0xFFFF);//切换硬件I2C1:&hi2c1


⛳移植说明

  • 🌴如果移植到其他型号单片机上,只需将下面的文件夹拷贝到自己项目下面,然后自己的工程当作添加源文件,以及将头文件路径设置进去。

  • 🛠取模软件:PCtoLCD2002

  • 🔧汉字取模方式:

📝主程序代码

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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 <stdio.h>
#include "oled.h"
#include "bmp.h"
/* 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();

    /* 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();
  MX_I2C1_Init();
//  MX_USART1_UART_Init();//串口1初始化
    MX_I2C2_Init();
    /* USER CODE BEGIN 2 */
    OLED_Init();
// printf("OLED 0.96' TEST...\\r\\n");
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        OLED_Clear();
        OLED_ShowChar(0, 0, 'A', 16);
        OLED_ShowChar(0, 2, 'B', 16);
        OLED_ShowChar(0, 4, 'C', 16);
        OLED_ShowChar(0, 6, 'D', 16);

        OLED_ShowChar(15, 0, 'A', 12);
        OLED_ShowChar(15, 1, 'B', 12);
        OLED_ShowChar(15, 2, 'C', 12);
        OLED_ShowChar(15, 3, 'D', 12);
        OLED_ShowChar(15, 4, 'E', 12);
        OLED_ShowChar(15, 5, 'F', 12);
        OLED_ShowChar(15, 6, 'G', 12);
        OLED_ShowChar(15, 7, 'H', 12);

        OLED_ShowString(30, 0, "Perseverance", 12);//x,y,字符串,字体大小

        OLED_ShowCHinese(35, 2, 0);//汉字显示
        OLED_ShowCHinese(65, 2, 1);
        OLED_ShowCHinese(95, 2, 2);

        OLED_ShowString(36, 6, "Hello World", 16);

        HAL_Delay(3000);
        OLED_DrawBMP(0, 0, 128, 8, BMP1);

        HAL_Delay(3000);
    
    /* USER CODE END 3 */


/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)

    RCC_OscInitTypeDef RCC_OscInitStruct = 0;
    RCC_ClkInitTypeDef RCC_ClkInitStruct = 0;

    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
    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_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != 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 */

  • 📍相关参考文章:https://cloud.tencent.com/developer/article/1662939

📚工程源码

链接: https://pan.baidu.com/s/1ONRm1E8nk0En9g0t_voQGQ
提取码: 849b

以上是关于STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动SH1106 OLED屏幕的主要内容,如果未能解决你的问题,请参考以下文章

STM32F103VET6基于STM32CubeMX创建EXTI外部中断工程

STM32F103VET6基于STM32CubeMX RTC时钟秒更新中断使用示例

STM32F103VET6基于STM32CubeMX 配置非DMA方式获取内部温度

STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动ssd1306 OLED屏幕

STM32F103VET6基于STM32CubeMX RTC时钟使用示例

STM32F103VET6基于STM32CubeMX 配置DMA方式获取内部温度