STM32第十二节(中级篇):串口通信(第二节)——串口固件库函数以及串口发送和接收代码讲解
串口固件库函数
代码片段
/**
* @brief USART Init Structure definition
*/
typedef struct
{
uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate.
The baud rate is computed using the following formula:
- IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
This parameter can be a value of @ref USART_Word_Length */
uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.
This parameter can be a value of @ref USART_Stop_Bits */
uint16_t USART_Parity; /*!< Specifies the parity mode.
This parameter can be a value of @ref USART_Parity
@note When parity is enabled, the computed parity is inserted
at the MSB position of the transmitted data (9th bit when
the word length is set to 9 data bits; 8th bit when the
word length is set to 8 data bits). */
uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
This parameter can be a value of @ref USART_Mode */
uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
or disabled.
This parameter can be a value of @ref USART_Hardware_Flow_Control */
} USART_InitTypeDef;
本节课的代码是基础的配置,在后续学习以及编写代码的时候有很大的作用在上述代码中,就是对于USART结构体的初始化,我们可以在野火的资料盘中找到,在stm32f10x_usart.h文件中可以找到。我们观察这段代码,逐行解释代码部分。
结构体参数解释
uint32 _t USART BaudRate; //波特率 BRR
uint16_t USART WordLength; //字长 CR_1/M
uint16_t USART StopBits; //停止位 CR_2/STOP
uint16_t USART Parity; &nb