目录
模块介绍
详细见文末资料
硬件特性
Ø 模组封装: 15.1mm*11.2mm*2mm
Ø 工作频段: 2.4GHz-2.48GHz 免许可 ISM 频段
Ø RX 灵敏度: LE GFSK 30.8% PER 1 Mbps -95dBm
GFSK 0.1% BER 1 Mbps -93dBm
π /4-DQPSK 0.01% BER 2 Mbps -90dBm
8-DPSK 0.01% BER 3 Mbps -88dBm
Ø 工作电压: 3.3V
Ø 工作温度: -20℃ ~ +70℃
模块使用流程
硬件配置
串口调试模块
先准备一个USB转TTL模块,用作串口通信,使用AT指令对蓝牙模块进行配置。
屏幕剪辑的捕获时间: 2023/12/1 15:35
接线对照
CH340G | MX-22A |
RXD | TXD |
TXD | RXD |
3V3 | 3V3 |
GND | GND |
推荐使用的串口调试软件
XCOM V2.0(软件在下载链接中)
AT指令集和使用方法在使用手册中
屏幕剪辑的捕获时间: 2023/12/1 15:43
注意:波特率默认为115200,为了和程序匹配,可以用AT指令将波特率改为9600
用MX-22A进行串口通信的代码
bsp_ble.h代码
#ifndef __BSP_BLE_H
#define __BSP_BLE_H
#include <stdint.h>
#define BLE_GPIO_RCC RCC_APB2Periph_GPIOA
#define BLE_NVIC_RCC RCC_APB1Periph_USART2
static void BLE_NVIC_Config(void);
void BLE_GPIO_Config(void);
void USART2_IRQHandler(void);
#endif /*__BSP_BLE_H*/
#ifndef __BSP_BLE_H
#define __BSP_BLE_H
#include <stdint.h>
#define BLE_GPIO_RCC RCC_APB2Periph_GPIOA
#define BLE_NVIC_RCC RCC_APB1Periph_USART2
static void BLE_NVIC_Config(void);
void BLE_GPIO_Config(void);
void USART2_IRQHandler(void);
#endif /*__BSP_BLE_H*/
bsp_ble.c部分代码
包含的头文件
#include "bsp_ble.h"
#include "stm32f10x.h"
#include "OLED.h"
#include "Delay.h"
#include "bsp_ble.h"
#include "stm32f10x.h"
#include "OLED.h"
#include "Delay.h"
-串口参数初始化
-开启中断并且初始化 NVIC(如果需要开启中断才需要这个步骤)
-使能串口
static void BLE_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; /*使用的是USART2*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
static void BLE_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; /*使用的是USART2*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
-串口时钟使能,GPIO 时钟使能
-串口复位
-GPIO 端口模式设置
void BLE_GPIO_Config(void)
{
RCC_APB2PeriphClockCmd(BLE_GPIO_RCC,ENABLE);
RCC_APB1PeriphClockCmd(BLE_NVIC_RCC,ENABLE);
/**********************配置GPIO**********************/
GPIO_InitTypeDef GPIO_InitStructure;
/*PA2 USART2_TX*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/*PA3 USART2_RX*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/************************配置USART********************/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;/*此模块初始波特率为115200*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
BLE_NVIC_Config();
}
void BLE_GPIO_Config(void)
{
RCC_APB2PeriphClockCmd(BLE_GPIO_RCC,ENABLE);
RCC_APB1PeriphClockCmd(BLE_NVIC_RCC,ENABLE);
/**********************配置GPIO**********************/
GPIO_InitTypeDef GPIO_InitStructure;
/*PA2 USART2_TX*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/*PA3 USART2_RX*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/************************配置USART********************/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;/*此模块初始波特率为115200*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
BLE_NVIC_Config();
}
状态指示灯代码,用于判断蓝牙是否接收到数据(可以忽略)
使用的是PA1,可以更改
/**********************LED测试*************************/
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOA /* GPIO端口时钟 */
#define LED1_GPIO_PORT GPIOA /* GPIO端口 */
#define LED1_GPIO_PIN GPIO_Pin_1
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitTStruct;
/*第一步,打开外设的时钟*/
RCC_APB2PeriphClockCmd(LED1_GPIO_CLK , ENABLE); //A1
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //D2
/*第二步,配置外设初始化结构体*/
GPIO_InitTStruct.GPIO_Pin = LED1_GPIO_PIN;
//GPIO_InitTStruct.GPIO_Pin = LED2_GPIO_PIN;
GPIO_InitTStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitTStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED1_GPIO_PORT,&GPIO_InitTStruct);
//GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
}
/**********************LED测试*************************/
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOA /* GPIO端口时钟 */
#define LED1_GPIO_PORT GPIOA /* GPIO端口 */
#define LED1_GPIO_PIN GPIO_Pin_1
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitTStruct;
/*第一步,打开外设的时钟*/
RCC_APB2PeriphClockCmd(LED1_GPIO_CLK , ENABLE); //A1
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //D2
/*第二步,配置外设初始化结构体*/
GPIO_InitTStruct.GPIO_Pin = LED1_GPIO_PIN;
//GPIO_InitTStruct.GPIO_Pin = LED2_GPIO_PIN;
GPIO_InitTStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitTStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED1_GPIO_PORT,&GPIO_InitTStruct);
//GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
}
蓝牙接收指令并执行程序的代码
注:如未使用OLED屏显示和LED指示灯请注释掉相关代码
void USART2_IRQHandler(void)
{
uint8_t data;
LED_GPIO_Config();
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
data= USART_ReceiveData(USART2);
USART_SendData(USART2,data);
if (data == 'W')/*接收到的指令*/
{
OLED_Clear();
Delay_us(10);
OLED_ShowString(1,1,"The command is:");
OLED_ShowNum(2,1,1314,4);
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
Delay_s(1);
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
/*以上为所执行的程序,可以按需更改内容*/
}
else
{
OLED_Clear();
Delay_us(10);
OLED_ShowString(1,1,"The command is:");
OLED_ShowString(2,1,"Error");
GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
Delay_s(1);
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
}
}
}
void USART2_IRQHandler(void)
{
uint8_t data;
LED_GPIO_Config();
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
data= USART_ReceiveData(USART2);
USART_SendData(USART2,data);
if (data == 'W')/*接收到的指令*/
{
OLED_Clear();
Delay_us(10);
OLED_ShowString(1,1,"The command is:");
OLED_ShowNum(2,1,1314,4);
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
Delay_s(1);
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
/*以上为所执行的程序,可以按需更改内容*/
}
else
{
OLED_Clear();
Delay_us(10);
OLED_ShowString(1,1,"The command is:");
OLED_ShowString(2,1,"Error");
GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
Delay_s(1);
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
}
}
}
main.c的代码
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "OLED.h"
#include "Delay.h"
#include "bsp_ble.h"
int main(void)
{
OLED_Init();
BLE_GPIO_Config();
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//USART2_IRQHandler();
while(1)
{
USART2_IRQHandler();
}
// 来到这里的时候,系统的时钟已经被配置成72M。
}
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "OLED.h"
#include "Delay.h"
#include "bsp_ble.h"
int main(void)
{
OLED_Init();
BLE_GPIO_Config();
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//USART2_IRQHandler();
while(1)
{
USART2_IRQHandler();
}
// 来到这里的时候,系统的时钟已经被配置成72M。
}
OLED的代码可以下载江协的代码使用
资料下载链接:
MX-22A使用手册
链接:https://pan.baidu.com/s/1aLLauBuu0pv8F7lBw_DJxg?pwd=xdie
提取码:xdie
通用调试上位机
链接:https://pan.baidu.com/s/139pRWUWONRF6P5L3Sr55OQ?pwd=t0p2
提取码:t0p2