stm32hal库写三路串口发送和接收函数代码
时间: 2023-07-29 10:11:20 浏览: 209
以下是使用STM32 HAL库编写的三路串口发送和接收函数的参考代码。假设使用的是STM32F4xx系列的芯片,串口分别为USART1、USART2和USART3。
首先需要在CubeMX中配置串口。假设需要配置的串口参数如下:波特率为9600,无校验位,单停止位,数据位长度为8位,串口1使用PA9和PA10引脚,串口2使用PA2和PA3引脚,串口3使用PB10和PB11引脚。
在main.c文件中添加以下函数:
```
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
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();
}
}
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();
}
}
void MX_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 9600;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if(uartHandle->Instance==USART2)
{
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if(uartHandle->Instance==USART3)
{
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART3 GPIO Configuration
PB10 ------> USART3_TX
PB11 ------> USART3_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
/**
* @brief Initialises three UART devices.
* @param None
* @retval None
*/
void init_uart(void)
{
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
}
```
接下来是三路串口发送和接收的函数代码:
```
/**
* @brief Sends data via UART.
* @param uart: UART device number (1, 2 or 3).
* @param data: Pointer to data buffer.
* @param len: Length of data buffer.
* @retval None
*/
void uart_send(uint8_t uart, uint8_t* data, uint16_t len)
{
switch(uart)
{
case 1:
HAL_UART_Transmit(&huart1, data, len, HAL_MAX_DELAY);
break;
case 2:
HAL_UART_Transmit(&huart2, data, len, HAL_MAX_DELAY);
break;
case 3:
HAL_UART_Transmit(&huart3, data, len, HAL_MAX_DELAY);
break;
default:
break;
}
}
/**
* @brief Receives data via UART.
* @param uart: UART device number (1, 2 or 3).
* @param data: Pointer to data buffer.
* @param len: Length of data buffer.
* @retval Number of bytes received.
*/
uint16_t uart_receive(uint8_t uart, uint8_t* data, uint16_t len)
{
uint16_t rx_len = 0;
switch(uart)
{
case 1:
rx_len = HAL_UART_Receive(&huart1, data, len, HAL_MAX_DELAY);
break;
case 2:
rx_len = HAL_UART_Receive(&huart2, data, len, HAL_MAX_DELAY);
break;
case 3:
rx_len = HAL_UART_Receive(&huart3, data, len, HAL_MAX_DELAY);
break;
default:
break;
}
return rx_len;
}
```
在需要使用串口的地方,可以调用uart_send()函数发送数据,调用uart_receive()函数接收数据。需要注意的是,以上代码仅供参考,具体实现应根据实际需求进行调整。
阅读全文