BlueTooth

main.c

#include "stm32f10x.h" 
#include "./led/bsp_led.h"
#include "./systick/bsp_systick.h"
#include "./beep/bsp_beep.h"
#include "./key/bsp_key.h"
#include "./usart/bsp_usart.h"
#include "./HC_SR04/bsp_hcsr04.h"
#include "./BlueTooth/bsp_bluetooth.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

extern volatile uint8_t BlueTooth_ReceiveData;
extern volatile uint8_t   g_usart2_buf[32];
extern volatile uint32_t  g_usart2_event;
//软件延时函数
void  Soft_Delay(unsigned int time)
{
	while(time--);
}
void BlueTooth_PrintfMessage(void)
{
	printf("发送字符'0'点亮红灯,发送数值'0'熄灭红灯\n");
	printf("发送字符'1'点亮绿灯,发送数值'1'熄灭绿灯\n");
	printf("发送字符'2'点亮蓝灯,发送数值'2'熄灭蓝灯\n");
}

int main(void)
{
	char buf[32]={0};
	char *p=NULL;
	LED_GPIO_Init();
	Beep_Init();
	KEY_GPIO_Init();
	USARTX_Config();
	HC_SR04_GPIO_Init();
	USARTX2_Config();
	BlueTooth_Init();
	printf("蓝牙测试模块\r\n");
	BlueTooth_PrintfMessage();
	while(1)
	{
		if(g_usart2_event)
		{
			//判断数据有效性
			//g_usart2_buf: 'RedLed=on#'
			if(strstr((const char*)g_usart2_buf,"RedLed"))//如果遇到RedLed
			{
				//分解字符
				p = strtok((char *)g_usart2_buf,"=");//将g_usart2_buf字符串中的数据切割,并返回切割的第一组字符串数据
				printf("%s\r\n",p);//打印RedLed
				p = strtok(NULL,"=");//返回剩下的一个字符串
				printf("%s\r\n",p);//打印ON#
				sprintf(buf,"RedLed_ON OK\r\n");
				//告诉手机RedLed打开完毕
				Usart2_SendString(USARTx,buf);
				Red_Led_ON;
				Green_Led_OFF;
				Blue_Led_OFF;
			}
			//g_usart2_buf: 'GreenLed=on#'
			if(strstr((const char*)g_usart2_buf,"GreenLed"))//如果遇到RedLed
			{
				//分解字符
				p = strtok((char *)g_usart2_buf,"=");//将g_usart2_buf字符串中的数据切割,并返回切割的第一组字符串数据
				printf("%s\r\n",p);//打印GreenLed
				p = strtok(NULL,"=");//返回剩下的一个字符串
				printf("%s\r\n",p);//打印ON#
				sprintf(buf,"GreenLed OK\r\n");
				//告诉手机RedLed打开完毕
				Usart2_SendString(USARTx,buf);
				Red_Led_OFF;
				Green_Led_ON;
				Blue_Led_OFF;
			}
			//g_usart2_buf: 'BlueLed=on#'
			if(strstr((const char*)g_usart2_buf,"BlueLed"))//如果遇到RedLed
			{
				//分解字符
				p = strtok((char *)g_usart2_buf,"=");//将g_usart2_buf字符串中的数据切割,并返回切割的第一组字符串数据
				printf("%s\r\n",p);//打印GreenLed
				p = strtok(NULL,"=");//返回剩下的一个字符串
				printf("%s\r\n",p);//打印ON#
				sprintf(buf,"BlueLed OK\r\n");
				//告诉手机RedLed打开完毕
				Usart2_SendString(USARTx,buf);
				Red_Led_OFF;
				Green_Led_OFF;
				Blue_Led_ON;
			}
//			//g_usart2_buf: 'RedLed=off#'
//			if(strstr((const char*)g_usart2_buf,"RedLed"))//如果遇到RedLed
//			{
//				//分解字符
//				p = strtok((char *)g_usart2_buf,"=");//将g_usart2_buf字符串中的数据切割,并返回切割的第一组字符串数据
//				printf("%s\r\n",p);//打印RedLed
//				p = strtok(NULL,"=");//返回剩下的一个字符串
//				printf("%s\r\n",p);//打印Off#
//				sprintf(buf,"RedLed_Off OK\r\n");
//				//告诉手机RedLed打开完毕
//				Usart2_SendString(USARTx,buf);
//				Red_Led_OFF;
//			}
//			//g_usart2_buf: 'GreenLed=off#'
//			if(strstr((const char*)g_usart2_buf,"GreenLed"))//如果遇到RedLed
//			{
//				//分解字符
//				p = strtok((char *)g_usart2_buf,"=");//将g_usart2_buf字符串中的数据切割,并返回切割的第一组字符串数据
//				printf("%s\r\n",p);//打印GreenLed
//				p = strtok(NULL,"=");//返回剩下的一个字符串
//				printf("%s\r\n",p);//打印off#
//				sprintf(buf,"GreenLed OK\r\n");
//				//告诉手机RedLed打开完毕
//				Usart2_SendString(USARTx,buf);
//				Green_Led_OFF;
//			}
//			//g_usart2_buf: 'BlueLed=off#'
//			if(strstr((const char*)g_usart2_buf,"BlueLed"))//如果遇到RedLed
//			{
//				//分解字符
//				p = strtok((char *)g_usart2_buf,"=");//将g_usart2_buf字符串中的数据切割,并返回切割的第一组字符串数据
//				printf("%s\r\n",p);//打印GreenLed
//				p = strtok(NULL,"=");//返回剩下的一个字符串
//				printf("%s\r\n",p);//打印off#
//				sprintf(buf,"BlueLed OK\r\n");
//				//告诉手机RedLed打开完毕
//				Usart2_SendString(USARTx,buf);
//				Blue_Led_OFF;
//			}
		}
		
//		switch(BlueTooth_ReceiveData)
//		{
//			case '0': //发送字符串0,点亮红灯
//				Red_Led_ON;
//				Green_Led_OFF;
//				Blue_Led_OFF;
//				break;
//			case 0x00://发送16进制数值0,熄灭红灯
//				Red_Led_OFF;
//				Green_Led_OFF;
//				Blue_Led_OFF;
//				break;	
//			case '1': //发送字符串1,点亮绿灯
//				Red_Led_OFF;
//				Green_Led_ON;
//				Blue_Led_OFF;
//				break;
//			case 0x01://发送16进制数值1,熄灭绿灯
//				Red_Led_OFF;
//				Green_Led_OFF;
//				Blue_Led_OFF;
//				break;
//			case '2': //发送字符串2,点亮蓝灯
//				Red_Led_OFF;
//				Green_Led_OFF;
//				Blue_Led_ON;
//				break;
//			case 0x02://发送16进制数值0,熄灭蓝灯
//				Red_Led_OFF;
//				Green_Led_OFF;
//				Blue_Led_OFF;
//				break;	
//		}	
	}
}

