STM32F407之蓝牙
蓝牙通信
#include "bluetooth.h"
#include "includes.h"
volatile uint16_t USART3_Buf[128] = {0};//存储元素
volatile uint16_t USART3_Cun = 0;//统计元素个数
volatile uint8_t USART3_Sigen = 0;//标志位
staticUSART_InitTypeDef USART_InitStructure;
staticNVIC_InitTypeDef NVIC_InitStructure;
staticGPIO_InitTypeDef GPIO_InitStructure;
/*
引脚 PB10 TX 发送端
PB11 RX 接收端
USART3 总线APB1
*/
void bluetooth_Init(void)
{
/* Enable GPIO clock PB*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//Enable APB1 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
/* Connect USART pins to USART3 */
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11, GPIO_AF_USART3);
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10|GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Configure USART3
USART_InitStructure.USART_BaudRate = 9600;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位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(USART3, &USART_InitStructure);
//Configure NVIC
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
//指定USART3的中断源 接收到数据就发生中断
USART_ITConfig(USART3,USART_IT_RXNE, ENABLE);
/* Enable USART */
USART_Cmd(USART3, ENABLE);
}
//通过串口来发送数据
void USART_SandString(USART_TypeDef* USARTx, char * Str)
{
const char *p = Str;//防止地址丢失
while(p && *p)
{
USART_SendData(USARTx,*p++);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE) == RESET);
}
}
//外部中断
void USART3_IRQHandler(void)
{
//进入中断
OSIntEnter();
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET )//检测中断是否发生
{
USART_ClearITPendingBit(USART3, USART_IT_RXNE);//清除中断标志
USART3_Buf[USART3_Cun++] = USART_ReceiveData(USART3);//存储数据
if(USART3_Buf [USART3_Cun-1] == '\n' || USART3_Cun >= sizeof(USART3_Buf)-1)
{
USART3_Sigen = 1;
}
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);//循环发送直到发送完毕
}
//退出中断
OSIntExit();
}
//-----------------------------------------------------
//任务块
//暂时没有任务
原文地址:https://blog.csdn.net/m0_60615238/article/details/142483976
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!