基于HAL库ecb02蓝牙通过串口透传,串口接收蓝牙端发送字符串的程序
时间: 2023-06-13 14:07:28 浏览: 177
以下是基于HAL库的ecb02蓝牙模块通过串口透传的程序:
```
#include "main.h"
#include "stdio.h"
#include "string.h"
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* Private function prototypes -----------------------------------------------*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
/* Private variables ---------------------------------------------------------*/
char BT_RX_Buffer[100];
int BT_RX_Index = 0;
char USART_RX_Buffer[100];
int USART_RX_Index = 0;
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* Infinite loop */
while (1)
{
if (BT_RX_Index > 0)
{
HAL_UART_Transmit(&huart1, (uint8_t *)BT_RX_Buffer, BT_RX_Index, 1000);
BT_RX_Index = 0;
}
if (USART_RX_Index > 0)
{
HAL_UART_Transmit(&huart1, (uint8_t *)USART_RX_Buffer, USART_RX_Index, 1000);
USART_RX_Index = 0;
}
}
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/** USART1 Initialization Function
*/
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* Enable the UART Data Register not empty interrupt */
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
}
/** GPIO Initialization Function
*/
void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
USART_RX_Buffer[USART_RX_Index] = (uint8_t)(USART1->DR & (uint8_t)0x00FF);
USART_RX_Index++;
if (USART_RX_Index >= 100)
{
USART_RX_Index = 0;
}
}
if (huart->Instance == USART2)
{
BT_RX_Buffer[BT_RX_Index] = (uint8_t)(USART2->DR & (uint8_t)0x00FF);
BT_RX_Index++;
if (BT_RX_Index >= 100)
{
BT_RX_Index = 0;
}
}
}
```
在程序中,我们使用了两个接收缓冲区,一个用于接收蓝牙端发送的数据,另一个用于接收串口数据。当接收到数据时,通过 HAL_UART_RxCpltCallback() 回调函数将数据存储到相应的缓冲区中。在主函数中,我们通过轮询的方式不断检查缓冲区中是否有数据,并使用 HAL_UART_Transmit() 函数将数据透传到另一个设备中。
需要注意的是,在使用 HAL_UART_Init() 函数初始化 UART 时,需要将 UART_IT_RXNE 使能,以使能 UART 的接收中断。同时,在回调函数中需要判断是哪个 UART 接收到了数据,以便将数据存储到相应的缓冲区中。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)