一.蓝牙HC-05简介
(主要为AT模式介绍)
1.接线要点:
(六个引脚中左右两侧的两个不用连接)
TX - RX RX - TX GND - GND VCC - VCC(5V)
2.AT模式:
进入方法:
使用CH340串口工具链接HC-05及电脑USB端(提前下好CH340驱动)
按下HC-05正面黑色小按键的同时插入电脑USB端,待上电后松开按键,观察到HC-05上灯不在快速闪烁,而是隔一到两秒秒闪烁一次时即为成功进入AT模式。
使用串口工具:
如ATK_XCOM
(我将打包好放在文末的百度网盘中)
切记:设置波特率为38400(此波特率为AT模式必需的),
停止位:1 数据位:8 校验位:None(均为默认值),
随后点击打开串口
AT常用指令:
查询蓝牙名字 AT+NAME?
修改蓝牙名字 AT+NAME=你想修改成的名字
查询蓝牙密码 AT+PSWD?
修改蓝牙密码 AT+PSWD=你想修改成的密码
其余AT模式指令与本文最终目的无关,若想进一步学习,自行搜索即可
关于串口的 CubeMX的配置:
二.单片机与手机之间的蓝牙通信
1.蓝牙APP
见文末百度网盘链接中,下载在手机上即可
2.手机与单片机通信代码部分
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "OLED4.h"
#include "HC05.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 */
uint8_t Rx_buff[2]; //作为缓存区,暂时存取从串口接收中断中得到的 数据
uint8_t OLED_t=0;//显示于OLED屏幕上,显示手机发给单片机的变量
uint8_t s[]="OK"; //单片机发给手机的反馈
/* 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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
OLED_Init(); //OLED初始化
HAL_UART_Receive_IT(&huart1, Rx_buff, 1);//开启串口接收中断 写在初始化中
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
OLED_ShowNum(0, 0,OLED_t, 4, OLED_8X16); //OLED显示变量OLED_t
OLED_Update(); //更新屏幕显示
}
/* 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 */
/*中断回调函数*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t t=0;
if(huart == &huart1)
{
HAL_UART_Transmit(&huart1,s,2,HAL_MAX_DELAY); //发送接收反馈给手机
t=Rx_buff[0]; //将缓存区的量赋值给t
OLED_t=t-48; //收到的为ASCII码,显示数字需减去‘0’,即48
HAL_UART_Receive_IT(&huart1, Rx_buff, 1); //重新开启串口接收中断
}
}
/* 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 */
3.OLED屏幕显示函数
源代码自江科大0.96寸四针脚软件模拟I2C的屏幕显示,原本是标准库,我将其改写为HAL库函数
为了省去麻烦,你可以直接将连接OLED屏幕的SCL引脚,SDA引脚的单片机的两个IO口,在CubeMX中改名为SCL4,SDA4。(因为是模拟I2C,因此任意IO口均可)
分为四个部分OLED4.c OLED4.h OLED_Data4.c OLED_Data4.h
/*OLED4.c*/
#include "main.h"
#include "OLED4.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include "gpio.h"
/**
* 数据存储格式:
* 纵向8点,高位在下,先从左到右,再从上到下
* 每一个Bit对应一个像素点
*
* B0 B0 B0 B0
* B1 B1 B1 B1
* B2 B2 B2 B2
* B3 B3 -------------> B3 B3 --
* B4 B4 B4 B4 |
* B5 B5 B5 B5 |
* B6 B6 B6 B6 |
* B7 B7 B7 B7 |
* |
* -----------------------------------
* |
* | B0 B0 B0 B0
* | B1 B1 B1 B1
* | B2 B2 B2 B2
* --> B3 B3 -------------> B3 B3
* B4 B4 B4 B4
* B5 B5 B5 B5
* B6 B6 B6 B6
* B7 B7 B7 B7
*
* 坐标轴定义:
* 左上角为(0, 0)点
* 横向向右为X轴,取值范围:0~127
* 纵向向下为Y轴,取值范围:0~63
*
* 0 X轴 127
* .------------------------------->
* 0 |
* |
* |
* |
* Y轴 |
* |
* |
* |
* 63 |
* v
*
*/
/*全局变量*********************/
/**
* OLED显存数组
* 所有的显示函数,都只是对此显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
*/
uint8_t OLED_DisplayBuf[8][128];
/*********************全局变量*/
/*引脚配置*********************/
/**
* 函 数:OLED写SCL高低电平
* 参 数:要写入SCL的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写SCL时,此函数会被调用
* 用户需要根据参数传入的值,将SCL置为高电平或者低电平
* 当参数传入0时,置SCL为低电平,当参数传入1时,置SCL为高电平
*/
void OLED_W_SCL(uint8_t BitValue)
{
/*根据BitValue的值,将SCL置高电平或者低电平*/
// GPIO_WriteBit(GPIOB, GPIO_Pin_7, (BitAction)BitValue);
HAL_GPIO_WritePin(SCL4_GPIO_Port,SCL4_Pin,(GPIO_PinState)(BitValue));
/*如果单片机速度过快,可在此添加适量延时,以避免超出I2C通信的最大速度*/
//...
}
/**
* 函 数:OLED写SDA高低电平
* 参 数:要写入SDA的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写SDA时,此函数会被调用
* 用户需要根据参数传入的值,将SDA置为高电平或者低电平
* 当参数传入0时,置SDA为低电平,当参数传入1时,置SDA为高电平
*/
void OLED_W_SDA(uint8_t BitValue)
{
/*根据BitValue的值,将SDA置高电平或者低电平*/
// GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)BitValue);
HAL_GPIO_WritePin(SDA4_GPIO_Port,SDA4_Pin,(GPIO_PinState)(BitValue));
/*如果单片机速度过快,可在此添加适量延时,以避免超出I2C通信的最大速度*/
//...
}
/**
* 函 数:OLED引脚初始化
* 参 数:无
* 返 回 值:无
* 说 明:当上层函数需要初始化时,此函数会被调用
* 用户需要将SCL和SDA引脚初始化为开漏模式,并释放引脚
*/
void OLED_GPIO_Init(void)
{
uint32_t i, j;
/*在初始化前,加入适量延时,待OLED供电稳定*/
for (i = 0; i < 1000; i ++)
{
for (j = 0; j < 1000; j ++);
}
/*将SCL和SDA引脚初始化为开漏模式*/
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//
// GPIO_InitTypeDef GPIO_InitStructure;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
/*释放SCL和SDA*/
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/*********************引脚配置*/
/*通信协议*********************/
/**
* 函 数:I2C起始
* 参 数:无
* 返 回 值:无
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1); //释放SDA,确保SDA为高电平
OLED_W_SCL(1); //释放SCL,确保SCL为高电平
OLED_W_SDA(0); //在SCL高电平期间,拉低SDA,产生起始信号
OLED_W_SCL(0); //起始后把SCL也拉低,即为了占用总线,也为了方便总线时序的拼接
}
/**
* 函 数:I2C终止
* 参 数:无
* 返 回 值:无
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0); //拉低SDA,确保SDA为低电平
OLED_W_SCL(1); //释放SCL,使SCL呈现高电平
OLED_W_SDA(1); //在SCL高电平期间,释放SDA,产生终止信号
}
/**
* 函 数:I2C发送一个字节
* 参 数:Byte 要发送的一个字节数据,范围:0x00~0xFF
* 返 回 值:无
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
/*循环8次,主机依次发送数据的每一位*/
for (i = 0; i < 8; i++)
{
/*使用掩码的方式取出Byte的指定一位数据并写入到SDA线*/
/*两个!的作用是,让所有非零的值变为1*/
OLED_W_SDA(!!(Byte & (0x80 >> i)));
OLED_W_SCL(1); //释放SCL,从机在SCL高电平期间读取SDA
OLED_W_SCL(0); //拉低SCL,主机开始发送下一位数据
}
OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
OLED_W_SCL(0);
}
/**
* 函 数:OLED写命令
* 参 数:Command 要写入的命令值,范围:0x00~0xFF
* 返 回 值:无
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start(); //I2C起始
OLED_I2C_SendByte(0x78); //发送OLED的I2C从机地址
OLED_I2C_SendByte(0x00); //控制字节,给0x00,表示即将写命令
OLED_I2C_SendByte(Command); //写入指定的命令
OLED_I2C_Stop(); //I2C终止
}
/**
* 函 数:OLED写数据
* 参 数:Data 要写入数据的起始地址
* 参 数:Count 要写入数据的数量
* 返 回 值:无
*/
void OLED_WriteData(uint8_t *Data, uint8_t Count)
{
uint8_t i;
OLED_I2C_Start(); //I2C起始
OLED_I2C_SendByte(0x78); //发送OLED的I2C从机地址
OLED_I2C_SendByte(0x40); //控制字节,给0x40,表示即将写数量
/*循环Count次,进行连续的数据写入*/
for (i = 0; i < Count; i ++)
{
OLED_I2C_SendByte(Data[i]); //依次发送Data的每一个数据
}
OLED_I2C_Stop(); //I2C终止
}
/*********************通信协议*/
/*硬件配置*********************/
/**
* 函 数:OLED初始化
* 参 数:无
* 返 回 值:无
* 说 明:使用前,需要调用此初始化函数
*/
void OLED_Init(void)
{
OLED_GPIO_Init(); //先调用底层的端口初始化
/*写入一系列的命令,对OLED进行初始化配置*/
OLED_WriteCommand(0xAE); //设置显示开启/关闭,0xAE关闭,0xAF开启
OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
OLED_WriteCommand(0x80); //0x00~0xFF
OLED_WriteCommand(0xA8); //设置多路复用率
OLED_WriteCommand(0x3F); //0x0E~0x3F
OLED_WriteCommand(0xD3); //设置显示偏移
OLED_WriteCommand(0x00); //0x00~0x7F
OLED_WriteCommand(0x40); //设置显示开始行,0x40~0x7F
OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常,0xA0左右反置
OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常,0xC0上下反置
OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //设置对比度
OLED_WriteCommand(0xCF); //0x00~0xFF
OLED_WriteCommand(0xD9); //设置预充电周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
OLED_WriteCommand(0xA6); //设置正常/反色显示,0xA6正常,0xA7反色
OLED_WriteCommand(0x8D); //设置充电泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //开启显示
OLED_Clear(); //清空显存数组
OLED_Update(); //更新显示,清屏,防止初始化后未显示内容时花屏
}
/**
* 函 数:OLED设置显示光标位置
* 参 数:Page 指定光标所在的页,范围:0~7
* 参 数:X 指定光标所在的X轴坐标,范围:0~127
* 返 回 值:无
* 说 明:OLED默认的Y轴,只能8个Bit为一组写入,即1页等于8个Y轴坐标
*/
void OLED_SetCursor(uint8_t Page, uint8_t X)
{
/*如果使用此程序驱动1.3寸的OLED显示屏,则需要解除此注释*/
/*因为1.3寸的OLED驱动芯片(SH1106)有132列*/
/*屏幕的起始列接在了第2列,而不是第0列*/
/*所以需要将X加2,才能正常显示*/
// X += 2;
/*通过指令设置页地址和列地址*/
OLED_WriteCommand(0xB0 | Page); //设置页位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
}
/*********************硬件配置*/
/*工具函数*********************/
/*工具函数仅供内部部分函数使用*/
/**
* 函 数:次方函数
* 参 数:X 底数
* 参 数:Y 指数
* 返 回 值:等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1; //结果默认为1
while (Y --) //累乘Y次
{
Result *= X; //每次把X累乘到结果上
}
return Result;
}
/**
* 函 数:判断指定点是否在指定多边形内部
* 参 数:nvert 多边形的顶点数
* 参 数:vertx verty 包含多边形顶点的x和y坐标的数组
* 参 数:testx testy 测试点的X和y坐标
* 返 回 值:指定点是否在指定多边形内部,1:在内部,0:不在内部
*/
uint8_t OLED_pnpoly(uint8_t nvert, int16_t *vertx, int16_t *verty, int16_t testx, int16_t testy)
{
int16_t i, j, c = 0;
/*此算法由W. Randolph Franklin提出*/
/*参考链接:https://wrfranklin.org/Research/Short_Notes/pnpoly.html*/
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((verty[i] > testy) != (verty[j] > testy)) &&
(testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
{
c = !c;
}
}
return c;
}
/**
* 函 数:判断指定点是否在指定角度内部
* 参 数:X Y 指定点的坐标
* 参 数:StartAngle EndAngle 起始角度和终止角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 返 回 值:指定点是否在指定角度内部,1:在内部,0:不在内部
*/
uint8_t OLED_IsInAngle(int16_t X, int16_t Y, int16_t StartAngle, int16_t EndAngle)
{
int16_t PointAngle;
PointAngle = atan2(Y, X) / 3.14 * 180; //计算指定点的弧度,并转换为角度表示
if (StartAngle < EndAngle) //起始角度小于终止角度的情况
{
/*如果指定角度在起始终止角度之间,则判定指定点在指定角度*/
if (PointAngle >= StartAngle && PointAngle <= EndAngle)
{
return 1;
}
}
else //起始角度大于于终止角度的情况
{
/*如果指定角度大于起始角度或者小于终止角度,则判定指定点在指定角度*/
if (PointAngle >= StartAngle || PointAngle <= EndAngle)
{
return 1;
}
}
return 0; //不满足以上条件,则判断判定指定点不在指定角度
}
/*********************工具函数*/
/*功能函数*********************/
/**
* 函 数:将OLED显存数组更新到OLED屏幕
* 参 数:无
* 返 回 值:无
* 说 明:所有的显示函数,都只是对OLED显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
* 故调用显示函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Update(void)
{
uint8_t j;
/*遍历每一页*/
for (j = 0; j < 8; j ++)
{
/*设置光标位置为每一页的第一列*/
OLED_SetCursor(j, 0);
/*连续写入128个数据,将显存数组的数据写入到OLED硬件*/
OLED_WriteData(OLED_DisplayBuf[j], 128);
}
}
/**
* 函 数:将OLED显存数组部分更新到OLED屏幕
* 参 数:X 指定区域左上角的横坐标,范围:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:此函数会至少更新参数指定的区域
* 如果更新区域Y轴只包含部分页,则同一页的剩余部分会跟随一起更新
* 说 明:所有的显示函数,都只是对OLED显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
* 故调用显示函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_UpdateArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height)
{
uint8_t j;
/*参数检查,保证指定区域不会超出屏幕范围*/
if (X > 127) {return;}
if (Y > 63) {return;}
if (X + Width > 128) {Width = 128 - X;}
if (Y + Height > 64) {Height = 64 - Y;}
/*遍历指定区域涉及的相关页*/
/*(Y + Height - 1) / 8 + 1的目的是(Y + Height) / 8并向上取整*/
for (j = Y / 8; j < (Y + Height - 1) / 8 + 1; j ++)
{
/*设置光标位置为相关页的指定列*/
OLED_SetCursor(j, X);
/*连续写入Width个数据,将显存数组的数据写入到OLED硬件*/
OLED_WriteData(&OLED_DisplayBuf[j][X], Width);
}
}
/**
* 函 数:将OLED显存数组全部清零
* 参 数:无
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j ++) //遍历8页
{
for (i = 0; i < 128; i ++) //遍历128列
{
OLED_DisplayBuf[j][i] = 0x00; //将显存数组数据全部清零
}
}
}
/**
* 函 数:将OLED显存数组部分清零
* 参 数:X 指定区域左上角的横坐标,范围:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ClearArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height)
{
uint8_t i, j;
/*参数检查,保证指定区域不会超出屏幕范围*/
if (X > 127) {return;}
if (Y > 63) {return;}
if (X + Width > 128) {Width = 128 - X;}
if (Y + Height > 64) {Height = 64 - Y;}
for (j = Y; j < Y + Height; j ++) //遍历指定页
{
for (i = X; i < X + Width; i ++) //遍历指定列
{
OLED_DisplayBuf[j / 8][i] &= ~(0x01 << (j % 8)); //将显存数组指定数据清零
}
}
}
/**
* 函 数:将OLED显存数组全部取反
* 参 数:无
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Reverse(void)
{
uint8_t i, j;
for (j = 0; j < 8; j ++) //遍历8页
{
for (i = 0; i < 128; i ++) //遍历128列
{
OLED_DisplayBuf[j][i] ^= 0xFF; //将显存数组数据全部取反
}
}
}
/**
* 函 数:将OLED显存数组部分取反
* 参 数:X 指定区域左上角的横坐标,范围:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ReverseArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height)
{
uint8_t i, j;
/*参数检查,保证指定区域不会超出屏幕范围*/
if (X > 127) {return;}
if (Y > 63) {return;}
if (X + Width > 128) {Width = 128 - X;}
if (Y + Height > 64) {Height = 64 - Y;}
for (j = Y; j < Y + Height; j ++) //遍历指定页
{
for (i = X; i < X + Width; i ++) //遍历指定列
{
OLED_DisplayBuf[j / 8][i] ^= 0x01 << (j % 8); //将显存数组指定数据取反
}
}
}
/**
* 函 数:OLED显示一个字符
* 参 数:X 指定字符左上角的横坐标,范围:0~127
* 参 数:Y 指定字符左上角的纵坐标,范围:0~63
* 参 数:Char 指定要显示的字符,范围:ASCII码可见字符
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowChar(uint8_t X, uint8_t Y, char Char, uint8_t FontSize)
{
if (FontSize == OLED_8X16) //字体为宽8像素,高16像素
{
/*将ASCII字模库OLED_F8x16的指定数据以8*16的图像格式显示*/
OLED_ShowImage(X, Y, 8, 16, OLED_F8x16[Char - ' ']);
}
else if(FontSize == OLED_6X8) //字体为宽6像素,高8像素
{
/*将ASCII字模库OLED_F6x8的指定数据以6*8的图像格式显示*/
OLED_ShowImage(X, Y, 6, 8, OLED_F6x8[Char - ' ']);
}
}
/**
* 函 数:OLED显示字符串
* 参 数:X 指定字符串左上角的横坐标,范围:0~127
* 参 数:Y 指定字符串左上角的纵坐标,范围:0~63
* 参 数:String 指定要显示的字符串,范围:ASCII码可见字符组成的字符串
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowString(uint8_t X, uint8_t Y, char *String, uint8_t FontSize)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++) //遍历字符串的每个字符
{
/*调用OLED_ShowChar函数,依次显示每个字符*/
OLED_ShowChar(X + i * FontSize, Y, String[i], FontSize);
}
}
/**
* 函 数:OLED显示数字(十进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:0~63
* 参 数:Number 指定要显示的数字,范围:0~4294967295
* 参 数:Length 指定数字的长度,范围:0~10
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number / OLED_Pow(10, Length - i - 1) % 10 可以十进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, Number / OLED_Pow(10, Length - i - 1) % 10 + '0', FontSize);
}
}
/**
* 函 数:OLED显示有符号数字(十进制,整数)
* 参 数:X 指定数字左上角的横坐标,范围:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:0~63
* 参 数:Number 指定要显示的数字,范围:-2147483648~2147483647
* 参 数:Length 指定数字的长度,范围:0~10
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowSignedNum(uint8_t X, uint8_t Y, int32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0) //数字大于等于0
{
OLED_ShowChar(X, Y, '+', FontSize); //显示+号
Number1 = Number; //Number1直接等于Number
}
else //数字小于0
{
OLED_ShowChar(X, Y, '-', FontSize); //显示-号
Number1 = -Number; //Number1等于Number取负
}
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number1 / OLED_Pow(10, Length - i - 1) % 10 可以十进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + (i + 1) * FontSize, Y, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0', FontSize);
}
}
/**
* 函 数:OLED显示十六进制数字(十六进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:0~63
* 参 数:Number 指定要显示的数字,范围:0x00000000~0xFFFFFFFF
* 参 数:Length 指定数字的长度,范围:0~8
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowHexNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*以十六进制提取数字的每一位*/
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10) //单个数字小于10
{
/*调用OLED_ShowChar函数,显示此数字*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, SingleNumber + '0', FontSize);
}
else //单个数字大于10
{
/*调用OLED_ShowChar函数,显示此数字*/
/*+ 'A' 可将数字转换为从A开始的十六进制字符*/
OLED_ShowChar(X + i * FontSize, Y, SingleNumber - 10 + 'A', FontSize);
}
}
}
/**
* 函 数:OLED显示二进制数字(二进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:0~63
* 参 数:Number 指定要显示的数字,范围:0x00000000~0xFFFFFFFF
* 参 数:Length 指定数字的长度,范围:0~16
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowBinNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number / OLED_Pow(2, Length - i - 1) % 2 可以二进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, Number / OLED_Pow(2, Length - i - 1) % 2 + '0', FontSize);
}
}
/**
* 函 数:OLED显示浮点数字(十进制,小数)
* 参 数:X 指定数字左上角的横坐标,范围:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:0~63
* 参 数:Number 指定要显示的数字,范围:-4294967295.0~4294967295.0
* 参 数:IntLength 指定数字的整数位长度,范围:0~10
* 参 数:FraLength 指定数字的小数位长度,范围:0~9,小数进行四舍五入显示
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowFloatNum(uint8_t X, uint8_t Y, double Number, uint8_t IntLength, uint8_t FraLength, uint8_t FontSize)
{
uint32_t PowNum, IntNum, FraNum;
if (Number >= 0) //数字大于等于0
{
OLED_ShowChar(X, Y, '+', FontSize); //显示+号
}
else //数字小于0
{
OLED_ShowChar(X, Y, '-', FontSize); //显示-号
Number = -Number; //Number取负
}
/*提取整数部分和小数部分*/
IntNum = Number; //直接赋值给整型变量,提取整数
Number -= IntNum; //将Number的整数减掉,防止之后将小数乘到整数时因数过大造成错误
PowNum = OLED_Pow(10, FraLength); //根据指定小数的位数,确定乘数
FraNum = round(Number * PowNum); //将小数乘到整数,同时四舍五入,避免显示误差
IntNum += FraNum / PowNum; //若四舍五入造成了进位,则需要再加给整数
/*显示整数部分*/
OLED_ShowNum(X + FontSize, Y, IntNum, IntLength, FontSize);
/*显示小数点*/
OLED_ShowChar(X + (IntLength + 1) * FontSize, Y, '.', FontSize);
/*显示小数部分*/
OLED_ShowNum(X + (IntLength + 2) * FontSize, Y, FraNum, FraLength, FontSize);
}
/**
* 函 数:OLED显示汉字串
* 参 数:X 指定汉字串左上角的横坐标,范围:0~127
* 参 数:Y 指定汉字串左上角的纵坐标,范围:0~63
* 参 数:Chinese 指定要显示的汉字串,范围:必须全部为汉字或者全角字符,不要加入任何半角字符
* 显示的汉字需要在OLED_Data.c里的OLED_CF16x16数组定义
* 未找到指定汉字时,会显示默认图形(一个方框,内部一个问号)
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowChinese(uint8_t X, uint8_t Y, char *Chinese)
{
uint8_t pChinese = 0;
uint8_t pIndex;
uint8_t i;
char SingleChinese[OLED_CHN_CHAR_WIDTH + 1] = {0};
for (i = 0; Chinese[i] != '\0'; i ++) //遍历汉字串
{
SingleChinese[pChinese] = Chinese[i]; //提取汉字串数据到单个汉字数组
pChinese ++; //计次自增
/*当提取次数到达OLED_CHN_CHAR_WIDTH时,即代表提取到了一个完整的汉字*/
if (pChinese >= OLED_CHN_CHAR_WIDTH)
{
pChinese = 0; //计次归零
/*遍历整个汉字字模库,寻找匹配的汉字*/
/*如果找到最后一个汉字(定义为空字符串),则表示汉字未在字模库定义,停止寻找*/
for (pIndex = 0; strcmp(OLED_CF16x16[pIndex].Index, "") != 0; pIndex ++)
{
/*找到匹配的汉字*/
if (strcmp(OLED_CF16x16[pIndex].Index, SingleChinese) == 0)
{
break; //跳出循环,此时pIndex的值为指定汉字的索引
}
}
/*将汉字字模库OLED_CF16x16的指定数据以16*16的图像格式显示*/
OLED_ShowImage(X + ((i + 1) / OLED_CHN_CHAR_WIDTH - 1) * 16, Y, 16, 16, OLED_CF16x16[pIndex].Data);
}
}
}
/**
* 函 数:OLED显示图像
* 参 数:X 指定图像左上角的横坐标,范围:0~127
* 参 数:Y 指定图像左上角的纵坐标,范围:0~63
* 参 数:Width 指定图像的宽度,范围:0~128
* 参 数:Height 指定图像的高度,范围:0~64
* 参 数:Image 指定要显示的图像
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowImage(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image)
{
uint8_t i, j;
/*参数检查,保证指定图像不会超出屏幕范围*/
if (X > 127) {return;}
if (Y > 63) {return;}
/*将图像所在区域清空*/
OLED_ClearArea(X, Y, Width, Height);
/*遍历指定图像涉及的相关页*/
/*(Height - 1) / 8 + 1的目的是Height / 8并向上取整*/
for (j = 0; j < (Height - 1) / 8 + 1; j ++)
{
/*遍历指定图像涉及的相关列*/
for (i = 0; i < Width; i ++)
{
/*超出边界,则跳过显示*/
if (X + i > 127) {break;}
if (Y / 8 + j > 7) {return;}
/*显示图像在当前页的内容*/
OLED_DisplayBuf[Y / 8 + j][X + i] |= Image[j * Width + i] << (Y % 8);
/*超出边界,则跳过显示*/
/*使用continue的目的是,下一页超出边界时,上一页的后续内容还需要继续显示*/
if (Y / 8 + j + 1 > 7) {continue;}
/*显示图像在下一页的内容*/
OLED_DisplayBuf[Y / 8 + j + 1][X + i] |= Image[j * Width + i] >> (8 - Y % 8);
}
}
}
/**
* 函 数:OLED使用printf函数打印格式化字符串
* 参 数:X 指定格式化字符串左上角的横坐标,范围:0~127
* 参 数:Y 指定格式化字符串左上角的纵坐标,范围:0~63
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 参 数:format 指定要显示的格式化字符串,范围:ASCII码可见字符组成的字符串
* 参 数:... 格式化字符串参数列表
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Printf(uint8_t X, uint8_t Y, uint8_t FontSize, char *format, ...)
{
char String[30]; //定义字符数组
va_list arg; //定义可变参数列表数据类型的变量arg
va_start(arg, format); //从format开始,接收参数列表到arg变量
vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中
va_end(arg); //结束变量arg
OLED_ShowString(X, Y, String, FontSize);//OLED显示字符数组(字符串)
}
/**
* 函 数:OLED在指定位置画一个点
* 参 数:X 指定点的横坐标,范围:0~127
* 参 数:Y 指定点的纵坐标,范围:0~63
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawPoint(uint8_t X, uint8_t Y)
{
/*参数检查,保证指定位置不会超出屏幕范围*/
if (X > 127) {return;}
if (Y > 63) {return;}
/*将显存数组指定位置的一个Bit数据置1*/
OLED_DisplayBuf[Y / 8][X] |= 0x01 << (Y % 8);
}
/**
* 函 数:OLED获取指定位置点的值
* 参 数:X 指定点的横坐标,范围:0~127
* 参 数:Y 指定点的纵坐标,范围:0~63
* 返 回 值:指定位置点是否处于点亮状态,1:点亮,0:熄灭
*/
uint8_t OLED_GetPoint(uint8_t X, uint8_t Y)
{
/*参数检查,保证指定位置不会超出屏幕范围*/
if (X > 127) {return 0;}
if (Y > 63) {return 0;}
/*判断指定位置的数据*/
if (OLED_DisplayBuf[Y / 8][X] & 0x01 << (Y % 8))
{
return 1; //为1,返回1
}
return 0; //否则,返回0
}
/**
* 函 数:OLED画线
* 参 数:X0 指定一个端点的横坐标,范围:0~127
* 参 数:Y0 指定一个端点的纵坐标,范围:0~63
* 参 数:X1 指定另一个端点的横坐标,范围:0~127
* 参 数:Y1 指定另一个端点的纵坐标,范围:0~63
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawLine(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1)
{
int16_t x, y, dx, dy, d, incrE, incrNE, temp;
int16_t x0 = X0, y0 = Y0, x1 = X1, y1 = Y1;
uint8_t yflag = 0, xyflag = 0;
if (y0 == y1) //横线单独处理
{
/*0号点X坐标大于1号点X坐标,则交换两点X坐标*/
if (x0 > x1) {temp = x0; x0 = x1; x1 = temp;}
/*遍历X坐标*/
for (x = x0; x <= x1; x ++)
{
OLED_DrawPoint(x, y0); //依次画点
}
}
else if (x0 == x1) //竖线单独处理
{
/*0号点Y坐标大于1号点Y坐标,则交换两点Y坐标*/
if (y0 > y1) {temp = y0; y0 = y1; y1 = temp;}
/*遍历Y坐标*/
for (y = y0; y <= y1; y ++)
{
OLED_DrawPoint(x0, y); //依次画点
}
}
else //斜线
{
/*使用Bresenham算法画直线,可以避免耗时的浮点运算,效率更高*/
/*参考文档:https://www.cs.montana.edu/courses/spring2009/425/dslectures/Bresenham.pdf*/
/*参考教程:https://www.bilibili.com/video/BV1364y1d7Lo*/
if (x0 > x1) //0号点X坐标大于1号点X坐标
{
/*交换两点坐标*/
/*交换后不影响画线,但是画线方向由第一、二、三、四象限变为第一、四象限*/
temp = x0; x0 = x1; x1 = temp;
temp = y0; y0 = y1; y1 = temp;
}
if (y0 > y1) //0号点Y坐标大于1号点Y坐标
{
/*将Y坐标取负*/
/*取负后影响画线,但是画线方向由第一、四象限变为第一象限*/
y0 = -y0;
y1 = -y1;
/*置标志位yflag,记住当前变换,在后续实际画线时,再将坐标换回来*/
yflag = 1;
}
if (y1 - y0 > x1 - x0) //画线斜率大于1
{
/*将X坐标与Y坐标互换*/
/*互换后影响画线,但是画线方向由第一象限0~90度范围变为第一象限0~45度范围*/
temp = x0; x0 = y0; y0 = temp;
temp = x1; x1 = y1; y1 = temp;
/*置标志位xyflag,记住当前变换,在后续实际画线时,再将坐标换回来*/
xyflag = 1;
}
/*以下为Bresenham算法画直线*/
/*算法要求,画线方向必须为第一象限0~45度范围*/
dx = x1 - x0;
dy = y1 - y0;
incrE = 2 * dy;
incrNE = 2 * (dy - dx);
d = 2 * dy - dx;
x = x0;
y = y0;
/*画起始点,同时判断标志位,将坐标换回来*/
if (yflag && xyflag){OLED_DrawPoint(y, -x);}
else if (yflag) {OLED_DrawPoint(x, -y);}
else if (xyflag) {OLED_DrawPoint(y, x);}
else {OLED_DrawPoint(x, y);}
while (x < x1) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += incrE;
}
else //下一个点在当前点东北方
{
y ++;
d += incrNE;
}
/*画每一个点,同时判断标志位,将坐标换回来*/
if (yflag && xyflag){OLED_DrawPoint(y, -x);}
else if (yflag) {OLED_DrawPoint(x, -y);}
else if (xyflag) {OLED_DrawPoint(y, x);}
else {OLED_DrawPoint(x, y);}
}
}
}
/**
* 函 数:OLED矩形
* 参 数:X 指定矩形左上角的横坐标,范围:0~127
* 参 数:Y 指定矩形左上角的纵坐标,范围:0~63
* 参 数:Width 指定矩形的宽度,范围:0~128
* 参 数:Height 指定矩形的高度,范围:0~64
* 参 数:IsFilled 指定矩形是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawRectangle(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, uint8_t IsFilled)
{
uint8_t i, j;
if (!IsFilled) //指定矩形不填充
{
/*遍历上下X坐标,画矩形上下两条线*/
for (i = X; i < X + Width; i ++)
{
OLED_DrawPoint(i, Y);
OLED_DrawPoint(i, Y + Height - 1);
}
/*遍历左右Y坐标,画矩形左右两条线*/
for (i = Y; i < Y + Height; i ++)
{
OLED_DrawPoint(X, i);
OLED_DrawPoint(X + Width - 1, i);
}
}
else //指定矩形填充
{
/*遍历X坐标*/
for (i = X; i < X + Width; i ++)
{
/*遍历Y坐标*/
for (j = Y; j < Y + Height; j ++)
{
/*在指定区域画点,填充满矩形*/
OLED_DrawPoint(i, j);
}
}
}
}
/**
* 函 数:OLED三角形
* 参 数:X0 指定第一个端点的横坐标,范围:0~127
* 参 数:Y0 指定第一个端点的纵坐标,范围:0~63
* 参 数:X1 指定第二个端点的横坐标,范围:0~127
* 参 数:Y1 指定第二个端点的纵坐标,范围:0~63
* 参 数:X2 指定第三个端点的横坐标,范围:0~127
* 参 数:Y2 指定第三个端点的纵坐标,范围:0~63
* 参 数:IsFilled 指定三角形是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawTriangle(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2, uint8_t IsFilled)
{
uint8_t minx = X0, miny = Y0, maxx = X0, maxy = Y0;
uint8_t i, j;
int16_t vx[] = {X0, X1, X2};
int16_t vy[] = {Y0, Y1, Y2};
if (!IsFilled) //指定三角形不填充
{
/*调用画线函数,将三个点用直线连接*/
OLED_DrawLine(X0, Y0, X1, Y1);
OLED_DrawLine(X0, Y0, X2, Y2);
OLED_DrawLine(X1, Y1, X2, Y2);
}
else //指定三角形填充
{
/*找到三个点最小的X、Y坐标*/
if (X1 < minx) {minx = X1;}
if (X2 < minx) {minx = X2;}
if (Y1 < miny) {miny = Y1;}
if (Y2 < miny) {miny = Y2;}
/*找到三个点最大的X、Y坐标*/
if (X1 > maxx) {maxx = X1;}
if (X2 > maxx) {maxx = X2;}
if (Y1 > maxy) {maxy = Y1;}
if (Y2 > maxy) {maxy = Y2;}
/*最小最大坐标之间的矩形为可能需要填充的区域*/
/*遍历此区域中所有的点*/
/*遍历X坐标*/
for (i = minx; i <= maxx; i ++)
{
/*遍历Y坐标*/
for (j = miny; j <= maxy; j ++)
{
/*调用OLED_pnpoly,判断指定点是否在指定三角形之中*/
/*如果在,则画点,如果不在,则不做处理*/
if (OLED_pnpoly(3, vx, vy, i, j)) {OLED_DrawPoint(i, j);}
}
}
}
}
/**
* 函 数:OLED画圆
* 参 数:X 指定圆的圆心横坐标,范围:0~127
* 参 数:Y 指定圆的圆心纵坐标,范围:0~63
* 参 数:Radius 指定圆的半径,范围:0~255
* 参 数:IsFilled 指定圆是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawCircle(uint8_t X, uint8_t Y, uint8_t Radius, uint8_t IsFilled)
{
int16_t x, y, d, j;
/*使用Bresenham算法画圆,可以避免耗时的浮点运算,效率更高*/
/*参考文档:https://www.cs.montana.edu/courses/spring2009/425/dslectures/Bresenham.pdf*/
/*参考教程:https://www.bilibili.com/video/BV1VM4y1u7wJ*/
d = 1 - Radius;
x = 0;
y = Radius;
/*画每个八分之一圆弧的起始点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X + y, Y + x);
OLED_DrawPoint(X - y, Y - x);
if (IsFilled) //指定圆填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X, Y + j);
}
}
while (x < y) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += 2 * x + 1;
}
else //下一个点在当前点东南方
{
y --;
d += 2 * (x - y) + 1;
}
/*画每个八分之一圆弧的点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X + y, Y + x);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - y, Y - x);
OLED_DrawPoint(X + x, Y - y);
OLED_DrawPoint(X + y, Y - x);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X - y, Y + x);
if (IsFilled) //指定圆填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
/*遍历两侧部分*/
for (j = -x; j < x; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X - y, Y + j);
OLED_DrawPoint(X + y, Y + j);
}
}
}
}
/**
* 函 数:OLED画椭圆
* 参 数:X 指定椭圆的圆心横坐标,范围:0~127
* 参 数:Y 指定椭圆的圆心纵坐标,范围:0~63
* 参 数:A 指定椭圆的横向半轴长度,范围:0~255
* 参 数:B 指定椭圆的纵向半轴长度,范围:0~255
* 参 数:IsFilled 指定椭圆是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawEllipse(uint8_t X, uint8_t Y, uint8_t A, uint8_t B, uint8_t IsFilled)
{
int16_t x, y, j;
int16_t a = A, b = B;
float d1, d2;
/*使用Bresenham算法画椭圆,可以避免部分耗时的浮点运算,效率更高*/
/*参考链接:https://blog.csdn.net/myf_666/article/details/128167392*/
x = 0;
y = b;
d1 = b * b + a * a * (-b + 0.5);
if (IsFilled) //指定椭圆填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X, Y + j);
OLED_DrawPoint(X, Y + j);
}
}
/*画椭圆弧的起始点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
/*画椭圆中间部分*/
while (b * b * (x + 1) < a * a * (y - 0.5))
{
if (d1 <= 0) //下一个点在当前点东方
{
d1 += b * b * (2 * x + 3);
}
else //下一个点在当前点东南方
{
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
y --;
}
x ++;
if (IsFilled) //指定椭圆填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
}
/*画椭圆中间部分圆弧*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
}
/*画椭圆两侧部分*/
d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * b * b;
while (y > 0)
{
if (d2 <= 0) //下一个点在当前点东方
{
d2 += b * b * (2 * x + 2) + a * a * (-2 * y + 3);
x ++;
}
else //下一个点在当前点东南方
{
d2 += a * a * (-2 * y + 3);
}
y --;
if (IsFilled) //指定椭圆填充
{
/*遍历两侧部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
}
/*画椭圆两侧部分圆弧*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
}
}
/**
* 函 数:OLED画圆弧
* 参 数:X 指定圆弧的圆心横坐标,范围:0~127
* 参 数:Y 指定圆弧的圆心纵坐标,范围:0~63
* 参 数:Radius 指定圆弧的半径,范围:0~255
* 参 数:StartAngle 指定圆弧的起始角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 参 数:EndAngle 指定圆弧的终止角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 参 数:IsFilled 指定圆弧是否填充,填充后为扇形
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawArc(uint8_t X, uint8_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, uint8_t IsFilled)
{
int16_t x, y, d, j;
/*此函数借用Bresenham算法画圆的方法*/
d = 1 - Radius;
x = 0;
y = Radius;
/*在画圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + y);}
if (OLED_IsInAngle(-x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y - y);}
if (OLED_IsInAngle(y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + x);}
if (OLED_IsInAngle(-y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y - x);}
if (IsFilled) //指定圆弧填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(0, j, StartAngle, EndAngle)) {OLED_DrawPoint(X, Y + j);}
}
}
while (x < y) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += 2 * x + 1;
}
else //下一个点在当前点东南方
{
y --;
d += 2 * (x - y) + 1;
}
/*在画圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + y);}
if (OLED_IsInAngle(y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + x);}
if (OLED_IsInAngle(-x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y - y);}
if (OLED_IsInAngle(-y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y - x);}
if (OLED_IsInAngle(x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y - y);}
if (OLED_IsInAngle(y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y - x);}
if (OLED_IsInAngle(-x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y + y);}
if (OLED_IsInAngle(-y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y + x);}
if (IsFilled) //指定圆弧填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, j, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + j);}
if (OLED_IsInAngle(-x, j, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y + j);}
}
/*遍历两侧部分*/
for (j = -x; j < x; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(-y, j, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y + j);}
if (OLED_IsInAngle(y, j, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + j);}
}
}
}
}
/*********************功能函数*/
/*****************江协科技|版权所有****************/
/*****************jiangxiekeji.com*****************/
/*OLED4.h*/
#ifndef __OLED4_H
#define __OLED4_H
#include <stdint.h>
#include "OLED_Data4.h"
/*参数宏定义*********************/
/*FontSize参数取值*/
/*此参数值不仅用于判断,而且用于计算横向字符偏移,默认值为字体像素宽度*/
#define OLED_8X16 8
#define OLED_6X8 6
/*IsFilled参数数值*/
#define OLED_UNFILLED 0
#define OLED_FILLED 1
/*********************参数宏定义*/
/*函数声明*********************/
/*初始化函数*/
void OLED_Init(void);
/*更新函数*/
void OLED_Update(void);
void OLED_UpdateArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
/*显存控制函数*/
void OLED_Clear(void);
void OLED_ClearArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void OLED_Reverse(void);
void OLED_ReverseArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
/*显示函数*/
void OLED_ShowChar(uint8_t X, uint8_t Y, char Char, uint8_t FontSize);
void OLED_ShowString(uint8_t X, uint8_t Y, char *String, uint8_t FontSize);
void OLED_ShowNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowSignedNum(uint8_t X, uint8_t Y, int32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowHexNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowBinNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowFloatNum(uint8_t X, uint8_t Y, double Number, uint8_t IntLength, uint8_t FraLength, uint8_t FontSize);
void OLED_ShowChinese(uint8_t X, uint8_t Y, char *Chinese);
void OLED_ShowImage(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image);
void OLED_Printf(uint8_t X, uint8_t Y, uint8_t FontSize, char *format, ...);
/*绘图函数*/
void OLED_DrawPoint(uint8_t X, uint8_t Y);
uint8_t OLED_GetPoint(uint8_t X, uint8_t Y);
void OLED_DrawLine(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1);
void OLED_DrawRectangle(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, uint8_t IsFilled);
void OLED_DrawTriangle(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2, uint8_t IsFilled);
void OLED_DrawCircle(uint8_t X, uint8_t Y, uint8_t Radius, uint8_t IsFilled);
void OLED_DrawEllipse(uint8_t X, uint8_t Y, uint8_t A, uint8_t B, uint8_t IsFilled);
void OLED_DrawArc(uint8_t X, uint8_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, uint8_t IsFilled);
/*********************函数声明*/
#endif
/*****************江协科技|版权所有****************/
/*****************jiangxiekeji.com*****************/
/*OLED_Data4.c*/
#include "OLED_Data4.h"
/**
* 数据存储格式:
* 纵向8点,高位在下,先从左到右,再从上到下
* 每一个Bit对应一个像素点
*
* B0 B0 B0 B0
* B1 B1 B1 B1
* B2 B2 B2 B2
* B3 B3 -------------> B3 B3 --
* B4 B4 B4 B4 |
* B5 B5 B5 B5 |
* B6 B6 B6 B6 |
* B7 B7 B7 B7 |
* |
* -----------------------------------
* |
* | B0 B0 B0 B0
* | B1 B1 B1 B1
* | B2 B2 B2 B2
* --> B3 B3 -------------> B3 B3
* B4 B4 B4 B4
* B5 B5 B5 B5
* B6 B6 B6 B6
* B7 B7 B7 B7
*
*/
/*ASCII字模数据*********************/
/*宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,// ! 1
0x00,0x16,0x0E,0x00,0x16,0x0E,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// " 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,// # 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,// $ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,// % 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,// & 6
0x00,0x00,0x00,0x16,0x0E,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,// ( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,// ) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,// * 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,// + 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,// , 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,// - 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,// . 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,// / 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,// 0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// 1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,// 2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,// 3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,// 4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,// 5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,// 6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,// 7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,// 8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,// 9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,// : 26
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,// ; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,// < 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,// = 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,// > 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,// ? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,// @ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,// A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,// B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,// C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,// D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,// E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,// F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,// G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,// H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,// J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,// K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,// L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,// M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,// N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,// O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,// P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,// Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,// R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,// S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,// T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,// U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,// V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,// W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,// X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,// Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,// Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,// [ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,// \ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,// ] 61
0x00,0x20,0x10,0x08,0x04,0x08,0x10,0x20,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,// _ 63
0x00,0x02,0x04,0x08,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,// a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,// b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,// c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,// d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,// e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,// g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,// h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,// j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,// k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,// m 77
0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,
0x00,0x20,0x3F,0x21,0x00,0x20,0x3F,0x20,// n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,// o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,// p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,// q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,// r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,// s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,// t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,// u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,// v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,// w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,// x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,// y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,// z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,// { 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,// | 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,// } 93
0x00,0x80,0x40,0x40,0x80,0x00,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,// ~ 94
};
/*宽6像素,高8像素*/
const uint8_t OLED_F6x8[][6] =
{
0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0x2F,0x00,0x00,// ! 1
0x00,0x00,0x07,0x00,0x07,0x00,// " 2
0x00,0x14,0x7F,0x14,0x7F,0x14,// # 3
0x00,0x24,0x2A,0x7F,0x2A,0x12,// $ 4
0x00,0x23,0x13,0x08,0x64,0x62,// % 5
0x00,0x36,0x49,0x55,0x22,0x50,// & 6
0x00,0x00,0x00,0x07,0x00,0x00,// ' 7
0x00,0x00,0x1C,0x22,0x41,0x00,// ( 8
0x00,0x00,0x41,0x22,0x1C,0x00,// ) 9
0x00,0x14,0x08,0x3E,0x08,0x14,// * 10
0x00,0x08,0x08,0x3E,0x08,0x08,// + 11
0x00,0x00,0x00,0xA0,0x60,0x00,// , 12
0x00,0x08,0x08,0x08,0x08,0x08,// - 13
0x00,0x00,0x60,0x60,0x00,0x00,// . 14
0x00,0x20,0x10,0x08,0x04,0x02,// / 15
0x00,0x3E,0x51,0x49,0x45,0x3E,// 0 16
0x00,0x00,0x42,0x7F,0x40,0x00,// 1 17
0x00,0x42,0x61,0x51,0x49,0x46,// 2 18
0x00,0x21,0x41,0x45,0x4B,0x31,// 3 19
0x00,0x18,0x14,0x12,0x7F,0x10,// 4 20
0x00,0x27,0x45,0x45,0x45,0x39,// 5 21
0x00,0x3C,0x4A,0x49,0x49,0x30,// 6 22
0x00,0x01,0x71,0x09,0x05,0x03,// 7 23
0x00,0x36,0x49,0x49,0x49,0x36,// 8 24
0x00,0x06,0x49,0x49,0x29,0x1E,// 9 25
0x00,0x00,0x36,0x36,0x00,0x00,// : 26
0x00,0x00,0x56,0x36,0x00,0x00,// ; 27
0x00,0x08,0x14,0x22,0x41,0x00,// < 28
0x00,0x14,0x14,0x14,0x14,0x14,// = 29
0x00,0x00,0x41,0x22,0x14,0x08,// > 30
0x00,0x02,0x01,0x51,0x09,0x06,// ? 31
0x00,0x3E,0x49,0x55,0x59,0x2E,// @ 32
0x00,0x7C,0x12,0x11,0x12,0x7C,// A 33
0x00,0x7F,0x49,0x49,0x49,0x36,// B 34
0x00,0x3E,0x41,0x41,0x41,0x22,// C 35
0x00,0x7F,0x41,0x41,0x22,0x1C,// D 36
0x00,0x7F,0x49,0x49,0x49,0x41,// E 37
0x00,0x7F,0x09,0x09,0x09,0x01,// F 38
0x00,0x3E,0x41,0x49,0x49,0x7A,// G 39
0x00,0x7F,0x08,0x08,0x08,0x7F,// H 40
0x00,0x00,0x41,0x7F,0x41,0x00,// I 41
0x00,0x20,0x40,0x41,0x3F,0x01,// J 42
0x00,0x7F,0x08,0x14,0x22,0x41,// K 43
0x00,0x7F,0x40,0x40,0x40,0x40,// L 44
0x00,0x7F,0x02,0x0C,0x02,0x7F,// M 45
0x00,0x7F,0x04,0x08,0x10,0x7F,// N 46
0x00,0x3E,0x41,0x41,0x41,0x3E,// O 47
0x00,0x7F,0x09,0x09,0x09,0x06,// P 48
0x00,0x3E,0x41,0x51,0x21,0x5E,// Q 49
0x00,0x7F,0x09,0x19,0x29,0x46,// R 50
0x00,0x46,0x49,0x49,0x49,0x31,// S 51
0x00,0x01,0x01,0x7F,0x01,0x01,// T 52
0x00,0x3F,0x40,0x40,0x40,0x3F,// U 53
0x00,0x1F,0x20,0x40,0x20,0x1F,// V 54
0x00,0x3F,0x40,0x38,0x40,0x3F,// W 55
0x00,0x63,0x14,0x08,0x14,0x63,// X 56
0x00,0x07,0x08,0x70,0x08,0x07,// Y 57
0x00,0x61,0x51,0x49,0x45,0x43,// Z 58
0x00,0x00,0x7F,0x41,0x41,0x00,// [ 59
0x00,0x02,0x04,0x08,0x10,0x20,// \ 60
0x00,0x00,0x41,0x41,0x7F,0x00,// ] 61
0x00,0x04,0x02,0x01,0x02,0x04,// ^ 62
0x00,0x40,0x40,0x40,0x40,0x40,// _ 63
0x00,0x00,0x01,0x02,0x04,0x00,// ` 64
0x00,0x20,0x54,0x54,0x54,0x78,// a 65
0x00,0x7F,0x48,0x44,0x44,0x38,// b 66
0x00,0x38,0x44,0x44,0x44,0x20,// c 67
0x00,0x38,0x44,0x44,0x48,0x7F,// d 68
0x00,0x38,0x54,0x54,0x54,0x18,// e 69
0x00,0x08,0x7E,0x09,0x01,0x02,// f 70
0x00,0x18,0xA4,0xA4,0xA4,0x7C,// g 71
0x00,0x7F,0x08,0x04,0x04,0x78,// h 72
0x00,0x00,0x44,0x7D,0x40,0x00,// i 73
0x00,0x40,0x80,0x84,0x7D,0x00,// j 74
0x00,0x7F,0x10,0x28,0x44,0x00,// k 75
0x00,0x00,0x41,0x7F,0x40,0x00,// l 76
0x00,0x7C,0x04,0x18,0x04,0x78,// m 77
0x00,0x7C,0x08,0x04,0x04,0x78,// n 78
0x00,0x38,0x44,0x44,0x44,0x38,// o 79
0x00,0xFC,0x24,0x24,0x24,0x18,// p 80
0x00,0x18,0x24,0x24,0x18,0xFC,// q 81
0x00,0x7C,0x08,0x04,0x04,0x08,// r 82
0x00,0x48,0x54,0x54,0x54,0x20,// s 83
0x00,0x04,0x3F,0x44,0x40,0x20,// t 84
0x00,0x3C,0x40,0x40,0x20,0x7C,// u 85
0x00,0x1C,0x20,0x40,0x20,0x1C,// v 86
0x00,0x3C,0x40,0x30,0x40,0x3C,// w 87
0x00,0x44,0x28,0x10,0x28,0x44,// x 88
0x00,0x1C,0xA0,0xA0,0xA0,0x7C,// y 89
0x00,0x44,0x64,0x54,0x4C,0x44,// z 90
0x00,0x00,0x08,0x7F,0x41,0x00,// { 91
0x00,0x00,0x00,0x7F,0x00,0x00,// | 92
0x00,0x00,0x41,0x7F,0x08,0x00,// } 93
0x00,0x08,0x04,0x08,0x10,0x08,// ~ 94
};
/*********************ASCII字模数据*/
/*汉字字模数据*********************/
/*相同的汉字只需要定义一次,汉字不分先后顺序*/
/*必须全部为汉字或者全角字符,不要加入任何半角字符*/
/*宽16像素,高16像素*/
const ChineseCell_t OLED_CF16x16[] = {
",",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"。",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,
/*按照上面的格式,在这个位置加入新的汉字数据*/
//...
/*未找到指定汉字时显示的默认图形(一个方框,内部一个问号),请确保其位于数组最末尾*/
"",
0xFF,0x01,0x01,0x01,0x31,0x09,0x09,0x09,0x09,0x89,0x71,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x96,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
};
/*********************汉字字模数据*/
/*图像数据*********************/
/*测试图像(一个方框,内部一个二极管符号),宽16像素,高16像素*/
const uint8_t Diode[] = {
0xFF,0x01,0x81,0x81,0x81,0xFD,0x89,0x91,0xA1,0xC1,0xFD,0x81,0x81,0x81,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x9F,0x88,0x84,0x82,0x81,0x9F,0x80,0x80,0x80,0x80,0xFF,
};
/*按照上面的格式,在这个位置加入新的图像数据*/
//...
/*********************图像数据*/
/*****************江协科技|版权所有****************/
/*****************jiangxiekeji.com*****************/
/*OLED_Data4.h*/
#ifndef __OLED_DATA4_H
#define __OLED_DATA4_H
#include <stdint.h>
/*中文字符字节宽度*/
#define OLED_CHN_CHAR_WIDTH 3 //UTF-8编码格式给3,GB2312编码格式给2
/*字模基本单元*/
typedef struct
{
char Index[OLED_CHN_CHAR_WIDTH + 1]; //汉字索引
uint8_t Data[32]; //字模数据
} ChineseCell_t;
/*ASCII字模数据声明*/
extern const uint8_t OLED_F8x16[][16];
extern const uint8_t OLED_F6x8[][6];
/*汉字字模数据声明*/
extern const ChineseCell_t OLED_CF16x16[];
/*图像数据声明*/
extern const uint8_t Diode[];
/*按照上面的格式,在这个位置加入新的图像数据声明*/
//...
#endif
/*****************江协科技|版权所有****************/
/*****************jiangxiekeji.com*****************/
三.两个单片机之间的蓝牙通信
特别注意:使用单片机之间蓝牙通信时,要关掉已经连过蓝牙的手机设备,防止手机抢主机或从机
1.AT模式修改主从模式
查询主从状态 AT+ROLE?
修改主从状态 AT+ROLE=1设为主机,AT+ROLE=0设为从机
获取蓝牙地址码 AT+ADDR?
查询绑定从机的地址 AT+BIND?
设置绑定从机的地址 AT+BIND=从机地址(由AT+ADDR?查出来的)
额外需要注意的是 绑定从机地址时,要把地址中的冒号均改为逗号
如 98D3:51:F6852F 改成 98D3,51,F6852F
另外主机与从机密码需一致
自此两个蓝牙就完成了绑定
绑定期间蓝牙不返回消息,上电按着按键重连即可
2.两个单片机之间通信代码部分
CubeMX配置主从机时,要保证两者波特率相同,一般均为9600,其他全部一致
1.主机(用于发送测试数据)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "OLED4.h"
#include "HC05.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 */
uint8_t Rx_buff[2]; //作为缓存区,暂时存取从串口接收中断中得到的 数据
uint8_t OLED_t=0;
uint8_t s[]="OK";
/* 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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
HAL_UART_Receive_IT(&huart1, Rx_buff, 1);//开启串口接收中断 写在初始化中
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
SendNum_int(&huart1,1,1);//向从机发送数据1,此函数来自于HC05.c
OLED_ShowNum(0, 0,OLED_t, 4, OLED_8X16);
OLED_Update();
}
/* 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 */
/*中断回调函数*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t t=0;
if(huart == &huart1)
{
// HAL_UART_Transmit(&huart1,s,2,HAL_MAX_DELAY);
t=Rx_buff[0];
OLED_t=t-48;
HAL_UART_Receive_IT(&huart1, Rx_buff, 1); //重新开启串口接收中断
}
}
/* 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 */
2.从机(用于接收测试数据)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "OLED4.h"
#include "HC05.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 */
uint8_t Rx_buff[2]; //作为缓存区,暂时存取从串口接收中断中得到的 数据
uint8_t OLED_t=0;
uint8_t s[]="OK";
/* 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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
HAL_UART_Receive_IT(&huart1, Rx_buff, 1);//开启串口接收中断 写在初始化中
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
OLED_ShowNum(0, 0,OLED_t, 4, OLED_8X16);
OLED_Update();
}
/* 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 */
/*中断回调函数*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t t=0;
if(huart == &huart1)
{
// HAL_UART_Transmit(&huart1,s,2,HAL_MAX_DELAY);
t=Rx_buff[0];
OLED_t=t-48;
SendNum_int(&huart1,OLED_t,1);//从机向主机发送验证数据
HAL_UART_Receive_IT(&huart1, Rx_buff, 1); //重新开启串口接收中断
}
}
/* 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 */
3.简单的蓝牙封装函数
自己写的函数,用于实现HC05简单发送字符串,整型数,浮点数等
c文件及头文件分别为 HC05.c HC05.h
/*HC05.c*/
#include "stm32f1xx_hal.h"
#include "main.h"
#include "string.h"
/*Cube MX的配置*/
/*
Buad Rate 9600 波特率
Word Length 8
Parity 0 奇偶校验位
Stop Bits 1 停止位
*/
uint8_t N[11]="0123456789.";
/*发送字符串*/
//\r\n算长度时是两个字符
void SendString(UART_HandleTypeDef *huart, char *String)
{
HAL_UART_Transmit(huart,(uint8_t *)String,strlen(String),HAL_MAX_DELAY);
}
/**
* 函 数:次方函数(内部使用)
* 返 回 值:返回值等于X的Y次方
*/
uint32_t Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1; //设置结果初值为1
while (Y --) //执行Y次
{
Result *= X; //将X累乘到结果
}
return Result;
}
/*发送一个字节*/
void SendByte(UART_HandleTypeDef *huart, uint8_t *Byte)
{
HAL_UART_Transmit(huart,(uint8_t *) Byte,1,HAL_MAX_DELAY);
}
/*发送整形数字*/
void SendNum_int(UART_HandleTypeDef *huart,uint32_t Number, uint8_t Length)
{
uint8_t i;
uint32_t x;
for (i = 0; i < Length; i++) //根据数字长度遍历数字的每一位
{
x= Number/Pow(10,Length-i-1);
SendByte(huart,N+x%10);
}
}
/*发送浮点型数字*/
void SendNum_float(UART_HandleTypeDef *huart,float Number, uint8_t Length_a,uint8_t Length_b)
{
uint8_t i;
uint32_t N1,N2;
uint32_t x,y;
N1=Number;
N2=(Number-N1)*Pow(10,Length_b);
for (i = 0; i < Length_a; i++) //根据数字长度遍历数字的每一位
{
x= N1/Pow(10,Length_a-i-1);
SendByte(huart,N+x%10);
}
SendByte(huart,N+10);
for (i = 0; i < Length_b; i++) //根据数字长度遍历数字的每一位
{
x= N2/Pow(10,Length_b-i-1);
SendByte(huart,N+x%10);
}
}
/*HC05.h*/
#ifndef __HC05_H
#define __HC05_H
#include "stm32f1xx_hal.h"
/*发送字符串*/
void SendString(UART_HandleTypeDef *huart, char *String);
/*发送一个字节*/
void SendByte(UART_HandleTypeDef *huart, uint8_t *Byte);
/*发送整形数据*/
void SendNum_int(UART_HandleTypeDef *huart,uint32_t Number, uint8_t Length);
/*发送浮点型数据*/
void SendNum_float(UART_HandleTypeDef *huart,float Number, uint8_t Length_a,uint8_t Length_b);
/*接收中断回调函数*/
//见HC_05.c文件
#endif
4.预期现象及检验
主机发送整型数字1,从机接收并返回接收到的数据1,
最终两个单片机OLED屏幕上应该都显示0001
经检验现象正确。
四.网盘资料分享:
百度网盘链接:https://pan.baidu.com/s/1cccyvYlSSsoo_fbQDA1c7w?pwd=6s8r
提取码:6s8r
夸克网盘链接:https://pan.quark.cn/s/eba4ca7c3569
提取码:spwE