Stm32_标准库_13_串口&蓝牙模块_手机与蓝牙模块通信

代码:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
 

char News[100] = "";

uint8_t flag = 1;

void Get_Hc05News(char *a){
	   uint32_t i = 0, j = 0;
	  
	      while(j < 10000){//等待中断
			
	      while(Serial_GetRxFlag() == 1){//获取一个字节
	         //查看标志位并清除
			       
					    News[i] =  Serial_GetRxData();//传入数据
					    i ++;
				      j = 0;
					    flag = 0;
		     }
		 
		     j ++;
		    }
	 }

 

	
int main(void)
{
	OLED_Init();
  Serial_Init();//开启串口
	 
 
	 while(1){
		     Get_Hc05News(News);
		     if(flag == 0){//有数据再输出
		    
		       OLED_ShowString(1, 1, News);
					 flag = 1;
				 }
	 }
}

其中 Serial.c

代码:

#include "stm32f10x.h"                  // Device header
#include 
#include 
#include "Delay.h"

uint8_t TEMP;
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
GPIO_InitTypeDef GPIO_InitStructu;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	 
	GPIO_InitStructu.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructu.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructu.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructu);
	
	GPIO_InitStructu.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructu.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructu.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructu);
	
	
	 
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	
	
	//中断开启
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//通道
	
	 
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//中断通道
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}

void Serial_SendByte(uint8_t Byte)//传递数据至TDR
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array, uint16_t Length)//传数组
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}

void Serial_SendString(char *String)//传字符串
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

uint32_t Serial_Pow(uint32_t X, uint32_t Y)//pow函数
{
	uint32_t Result = 1;
	while (Y --)
	{
		Result *= X;
	}
	return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length)//传递无符号数字
{
	uint8_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
	}
}

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Serial_SendString(String);
}

uint8_t Serial_GetRxFlag(void)//读取标志位后自动青除
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}

void Serial_GetRxFlag_SET(void){
	   Serial_RxFlag = 1;
}
uint8_t Serial_GetRxData(void)//返回一个字节
{
	 
  TEMP = Serial_RxData;
	//Serial_RxData = '#';//消除传输数据
	return TEMP;
}

void USART1_IRQHandler(void)//中断函数
{
	
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		Serial_RxData = USART_ReceiveData(USART1);//读数据
		Serial_SendByte(Serial_RxData);
	 
		Serial_RxFlag = 1;
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}

Serial.h

代码:

#ifndef __SERIAL_H
#define __SERIAL_H
#include "stm32f10x.h"  
#include 

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array, uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number, uint8_t Length);
void Serial_Printf(char *format, ...);

uint8_t Serial_GetRxFlag(void);
uint8_t Serial_GetRxData(void);

void Serial_GetRxFlag_SET(void);

#endif

效果:

Stm32_标准库_13_串口&蓝牙模块_手机与蓝牙模块通信_第1张图片
Stm32_标准库_13_串口&蓝牙模块_手机与蓝牙模块通信_第2张图片
Stm32_标准库_13_串口&蓝牙模块_手机与蓝牙模块通信_第3张图片

你可能感兴趣的:(stm32,单片机,智能手机)