硬件平台: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

最低0.47元/天 解锁文章
6057

被折叠的 条评论
为什么被折叠?



