//在使用本程序前,先将模块与手机端匹配成功,波特率38400
//串口1(A9、A10)接电脑,串口2(A2、A3)接蓝牙模块
//所有的波特率都为38400,蓝牙的供电为3.3-5v
//程序功能,转接蓝牙模块的指令到PC
#include "stm32f10x.h" //在该头文件中默认定义系统时钟为72M
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "bluetooth.h"
u8 flag,data;
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 dat;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //若接收数据寄存器满
{
dat = (u8)(USART_ReceiveData(USART1)&0xff); //第2个字节才是接受到的数据
data=dat;
flag=1;
USART2_SendData(dat);
}
}
void USART2_IRQHandler(void) //串口1中断服务程序
{
u8 dat;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //若接收数据寄存器满
{
dat = (u8)(USART_ReceiveData(USART2)&0xff); //第2个字节才是接受到的数据
data=dat;
flag=2;
USART1_SendData(dat);
}
}
int main(void)
{
RCC_Configuration();
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart1_init(38400); //串口初始化为9600
uart2_init(38400);
while(1)
{
// switch (flag)
// {
// case 1: USART2_SendData(data); flag=0; break;
// case 2: USART1_SendData(data); flag=0; break;
// default: flag=0; break;
// }
//USART1_SendData('A');
delay_ms(300);
}
}
#include "stm32f10x.h"
#include "bluetooth.h"
#include "usart.h"
void uart2_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_DeInit(USART2); //复位串口1
//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
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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); //初始化串口
//#if EN_USART1_RX //如果使能了接收
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
//#endif
USART_Cmd(USART2, ENABLE); //使能串口
}
void USART2_SendData(unsigned char SendData)
{
USART_SendData(USART2, SendData);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); //Tansmit Data Register empty interrupt
}