STM32F103VET6基于STM32CubeMX 配置硬件I2C驱动SH1106 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
✅新增描点划线以及绘圆函数
- 🌿
oled.h
/* OLED 绘图函数 */
void OLED_Refresh(void);
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t);
void OLED_DrawLine(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t mode);
void OLED_DrawCircle(uint8_t x,uint8_t y,uint8_t r);
- 🌿
oled.c
/**
* @brief OLED显示缓冲区(所有像素点全亮)
*/
/* SSD1306 data buffer */
static uint8_t OLED_GRAM[144][8];
void OLED_Refresh(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_Write_Cmd(0xb0+i); //设置页地址(0~7)
OLED_Write_Cmd(0x02); //设置显示位置—列低地址
OLED_Write_Cmd(0x10); //设置显示位置—列高地址
for(n=0;n<132;n++)
{
OLED_Write_Dat(OLED_GRAM[n][i]);
}
}
}
//画点
//x:0~127
//y:0~63
//t:1 填充 0,清空
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t)
{
uint8_t i,m,n;
i=y/8;
m=y%8;
n=1<<m;
if(t){OLED_GRAM[x][i]|=n;}
else
{
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
OLED_GRAM[x][i]|=n;
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
}
}
//画线
//x1,y1:起点坐标
//x2,y2:结束坐标
void OLED_DrawLine(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t mode)
{
uint16_t t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1; //计算坐标增量
delta_y=y2-y1;
uRow=x1;//画线起点坐标
uCol=y1;
if(delta_x>0)incx=1; //设置单步方向
else if (delta_x==0)incx=0;//垂直线
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if (delta_y==0)incy=0;//水平线
else {incy=-1;delta_y=-delta_x;}
if(delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴
else distance=delta_y;
for(t=0;t<distance+1;t++)
{
OLED_DrawPoint(uRow,uCol,mode);//画点
xerr+=delta_x;
yerr+=delta_y;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
//x,y:圆心坐标
//r:圆的半径
void OLED_DrawCircle(uint8_t x,uint8_t y,uint8_t r)
{
int a, b,num;
a = 0;
b = r;
while(2 * b * b >= r * r)
{
OLED_DrawPoint(x + a, y - b,1);
OLED_DrawPoint(x - a, y - b,1);
OLED_DrawPoint(x - a, y + b,1);
OLED_DrawPoint(x + a, y + b,1);
OLED_DrawPoint(x + b, y + a,1);
OLED_DrawPoint(x + b, y - a,1);
OLED_DrawPoint(x - b, y - a,1);
OLED_DrawPoint(x - b, y + a,1);
a++;
num = (a * a + b * b) - r*r;//计算画的点离圆心的距离
if(num > 0)
{
b--;
a--;
}
}
}
📚工程源码
链接: https://pan.baidu.com/e/19xcfCnQoPlb7us2SNlTf6w
提取码: 5ru4