HC-05实现电脑与stm32通信

        蓝牙在通信中代替的是串口通信时的一根线,所以在串口通信改蓝牙通信时无需更改代码,只需将蓝牙连到单片机上,注意若使用usart1,注意连接位置,之前我用的正点mini的板子,PA9、10用跳线帽和USB串口R和T连在一起,导致我把蓝牙的R和Tlian连在了USB的T和R上,蓝牙根本没和单片机连在一起,从而无法通信,正确连接方式应为蓝牙R——PA10,T——PA9

        初始化设置上电过程中长按蓝牙上的小按钮,直到红色由长亮变为隔1s亮2s,由此进入AT模式,串口助手配置即可。

        由于考虑到要下载程序,我把蓝牙改成了从单片机usart2接收发数据,程序如下:

#include "sys.h"
#include "bluetooth.h"	  
	 

#if SYSTEM_SUPPORT_UCOS
#include "includes.h"					
#endif
	  
#if 1
#pragma import(__use_no_semihosting)             
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý                 
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
   
_sys_exit(int x) 
{ 
	x = x; 
} 
//printf函数,重心
int fputc(int ch, FILE *f)
{      
	while((USART2->SR&0X40)==0); 
    USART2->DR = (u8) ch;      
	return ch;
}
#endif  

 


u8 USART_RX_BUF2[USART_REC_LEN2];    

u16 USART_RX_STA2=0;     	  


void uart2_init(u32 bound){
    //GPIO¶Ë¿ÚÉèÖÃ
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);	//usart2在APB1上
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	
	
 	USART_DeInit(USART2);  
	 //USART1_TX   PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//¸´ÓÃÍÆÍìÊä³ö
    GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA9
   
    //USART1_RX	  PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
    GPIO_Init(GPIOA, &GPIO_InitStructure);  //³õʼ»¯PA10

   //Usart1 NVIC ÅäÖÃ

     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇÀÕ¼ÓÅÏȼ¶3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//×ÓÓÅÏȼ¶3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQͨµÀʹÄÜ
	NVIC_Init(&NVIC_InitStructure);	//¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
  
   //USART ³õʼ»¯ÉèÖÃ

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_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(USART2, &USART_InitStructure); 
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    USART_Cmd(USART2, ENABLE);                  

}
#if EN_USART2_RX  
void USART2_IRQHandler(void)                	
	{
	u8 Res;
#ifdef OS_TICKS_PER_SEC	 	
	OSIntEnter();    
#endif
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  ±ØÐëÊÇ0x0d 0x0a½áβ)
		{
		Res =USART_ReceiveData(USART2);
		
		if((USART_RX_STA2&0x8000)==0)
			{
			if(USART_RX_STA2&0x4000)
				{
				if(Res!=0x0a)USART_RX_STA2=0;
				else USART_RX_STA2|=0x8000;	
				}
			else //»¹Ã»ÊÕµ½0X0D
				{	
				if(Res==0x0d)USART_RX_STA2|=0x4000;
				else
					{
					USART_RX_BUF2[USART_RX_STA2&0X3FFF]=Res ;
					USART_RX_STA2++;
					if(USART_RX_STA2>(USART_REC_LEN2-1))USART_RX_STA2=0;
					}		 
				}
			}   		 
     } 
#ifdef OS_TICKS_PER_SEC	 
	OSIntExit();  											 
#endif
} 
#endif	
#ifndef __BLUETOOTH_H
#define __BLUETOOTH_H
#include "stdio.h"	
#include "sys.h" 


#define USART_REC_LEN2 			200  	//¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART2_RX 			1		//ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ
	  	
extern u8  USART_RX_BUF2[USART_REC_LEN2]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû 
extern u16 USART_RX_STA2;         		//½ÓÊÕ״̬±ê¼Ç	
//Èç¹ûÏë´®¿ÚÖжϽÓÊÕ£¬Ç벻ҪעÊÍÒÔϺ궨Òå
void uart2_init(u32 bound);
#endif

由此,在主函数中添加“Bluetooth.h”,主函数内设置好波特率,记得线查对,就可以用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值