- ✨由于手上只有I2C接口的SSD1306 OLED屏幕,仅测试了硬件I2C驱动显示功能,实际测试的FPS帧率在37或38变化。
https://github.com/lamik/OLED_SSD1306_STM32_HAL
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
PA4 ------> SPI1_NSS
PA5 ------> SPI1_SCK
PA7 ------> SPI1_MOSI
- 调用关系:
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 */
}
/*
*
* 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
/* 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