BlueTooth.c

#include "./BlueTooth/bsp_bluetooth.h"
#include "./systick/bsp_systick.h"
//USART2配置
static void USARTX_GPIO_Init(void)
{
	GPIO_InitTypeDef 	GPIO_InitStructure = {0};
	//开启时钟
	USARTx_TX_RX_RCC_CLK_ENABLE(USARTx_TX_RX_RCC_CLK_PERIPH,ENABLE);	
	//Tx  复用推挽输出
	GPIO_InitStructure.GPIO_Pin = USARTx_TX_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(USARTx_TX_GPIO_PORT,&GPIO_InitStructure);
	//Rx  浮空输入
	GPIO_InitStructure.GPIO_Pin = USARTx_RX_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(USARTx_RX_GPIO_PORT,&GPIO_InitStructure);
}
static void USARSTX_NVIC_Init(void)
{
	NVIC_InitTypeDef		NVIC_InitStructure = {0};
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//配置中断分组为2
	NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}	
static void USARTX_Mode_Init(void)
{
	USART_InitTypeDef	USART_InitStructure = {0};
	//开启时钟
	USARTx_RCC_CLK_ENABLE(USARTx_RCC_CLK_PERIPH,ENABLE);	
	USART_InitStructure.USART_BaudRate = USARTx_Baudrate;//波特率:9600
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无需硬件流,如果需要硬件流,需要加上CTS、RTS位
	USART_InitStructure.USART_Mode = (USART_Mode_Tx|USART_Mode_Rx);//接收和发送模式
	USART_InitStructure.USART_Parity = USART_Parity_No;//没有奇偶检验位
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//1位停止位.
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USARTx,&USART_InitStructure);

	USARSTX_NVIC_Init();
	//使能串口,并使能接收中断
	USART_ITConfig(USARTx,USART_IT_RXNE,ENABLE);
	//使能串口
	USART_Cmd(USARTx,ENABLE);
}
void Usart2_SendByte(USART_TypeDef * pUSARTx,uint8_t ch)
{
	USART_SendData(pUSARTx,ch);
	while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TXE) == RESET);
}
 void Usart2_SendString( USART_TypeDef * pUSARTx, char *str)
{
	unsigned int k = 0;
	do
	{
		Usart2_SendByte(pUSARTx,*(str+k));
		k++;
	}while(*(str+k) != '\0');
	while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC) == RESET);
}
void USARTX2_Config(void)
{
	USARTX_GPIO_Init();
	USARTX_Mode_Init();
}
void BlueTooth_Init(void)
{
	//发送测试指令
	Usart2_SendString(USARTx,"AT\r\n");
	Delay_ms(500);
	//发送模块复位指令
	Usart2_SendString(USARTx,"AT+RESET\r\n");
	Delay_ms(500);
	//获取版本号指令
	Usart2_SendString(USARTx,"AT+VERSION\r\n");
	Delay_ms(500);	
	//获取波特率指令
	Usart2_SendString(USARTx,"AT+BAUD\r\n");
	Delay_ms(500);
	//设置蓝牙模块名字指令
	Usart2_SendString(USARTx,"AT+NAMEChenJunFeng\r\n");
	Delay_ms(500);
}


