手机蓝牙项目

蓝牙项目步骤

**1.蓝牙模块AT模式基础配置

1.蓝牙连接 ch340模块接线 TX、RX、VCC、GND、EN五根引脚**

2.AT指令配置波特率、蓝牙名称、从模式、配对密码
2.蓝牙硬件连接32单片机

蓝牙模块CND接单片机GND

蓝牙模块VCC接单片机5V

蓝牙模块RXD接收端接单片机PA2

蓝牙模块TXD接收端接单片机PA3

3.初始化蓝牙连接串口的时钟,引脚和外设配置

初始化函数:

cpp 复制代码
void myusart2_init(void)
{
	GPIO_InitTypeDef GPIOInitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	GPIOInitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIOInitStruct.GPIO_Pin=GPIO_Pin_2;
	GPIOInitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA, &GPIOInitStruct);
	
	GPIOInitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIOInitStruct.GPIO_Pin=GPIO_Pin_3;
	GPIO_Init(GPIOA, &GPIOInitStruct);
	
	USART_InitStruct.USART_BaudRate=9600;
	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStruct.USART_Parity=USART_Parity_No;
	USART_InitStruct.USART_StopBits=USART_StopBits_1;
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;
	
	NVIC_InitStruct.NVIC_IRQChannel=USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_Init(&NVIC_InitStruct);
	
	USART_Init(USART2,&USART_InitStruct);
	USART_Cmd(USART2, ENABLE);
	USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);
}
4.串口接收中断服务函数实现数据的接收和发送
cpp 复制代码
void USART2_IRQHandler()
{
			char str;
			if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
			{
				
					
				str=USART_ReceiveData(USART2);
					
				printf("receive:%c  \r\n",str);
				if(str=='1')
				{
				
						GPIO_ResetBits(GPIOA,GPIO_Pin_1);
						printf("led open  \r\n");


				}
				if(str=='0')
				{
				
			    	GPIO_SetBits(GPIOA,GPIO_Pin_1);
						printf("led close  \r\n");

				}
				
			  USART_ClearITPendingBit(USART2,USART_IT_RXNE);

			}
			



}	

具体实现代码

usart.c

cpp 复制代码
#include "stm32f10x.h"
#include "usart.h"
#include "stdio.h"

void myusart_init(void)
{
	GPIO_InitTypeDef GPIOInitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	GPIOInitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIOInitStruct.GPIO_Pin=GPIO_Pin_9;
	GPIOInitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA, &GPIOInitStruct);
	
	GPIOInitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIOInitStruct.GPIO_Pin=GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIOInitStruct);
	
	USART_InitStruct.USART_BaudRate=115200;
	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStruct.USART_Parity=USART_Parity_No;
	USART_InitStruct.USART_StopBits=USART_StopBits_1;
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;
	
	NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_Init(&NVIC_InitStruct);
	
	USART_Init(USART1,&USART_InitStruct);
	USART_Cmd(USART1, ENABLE);
	USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
}
void myusart2_init(void)
{
	GPIO_InitTypeDef GPIOInitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	GPIOInitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIOInitStruct.GPIO_Pin=GPIO_Pin_2;
	GPIOInitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOA, &GPIOInitStruct);
	
	GPIOInitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIOInitStruct.GPIO_Pin=GPIO_Pin_3;
	GPIO_Init(GPIOA, &GPIOInitStruct);
	
	USART_InitStruct.USART_BaudRate=9600;
	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStruct.USART_Parity=USART_Parity_No;
	USART_InitStruct.USART_StopBits=USART_StopBits_1;
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;
	
	NVIC_InitStruct.NVIC_IRQChannel=USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_Init(&NVIC_InitStruct);
	
	USART_Init(USART2,&USART_InitStruct);
	USART_Cmd(USART2, ENABLE);
	USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);
}
void My_Usart_Send_Byte(USART_TypeDef* USARTx,uint16_t Data)
{
	USART_SendData(USARTx, Data);
	while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET);
	
	

}
void My_Usart_Send_Sting(USART_TypeDef* USARTx,char* str)
{
		uint16_t  i=0;
		do
		{
					My_Usart_Send_Byte(USARTx,*(str+i));
					i++;
	
		
		}while(*(str+i)!='\0');
		while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);


}
int fputc(int ch,FILE *p)
{
		USART_SendData(USART1,(u8)ch);
		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
		return ch;
}

void USART1_IRQHandler()
{
			char str;
			if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
			{
				
					
				str=USART_ReceiveData(USART1);
					
				printf("receive:%c  \r\n",str);
				if(str=='0')
				{
				
						GPIO_ResetBits(GPIOA,GPIO_Pin_1);
						printf("led open  \r\n");


				}
				if(str=='1')
				{
				
			    	GPIO_SetBits(GPIOA,GPIO_Pin_1);
						printf("led close  \r\n");

				}
				
			  USART_ClearITPendingBit(USART1,USART_IT_RXNE);

			}
			



}	
void USART2_IRQHandler()
{
			char str;
			if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
			{
				
					
				str=USART_ReceiveData(USART2);
					
				printf("receive:%c  \r\n",str);
				if(str=='1')
				{
				
						GPIO_ResetBits(GPIOA,GPIO_Pin_1);
						printf("led open  \r\n");


				}
				if(str=='0')
				{
				
			    	GPIO_SetBits(GPIOA,GPIO_Pin_1);
						printf("led close  \r\n");

				}
				
			  USART_ClearITPendingBit(USART2,USART_IT_RXNE);

			}
			



}	

main.c代码

cpp 复制代码
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "usart.h"
#include "stdio.h"


int  main()
{
				LED_Init();
                myusart_init();
				myusart2_init();


   while(1)
	 {
	 }
		 
   
}

手机端蓝牙输入调试

输入1时灯亮,并打印在电脑

输入0时灯灭,并打印在电脑

相关推荐
千里马学框架2 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
开开心心就好2 天前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法
开开心心就好2 天前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
终端安全笔记3 天前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
ToDesk_Daas3 天前
随时随地手搓app!手机远程 Vibe Coding 实操分享
智能手机·电脑·iphone
终端安全笔记4 天前
描述文件、企业证书、MDM 不是三个名字,是三层
android·ios·智能手机
JoyCong19985 天前
鸿蒙用户等来的是ToDesk:折叠屏适配、星闪笔、互动Tab一次补齐
人工智能·智能手机·电脑·鸿蒙·鸿蒙系统·远程工作·ipad
是店小二呀5 天前
鸿蒙PC开源移植:CodeLite原生IDE与Remote Agent适配
android·智能手机·远程桌面
池央5 天前
通勤听书不想来回切 App?用 Audiobookshelf 搭一个自己的有声书库
智能手机·cpolar
花先锋队长6 天前
华为Mate XT2铰链防尘保养指南:如何让三折叠开合长久丝滑如初?
华为·智能手机·harmonyos