基于STM32的蓝牙HC05调试程序 通过手机控制STM32单片机程序

手机蓝牙助手
在这里插入图片描述
下载链接
链接:https://pan.baidu.com/s/17ecKjowgyzPd04ojvSK2Kg
提取码:idci

连接
RXD — PA2
TXD — PA3
波特率 9600

蓝牙测试程序

#include  "myusart.h"
#include "stm32f10x_usart.h"
#include  "stdio.h"
#include "led.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
 u8 res;  
_Bool USART_STATE =0;
void USART_Config(void)
{
    
    
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	   MYNVIC_Configuration();
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    USART_InitStructure.USART_BaudRate = 9600;
    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);
}


void USART_SendString(int8_t *str)
{
    
    
    uint8_t index = 0;
    do
    {
    
    
        USART_SendData(USART2,str[index]);
        while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET);
        index++;        
    }
    while(str[index] != 0);
    
}
int fputc(int ch,FILE *f)
{
    
    
	USART_SendData(USART2, (uint8_t) ch);
   while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
   {
    
    }
    return ch;

}


void 	MYNVIC_Configuration(void)
{
    
    
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	/* Enable the RTC Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

使用蓝牙接收控制程序

void RES_Read(void)
{
    
    
	if(USART_STATE)
	{
    
    
		switch(res)
		{
    
    
			case '1':
				LED_Control(LED_SS);
				break;
			case 'c':

				break;

			case 'd':


			break;

			
			 case 'e':

			 break;

			case 'f':

			break;
	
		}
	}
}


	

void USART2_IRQHandler(void)                                //??1??????
{
    
    
    u8 Res;
     if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)  
 {
    
     
	 
	 USART_ClearITPendingBit(USART2,USART_IT_RXNE);
     Res = USART_ReceiveData(USART2);
     res = Res; 	 
	 USART_STATE = 1 ;
	 RES_Read();
     printf("%c",Res); 	 
	 
 }
} 

蓝牙发送到手机端口程序

	printf("Welcome to maker space lab");

猜你喜欢

转载自blog.csdn.net/m0_46179894/article/details/109047807