基于HAL库ecb02蓝牙通过串口2透传,串口2接收蓝牙端发送字符串又通过串口1打印的程序
时间: 2023-06-13 18:07:05 浏览: 111
以下是基于HAL库的ecb02蓝牙通过串口2透传,串口2接收蓝牙端发送字符串并通过串口1打印的程序代码:
```c
#include "stm32f1xx_hal.h"
#include <string.h>
/* UART handle structure */
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* Buffer used for UART1 reception */
uint8_t uart1_rx_buffer[32];
/* Buffer used for UART2 reception */
uint8_t uart2_rx_buffer[32];
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
int main(void)
{
HAL_Init(); // Initialize the HAL library
SystemClock_Config(); // Configure the system clock
MX_GPIO_Init(); // Initialize GPIO
MX_USART1_UART_Init(); // Initialize UART1
MX_USART2_UART_Init(); // Initialize UART2
while (1)
{
HAL_UART_Receive_IT(&huart2, uart2_rx_buffer, sizeof(uart2_rx_buffer)); // Start UART2 reception
HAL_UART_Transmit(&huart2, uart1_rx_buffer, sizeof(uart1_rx_buffer), HAL_MAX_DELAY); // Transmit UART1 data through UART2
HAL_Delay(100); // Delay for stability
}
}
/* UART1 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();
}
}
/* UART2 initialization function */
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO initialization function */
void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
/* 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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* UART2 receive complete callback function */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart2)
{
HAL_UART_Receive_IT(&huart2, uart2_rx_buffer, sizeof(uart2_rx_buffer)); // Start UART2 reception again
HAL_UART_Transmit(&huart1, uart2_rx_buffer, sizeof(uart2_rx_buffer), HAL_MAX_DELAY); // Transmit UART2 data through UART1
}
}
/* Error handler function */
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
```
在以上代码中,我们定义了两个 UART 句柄:
```c
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
```
然后我们实现了串口1和串口2的初始化函数:
```c
void MX_USART1_UART_Init(void) //UART1 initialization function
{
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();
}
}
void MX_USART2_UART_Init(void) //UART2 initialization function
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
```
接着,在 `main()` 函数中,我们使用了类似轮询的方式:
```c
while (1)
{
HAL_UART_Receive_IT(&huart2, uart2_rx_buffer, sizeof(uart2_rx_buffer)); // Start UART2 reception
HAL_UART_Transmit(&huart2, uart1_rx_buffer, sizeof(uart1_rx_buffer), HAL_MAX_DELAY); // Transmit UART1 data through UART2
HAL_Delay(100); // Delay for stability
}
```
其中,我们使用了 `HAL_UART_Receive_IT()` 函数启动了串口2的接收中断,并且使用了 `HAL_UART_Transmit()` 函数将串口1接收到的数据透传到串口2,实现了串口1和串口2的数据透传。
最后,我们实现了 `HAL_UART_RxCpltCallback()` 函数,该函数是 UART2 接收完成的回调函数,用于处理串口2接收到的数据:
```c
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart2)
{
HAL_UART_Receive_IT(&huart2, uart2_rx_buffer, sizeof(uart2_rx_buffer)); // Start UART2 reception again
HAL_UART_Transmit(&huart1, uart2_rx_buffer, sizeof(uart2_rx_buffer), HAL_MAX_DELAY); // Transmit UART2 data through UART1
}
}
```
在该函数中,我们首先再次启动了串口2的接收中断,然后使用了 `HAL_UART_Transmit()` 函数将串口2接收到的数据透传到串口1,实现了串口2接收到的数据能够通过串口1打印出来的功能。
需要注意的是,以上代码仅供参考,实际应用中可能需要根据具体情况进行修改。
阅读全文