×
单片机 > 单片机程序设计 > 详情

STM32 UART 初始化

发布时间:2020-05-18 发布时间:
|

因为想用串口3,但配置了很长时间还是不行,为什么UART1,2行,UART3就不行的,最后原因是:使能GPIOB,端口时钟  USART3时钟,我只使能了UART3时钟,没有使能UART3所在端口GPIOB的时钟,所以导致无法正常启动串口3。

 

下面具体写下串口配置过程:
  1:系统时钟初始化,包括系统时钟和要开放的IO口和串口的时钟配置。
  2:IO口初始化,包括引脚,速率,输入输出模式等。
  3:配置USART的波特率,数据位等。

 


对应的3个函数,相当有条理

/--------------——————---------------------------------------------------------------------/

void  RCC_Configuration(void)

{

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能UART3所在GPIOB的时钟

       RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口时钟配置

}

 

void GPIO_Configuration(void)
{
      GPIO_InitTypeDef GPIO_InitStructure;

      // Configure USART3 Tx (PB.10) as alternate function push-pull 
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOB,   &GPIO_InitStructure);

      // Configure USART3 Rx (PB.11) as input floating 
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;          
      GPIO_Init(GPIOB, &GPIO_InitStructure);

}

 

void USART_Configuration(void)

{

      USART_InitStructure.USART_BaudRate = 38400;
      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(USART3, &USART_InitStructure);
      // 使能 USART3
      USART_Cmd(USART3, ENABLE);

}




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
PIC单片机的多路AD切换程序设计