现在的位置: 首页 > 综合 > 正文

STM32 学习笔记——USART

2014年03月06日 ⁄ 综合 ⁄ 共 4015字 ⁄ 字号 评论关闭

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 - ((uint32_t) 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;
这个结构体是ST库中的结构体,结构体实体在stm32f10x_usart.h

USART_BaudRate
该成员设置了USART 传输的波特率,波特率可以由以下公式计算:
IntegerDivider = ((APBClock) / (16 * (USART_InitStruct->USART_BaudRate)))
FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5

USART_WordLength                   描述
USART_WordLength_8b                8 位数据
USART_WordLength_9b                9 位数据

USART_StopBits                     描述
USART_StopBits_1                   在帧结尾传输 1 个停止位
USART_StopBits_0.5                 在帧结尾传输0.5 个停止位
USART_StopBits_2                   在帧结尾传输2 个停止位
USART_StopBits_1.5                 在帧结尾传输 1.5 个停止位

USART_Parity                       描述
USART_Parity_No                    奇偶失能
USART_Parity_Even                  偶模式
USART_Parity_Odd                   奇模式

USART_HardwareFlowControl          描述
USART_HardwareFlowControl_None     硬件流控制失能
USART_HardwareFlowControl_RTS      发送请求RTS 使能
USART_HardwareFlowControl_CTS      清除发送 CTS 使能
USART_HardwareFlowControl_RTS_CTS  RTS 和CTS 使能

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
打开USART1的时钟

例子:A9-Tx  A10-Rx
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);//发送数据的最开始,需要清除一下USART的标志位,否则,第1位数据会丢失。因为在硬件复位之后,USART的状态位TC是置位的。
while (1)
{  
    USART_SendData(USART1,'A');
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);/*注意在对数据进行发送和接收的时候,要检查USART的状态,只有等到数据发送或接收完毕之后才能进行下一帧数据的发送或接收*/
    char buf=USART_ReceiveData(USART1);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
}

抱歉!评论已关闭.