BlueTooth.h

#ifndef  __BSP_BLUETOOTH_H
#define  __BSP_BLUETOOTH_H
#include "stm32f10x.h"
#include <stdio.h>

//USART2 宏定义
#define    USARTx_RCC_CLK_ENABLE			RCC_APB1PeriphClockCmd
#define    USARTx_RCC_CLK_PERIPH			RCC_APB1Periph_USART2
#define    USARTx							USART2
#define    USARTx_Baudrate					9600
#define    USARTx_IRQn						USART2_IRQn
#define    USARTx_IRQHandler				USART2_IRQHandler

#define    USARTx_TX_RX_RCC_CLK_ENABLE		RCC_APB2PeriphClockCmd
#define    USARTx_TX_RX_RCC_CLK_PERIPH		RCC_APB2Periph_GPIOA
//USART2_TX宏定义
#define    USARTx_TX_RCC_CLK_ENABLE			RCC_APB2PeriphClockCmd
#define    USARTx_TX_RCC_CLK_PERIPH			RCC_APB2Periph_GPIOA
#define    USARTx_TX_GPIO_PORT				GPIOA
#define    USARTx_TX_GPIO_PIN				GPIO_Pin_2

//USART2_RX宏定义
#define    USARTx_RX_RCC_CLK_ENABLE			RCC_APB2PeriphClockCmd
#define    USARTx_RX_RCC_CLK_PERIPH			RCC_APB2Periph_GPIOA
#define    USARTx_RX_GPIO_PORT				GPIOA
#define    USARTx_RX_GPIO_PIN				GPIO_Pin_3

extern void USARTX2_Config(void);
extern void Usart2_SendByte(USART_TypeDef * pUSARTx,uint8_t ch);
extern  void Usart2_SendString( USART_TypeDef * pUSARTx, char *str);
extern void BlueTooth_Init(void);
extern void Usart2_SendByte(USART_TypeDef * pUSARTx,uint8_t ch);
extern void Usart2_SendString( USART_TypeDef * pUSARTx, char *str);

#endif

it.c

/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "./usart/bsp_usart.h"
#include "./BlueTooth/bsp_bluetooth.h"
volatile uint8_t BlueTooth_ReceiveData = 0;
volatile uint8_t 	g_usart2_buf[32]={0};
volatile uint32_t 	g_usart2_event=0;
/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}
void USARTX_IRQHandler(void)
{
	uint8_t Rx_temp = 0;
	if(USART_GetITStatus(USARTX,USART_IT_RXNE) == SET)
	{
		Rx_temp = USART_ReceiveData(USARTX);
		USART_SendData(USARTX,Rx_temp);
		USART_ClearITPendingBit(USARTX,USART_IT_RXNE);
	}
}
void USARTx_IRQHandler(void)
{
	static uint32_t i = 0;
	if(USART_GetITStatus(USART2,USART_IT_RXNE) == SET)
	{
		BlueTooth_ReceiveData = USART_ReceiveData(USARTx);//串口2接收到的数据
		
		g_usart2_buf[i] = BlueTooth_ReceiveData;//串口2接收到的数据装在这个变量中
		i++;//下一次接收到数据,再装入变量的下一个地址空间中	
		//判断是否有结束符‘#’,同时检测有没有超过缓冲区的大小
		if(BlueTooth_ReceiveData == '#' || i >= sizeof(g_usart2_buf))//数据以#结尾
		{
			i = 0;
			g_usart2_event = 1;
		}
		
		USART_SendData(USARTX,BlueTooth_ReceiveData);//通过串口1打印出来给上位机
		while(USART_GetFlagStatus(USARTX,USART_FLAG_TXE) == RESET);
		USART_ClearITPendingBit(USARTX,USART_IT_TXE);
		
		USART_ClearITPendingBit(USART2,USART_IT_RXNE);
	}	
}
/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值