当前位置:网站首页>STM32驱动HC05蓝牙串口通信模块
STM32驱动HC05蓝牙串口通信模块
2022-07-26 12:12:00 【W_oilpicture】
前言
时不可以苟遇,道不可以虚行。
今天分享一下最近学习的 HC05 蓝牙模块,通过用 手机蓝牙控制 STM32 单片机 进行 点灯、传输数据、显示波形 等基础操作。
一、介绍
HC05模块是一款高性能主从一体蓝牙串口模块,说白了,只是个蓝牙转串口的设备,你只要知道串口怎么编程使用,就可以了,实现了所谓的透明传输。
准备工作
- STM32H750VBT6
- IDE:Keil5
- STM32CubeMX
- VOFA+ 串口调试
二、手机蓝牙控制 STM32 单片机
- 手机通过蓝牙,向
STM32单片机发送消息,STM32接收到消息之后原封不动的返回给手机,也可以修改成,手机发送特定的消息,然后,STM32单片机做出相对应的动作。比如:点亮LED、发动电机、显示波形 等等。
连接说明:

数据包格式:

三、STM32 控制程序
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "math.h"
#include "stdarg.h"
#define CONSOLEBUF_SIZE 256
/* USER CODE END Includes */
/* USER CODE BEGIN PTD */
static char Uart_buf[CONSOLEBUF_SIZE];
void PrintfDebugUart(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int length = vsnprintf(Uart_buf, sizeof(Uart_buf) - 1, fmt, args);
va_end(args);
HAL_UART_Transmit(&huart1, (uint8_t *)&Uart_buf, length, 0xffff);
}
/* USER CODE END PTD */
/* USER CODE BEGIN PD */
uint8_t RxBuffer[4]; //接收数据包的变量
uint8_t TxBuffer[4]; //发送数据包的变量
uint8_t wave[100] = {
0};
/* USER CODE END PD */
/* USER CODE BEGIN 2 */
PrintfDebugUart("wang_Test.\r\n");
for(int i=0; i<100; i++)
{
wave[i] = (sin(2 * 3.1415926 * i / 100) + 1) * 100 / 2; //虚拟数据
}
HAL_UART_Receive_IT(&huart1, RxBuffer, 4); //循环使能,不断接受
/* USER CODE END 2 */
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //中断服务函数
{
if(huart == &huart1)
{
switch(RxBuffer[1])
{
case 1:
{
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_RESET);
PrintfDebugUart("LED IS OFF.\r\n");
}break;
case 2:
{
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);
for(uint16_t i=0; i<100; i++)
{
TxBuffer[0] = 0xA5;
TxBuffer[1] = wave[i];
TxBuffer[2] = TxBuffer[1];
TxBuffer[3] = 0x5A;
HAL_UART_Transmit(&huart1,TxBuffer,4,0xffff);
}
PrintfDebugUart("LED IS OPEN.\r\n");
}break;
case 3:
{
PrintfDebugUart("Over.\r\n");
}
default:
{
;
}
}
HAL_UART_Receive_IT(&huart1, RxBuffer, 4); //循环使能,才能不断接收
}
}
/* USER CODE END 4 */
实验结果
在手机上显示波形,且可以通过按键控制灯的亮灭:

边栏推荐
- Flink 在 讯飞 AI 营销业务的实时数据分析实践
- 【活动早知道】LiveVideoStack近期活动一览
- Here blog: running a large language model in a production environment - overview of the reasoning framework
- Flink's real-time data analysis practice in iFLYTEK AI marketing business
- Li Kai: the interesting and cutting-edge audio and video industry has always attracted me
- SSJ-21B时间继电器
- el-form 每行显示两列,底部按钮居中
- 什么是物联网?常见IoT协议最全讲解
- Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
- Redis主从复制原理
猜你喜欢

Pytoch deep learning quick start tutorial -- mound tutorial notes (I)

Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)

专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我

什么是Per-Title编码?

空洞卷积详解(输入输出大小分析)
![[early knowledge of activities] list of recent activities of livevideostack](/img/a5/06c13865b7adbd99d43c1a52f3fc4d.png)
[early knowledge of activities] list of recent activities of livevideostack

扫雷小游戏——轻松玩上瘾(C语言版)

Flink 在 讯飞 AI 营销业务的实时数据分析实践

Here blog: running a large language model in a production environment - overview of the reasoning framework

el-form 每行显示两列,底部按钮居中
随机推荐
Transactional事务传播行为?
Test cases should never be used casually, recording the thinking caused by the exception of a test case
Beauty salon management system unified management system?
[MySQL constraint]
剑指 Offer 24. 反转链表
Pycharm is really strong
The difference between JVM memory overflow and memory leak
File类的学习过程中出现的问题及解决方法
pytest接口自动化测试框架 | pytest的setup和teardown函数
Sword finger offer 25. merge two sorted linked lists
Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
Introduction to FPGA (II) - one out of two selector
大佬们,cdc oracle 怎么设置从指定scn号开始读取,或是怎么设置只读全量的归档,不去读取快
MySQL之数据查询(聚合函数)
SSJ-21B时间继电器
详解勒让德变换与共轭函数
JVM内存溢出和内存泄漏的区别
自定义浏览器默认右击菜单栏
pytest接口自动化测试框架 | 使用多个fixture
种种迹象表明,Apple将有望支持AV1