HC-05蓝牙模块的配置

  这是我自己配置HC-05蓝牙模块的一些经验,如果有什么其他问题可以在评论区告诉我。我用的开发板是stm32f407演示为例。

首先把蓝牙插在开发板的这个卡槽上,

1 首先是LED.C和LED.h文件

LED.c

#include "stm32f4xx.h"
#include "led.h"

void LED_init()
{
	//LED0   ----》  PF9   LED2 ----> PF10
	//1、定义外设结构变量
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	//打开外设时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	
	//2、对外设结构体成员进行初始化
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;	//选择配置的引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;		    //输出模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;		    //输出类型
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	    //引脚速率
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	    //上下拉电阻
	
	//3、调用对应的外设初始化函数
	GPIO_Init(GPIOF, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOF, GPIO_Pin_9);   //默认不亮
	GPIO_SetBits(GPIOF, GPIO_Pin_10);
}

LED.h

#ifndef _LED_H
#define _LED_H

#include "sys.h"

void LED_init(void);

#endif

2第二个是buletooth.c和buletooth.h文件

buletooth.c文件

#include "stm32f4xx.h"
#include "buletooth.h"

void BULETOOTH_init(uint32_t BaudRate)
{
	//buletooth  ------ USART3
	//PB11-----USART1_RX   PB10----USART1_TX
	//定义外设结构体变量
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//使能 GPIOB 时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	//使能 USART3 串口时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	
	//配置 GPIO端口串口复用模式
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
	
	//配置GPIO引脚并初始化为 复用模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;//选择配置的引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;		    //复用模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;		    //输出类型
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	    //引脚速率
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	    //上下拉电阻
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	//配置串口参数
	USART_InitStructure.USART_BaudRate = BaudRate;				//波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
	USART_InitStructure.USART_StopBits = USART_StopBits_1;		//1位停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;			//无校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  //无硬件流控
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
	USART_Init(USART3, &USART_InitStructure);

	//配置NVIC中断
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;			//中断编号
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;			//响应优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;				//使能中断通道
	NVIC_Init(&NVIC_InitStructure);
	
	//配置中断触发方式
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  //接收数据
	
	//使能串口
	USART_Cmd(USART3, ENABLE);
}

buletooth.h

#ifndef _BULETOOTH_H
#define _BULETOOTH_H

#include "sys.h"

void BULETOOTH_init(uint32_t BaudRate);

#endif

最后是main函数

#include "stm32f4xx.h"   //必须包含
#include "led.h"
#include "buletooth.h"


uint16_t blue_data = 0;  //全局变量,接收蓝牙的数据


//程序的入口
int main()
{
	LED_init();
	BULETOOTH_init(9600);
	
	
	while(1)  //A事件
	{
		if(blue_data == 'A')
		{
			GPIO_ResetBits(GPIOF, GPIO_Pin_9);   //亮
			
		}
		else if(blue_data == 'B')
		{
			GPIO_SetBits(GPIOF, GPIO_Pin_9);    //灭
		}
		
	}
}

void USART3_IRQHandler(void)  //串口3中断服务函数
{
	/*
		判断usart3 是否接收到了数据
		由于接收数据需要等移位寄存器接收数据,然后将数据移到接收数据寄存器中去,
		这些操作需要花费时间,如果我们不等数据接收完就马上进行下一个数据的接收,
		那么就会导致后面的数据将前面的数据给覆盖掉,这样的话数据就出现了错误
	*/
	
	if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET)
	{
		//如果接收到数据,就存到data中
		blue_data =  USART_ReceiveData(USART3);
		
		USART_ClearITPendingBit(USART3, USART_IT_RXNE);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值