地位:
对于任何一门编程语言的学习,print函数毫无疑问是一种最好的调试手段,调试者不仅能通过它获取程序变量的运行状态而且通过对其合理使用获取程序的运行流程,更能通过关键变量的输出帮你验证推理的正确与否,朴素的讲print函数就是人与程序交流的媒介,能否合理运用print函数处理问题是学好程序的关键
在嵌入式的学习中Oled显示就充当人与程序与实验板子之间沟通的媒介,合理使用Oled调试程序将会给后续程序调试带来巨大便利,这就是为什么在点灯之后就立即学习Oled显示
OLed1线路图:
这是Oled连线原理图,主要看画圈部分即可,PA8
以及PB4
是Stm32l071kbu6
芯片控制,所以只需要调这俩引脚即可,哦还有就是上面A_+3V3
也是需要用芯片控制这个等会说
Stm32l071连线图:
可以看出本芯片控制着Oled的电源以及俩引脚
当PB5为低电平时,三极管SI2301导通,A_3V3得电,OLED工作;
反之,三极管SI2301不导通,A_3V3失电,OLED熄灭。
引脚配置:
以下是对I2C3_CS
及I2C3_SDA
的概述:
I2C 是一种串行通信协议,用于在微控制器和外部设备之间进行数据传输和通信。I2C3即I2C3外设,I2C有多个外设,这里指第三个。
SDA(串行数据线): SDA 是用于实际传输数据的线路。数据在 SDA 上进行传输,包括地址、命令和实际的数据位。当时钟线(SCL)处于特定状态(上升沿或下降沿)时,数据线(SDA)上的数据被读取或写入。SDA 线由主设备或从设备控制,用于传输数据。
SCL(串行时钟线): SCL 是用于同步数据传输的时钟信号线。SCL 确定了数据传输的时序和速率。数据线(SDA)上的数据传输在 SCL
信号的节拍下进行。每个数据位都在 SCL 上的每个脉冲中传输。SCL 由主设备产生和控制。
接通电源后Oled传输数据的过程需要SDA和SCL配合传输数据,SCL打节拍,SDA传数据
CubMX配置:
Keil5配置:
注意:
oled.c及.h以及font.h蓝桥杯官方会提供,但是需要将OLED_Write函数写好。
链接: https://pan.baidu.com/s/1cDqH-5il_ca5lCBzMpnCEg 提取码:lq14
main.c:
/* 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 "gpio.h"
#include "Function.h"
#include "oled.h"
#include "stdio.h"
#include "string.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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)
{
char *a = "sjkjfisd";
HAL_Init();
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C3_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
Function_OledEnable(50);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
OLED_ShowString(5,2,a,16);
/* 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};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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 %drn", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Function.c及.h:
#include "Function.h"
#include "oled.h"
#include "stdio.h"
#include "gpio.h"
#include "i2c.h"
void OLED_Write(unsigned char type, unsigned char data){ // 配置写函数
unsigned char Write_Data[2]; // 需要传输的数据
Write_Data[0] = type; // 写入的数据类型
Write_Data[1] = data; // 写入的数据
HAL_I2C_Master_Transmit(&hi2c3, 0x78, Write_Data, 2, 0xff); // i2c配置、传入数据位置、传入数据信息、传入数据大小、超时传输时间
}
void Function_OledEnable(unsigned char ms){ // Oled使能
HAL_GPIO_WritePin(OLED_Power_GPIO_Port, OLED_Power_Pin, GPIO_PIN_RESET); // 通电
HAL_Delay(ms); // 延时等待,等待数据传输完再显示数据
OLED_Init(); // 初始化OLed
}
#ifndef __FUNCTION__
#define __FUNCTION__
void OLED_Write(unsigned char comm, unsigned char data);
void Function_OledEnable(unsigned char ms);
#endif
运行效果:
黄色警告消除:
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net