STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled

STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled


  • ✨由于手上只有I2C接口的SSD1306 OLED屏幕,仅测试了硬件I2C驱动显示功能,实际测试的FPS帧率在37或38变化。
  • 本项目从Github开源项目中移植过来,开源地址:https://github.com/lamik/OLED_SSD1306_STM32_HAL
  • I2C + DMA实测效果;

硬件接口说明

  • 采用的是STM32F103VC,配置的是:I2C1和SPI1
    PB8     ------> I2C1_SCL
    PB9     ------> I2C1_SDA
    PA4     ------> SPI1_NSS
    PA5     ------> SPI1_SCK
    PA7     ------> SPI1_MOSI

STM32cubemx配置参数

  • I2C接口配置参数:Fast-mode,比特速率最高400 kbit/s.
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第1张图片
  • 添加I2C DMA数据发送选项

STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第2张图片

  • SPI接口配置
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第3张图片
  • SPI DMA配置
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第4张图片
  • NVIC配置
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第5张图片
  • 相关引脚配置和定义
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第6张图片
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第7张图片
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第8张图片

⛳业务代码注意事项

  • 原工程是使用了抵达定时器中断的,在STM32cubemx中该接口函数是没有进行初始化的。需要手动添加,如果没有手动添加,OLED显示会卡在一个固定的显示画面上。
    STM32F103基于HAL库I2C/SPI硬件接口+DMA驱动 SSD1306 Oled_第9张图片
  • 调用关系:SysTick_Handler() --> HAL_SYSTICK_IRQHandler() --> HAL_SYSTICK_Callback()
  • stm32f1xx_it.c文件中找到下面的函数,并在对应位置添加相应的函数:
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
	HAL_SYSTICK_IRQHandler();			// 需手动添加,stm32cubemx不能自动生成
  /* USER CODE END SysTick_IRQn 1 */
}

I2C/SPI接口切换说明

  • 在OLED_SSD1306.h头文件中启用和注释相对应的宏,即可实现接口切换。
/*
 *
 *    SETTINGS
 *		设置ssd1306对应的接口,启用相对应的宏
 *    Please set only one interface. It won't work with both one time.
 *
 */
//#define SSD1306_SPI_CONTROL
#define SSD1306_I2C_CONTROL		

#ifdef SSD1306_I2C_CONTROL
#define SSD1306_I2C_DMA_ENABLE
#define SSD1306_I2C_ADDRESS   0x78
#endif
#ifdef SSD1306_SPI_CONTROL
#define SSD1306_RESET_USE
#define SSD1306_SPI_DMA_ENABLE
#define SPI_CS_HARDWARE_CONTROL
#endif

main主程序代码

/* 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 "dma.h"
#include "i2c.h"
#include "spi.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "OLED_SSD1306.h"
#include "GFX_BW.h"
#include "fonts/fonts.h"
#include "picture.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 */
volatile uint16_t timer_1s;
/* 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 */
void HAL_SYSTICK_Callback(void)
{
    if(timer_1s)
    {
        --timer_1s;
    }
}

void I2C_DMAError(void)
{
    while(1)
    {
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    }
}
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef* hspi)
{
#if defined(SSD1306_SPI_CONTROL) && !defined(SSD1306_SPI_DMA_ENABLE)
    SSD1306_DmaEndCallback(hspi);
#endif
}
/* 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_DMA_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
#ifdef SSD1306_I2C_CONTROL
    SSD1306_I2cInit(&hi2c1);
#endif
#ifdef SSD1306_SPI_CONTROL
    SSD1306_SpiInit(&hspi1);
#endif
//    SSD1306_Bitmap((uint8_t*)picture);
    HAL_Delay(1000);
    GFX_SetFont(font_8x5);
    GFX_SetFontSize(1);

//SSD1306_StartScrollLeftUp(0,7,SCROLL_EVERY_4_FRAMES, 1);
//SSD1306_StartScrollLeft(0,7,SCROLL_EVERY_4_FRAMES);
//SSD1306_StartBlinking(1);
//SSD1306_ZoomIn(1);
	
    uint16_t frames = 0, fps = 0;
    uint32_t loops = 0, loops_overal = 0;
    char fps_c[20];
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
        if(!timer_1s)
        {
            timer_1s = 1000;
            fps = frames;
            frames = 0;
            loops_overal = loops;
            loops = 0;
        }

#ifdef SSD1306_I2C_DMA_ENABLE
        if(hi2c1.hdmatx->State == HAL_DMA_STATE_READY)
        {
#endif
#ifdef SSD1306_SPI_DMA_ENABLE
            if(hspi1.hdmatx->State == HAL_DMA_STATE_READY)
            {
#endif
                sprintf(fps_c, "loops: %02d", (int)loops_overal);
                SSD1306_Clear(BLACK);
                GFX_DrawString(10, 10, fps_c, WHITE, BLACK);
                sprintf(fps_c, "FPS: %02d", fps);
                GFX_DrawString(10, 20, fps_c, WHITE, BLACK);
                GFX_DrawString(19, 40, "www.msalamon.pl", WHITE, BLACK);
                frames++;
                HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
                SSD1306_Display();
#if  defined(SSD1306_I2C_DMA_ENABLE) || defined(SSD1306_SPI_DMA_ENABLE)
            }
#endif
//

//	  SSD1306_Bitmap((uint8_t*)picture);
            HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
//	  HAL_Delay(500);
            loops++;
        }
  /* 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://pan.baidu.com/s/1rpXl_jEfdYW0Q5On20lMkg
提取码: syjh

你可能感兴趣的:(ssd1306,hal)