硬件平台:NUCLEO-STM32F411RE
基础知识
嵌入式开发中,UART串口通信协议是我们常用的通信协议之一,全称叫做通用异步收发传输器(Universal Asynchronous Receiver/Transmitter)。
基本原理:
常见数据结构:
CubeMx配置
硬件平台:STM32F411RE-NUCLEO
UART参数:
这里是用UART2进行数据接受发送
打开UART并设置波特率等:
打开中断:
函数说明
1、HAL_UART_Receive_IT和HAL_UART_Receive的区别就是:中断接收是有数据到了才去读;直接接收是直接读取,如果超时就返回
2、HAL_UART_Receive_IT配置后,有数据来,计数会在调用中断函数之后自动减1。只有到计数为0时,才会关闭中断并调用回调函数。至此有数据来不再调用中断函数,因为中断已经失效。
HAL_UART_Transmit
Function name
HAL_StatusTypeDef HAL_UART_Transmit (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size, uint32_t Timeout)
Function description:
Sends an amount of data in blocking mode.
Parameters:
• huart: Pointer to a UART_HandleTypeDef structure that contains the configuration information for the specified UART module.
• pData: Pointer to data buffer (u8 or u16 data elements).
• Size: Amount of data elements (u8 or u16) to be sent
• Timeout: Timeout duration
Return values
• HAL: status
Notes
• When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), the sent data is handled as a set of u16. In this case, Size must indicate the number of u16 provided through pData.
HAL_UART_Receive
Function name
HAL_StatusTypeDef HAL_UART_Receive (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size, uint32_t Timeout)
Function description:
Receives an amount of data in blocking mode.
Parameters
• huart: Pointer to a UART_HandleTypeDef structure that contains the configuration information for the specified UART module.
• pData: Pointer to data buffer (u8 or u16 data elements).
• Size: Amount of data elements (u8 or u16) to be received.
• Timeout: Timeout duration
Return values
• HAL: status
Notes
• When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), the received data is handled as a set of u16. In this case, Size must indicate the number of u16 available through pData.
HAL_UART_Transmit_IT
Function name
HAL_StatusTypeDef HAL_UART_Transmit_IT (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size)
Function description
Sends an amount of data in non blocking mode.
Parameters
• huart: Pointer to a UART_HandleTypeDef structure that contains the configuration information for the specified UART module.
• pData: Pointer to data buffer (u8 or u16 data elements).
• Size: Amount of data elements (u8 or u16) to be sent
Return values
• HAL: status
Notes
• When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), the sent data is handled as a set of u16. In this case, Size must indicate the number of u16 provided through pData.
HAL_UART_Receive_IT
Function name
HAL_StatusTypeDef HAL_UART_Receive_IT (UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size)
Function description
Receives an amount of data in non blocking mode.
Parameters
• huart: Pointer to a UART_HandleTypeDef structure that contains the configuration information for the specified UART module.
• pData: Pointer to data buffer (u8 or u16 data elements).
• Size: Amount of data elements (u8 or u16) to be received.
Return values
• HAL: status
Notes
• When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), the received data is handled as a set of u16. In this case, Size must indicate the number of u16 available through pData.
代码实现
定义一个全局数组用于存放数据
int Rx_pointer = 0;
uint8_t buffer_Rx[100];
main.c中打开中断
uint8_t buffer_Rx[100];
__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
stm32f4xx_it.c中修改中断处理函数
/**
* @brief This function handles USART2 global interrupt.
*/
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
// receive interrupt
if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE))// huart2.Instance->SR & UART_FLAG_RXNE
{
__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
Rx_pointer ++;
buffer_Rx[Rx_pointer] = huart2.Instance->DR;
}
//idle interrupt
if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE)) // huart2.Instance->SR & UART_FLAG_IDLE
{
if(Rx_pointer!=0)
{
Rx_pointer = 0;
HAL_UART_Transmit(&huart2, buffer_Rx, sizeof(buffer_Rx), 100);//UART发送函数,将收到的重新发出来
}
__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_IDLE);
__HAL_UART_CLEAR_IDLEFLAG(&huart2);//一定要清除IEDLDFLAG 否则会一直卡在中断出不来
}
/* USER CODE END USART2_IRQn 1 */
}