STM32一个函数配置所有串口的方法

STM32一个函数配置所有串口的方法

  • code example:
/**
 * config usartx
 * @param USARTx   [USART lable, example: USART1, USART2, USART3]
 * @param baudRate [usart baudRate]
 */
void Usart_InitConfig(USART_TypeDef* USARTx, uint32_t baudRate)
{
    GPIO_InitTypeDef GPIO_InitStruction;
    USART_InitTypeDef Usartx_InitStruction;
    NVIC_InitTypeDef  NVIC_InitStruction;

    if(USARTx == USART1)
    {
        /******************** usart1 *************************************/
        /************** set usart1 parameter **********************/
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
        /************************* usart1 GPIO config ***********************/
        GPIO_InitStruction.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruction);

        GPIO_InitStruction.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;    
        GPIO_Init(GPIOA, &GPIO_InitStruction);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  // usart1 clock    
        Usartx_InitStruction.USART_BaudRate = baudRate;
        Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        Usartx_InitStruction.USART_WordLength = USART_WordLength_8b;
        Usartx_InitStruction.USART_StopBits = USART_StopBits_1;
        Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        Usartx_InitStruction.USART_Parity = USART_Parity_No;

        USART_Init(USART1, &Usartx_InitStruction);

        /************** set usart1 NVIC ***************************/    
        NVIC_InitStruction.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStruction.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStruction);   
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  //set usart1 interrupt type
        USART_Cmd(USART1, ENABLE);
    }
    else if(USARTx == USART2) 
    {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
        /************************* usart2 ***********************/
        GPIO_InitStruction.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStruction);

        GPIO_InitStruction.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;    
        GPIO_Init(GPIOA, &GPIO_InitStruction);  

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  // usart2 clock
        Usartx_InitStruction.USART_BaudRate = baudRate;
        Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        Usartx_InitStruction.USART_WordLength = USART_WordLength_8b;
        Usartx_InitStruction.USART_StopBits = USART_StopBits_1;
        Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        Usartx_InitStruction.USART_Parity = USART_Parity_No;
        USART_Init(USART2, &Usartx_InitStruction);

        /************** set usart2 NVIC ***************************/ 
        NVIC_InitStruction.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStruction.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStruction);
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);  //set usart2 interrupt type
        USART_Cmd(USART2, ENABLE);
    }
    else if(USARTx == USART3)
    {
        /************************* usart3 ***********************/
        GPIO_InitStruction.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStruction);
        
        GPIO_InitStruction.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;    
        GPIO_Init(GPIOB, &GPIO_InitStruction);  
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  // usart3 clock
        Usartx_InitStruction.USART_BaudRate = baudRate;
        Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
        Usartx_InitStruction.USART_WordLength = USART_WordLength_8b;
        Usartx_InitStruction.USART_StopBits = USART_StopBits_1;
        Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        Usartx_InitStruction.USART_Parity = USART_Parity_No;
        USART_Init(USART3, &Usartx_InitStruction); 

        /************** set usart3 NVIC ***************************/    
        NVIC_InitStruction.NVIC_IRQChannel = USART3_IRQn;
        NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStruction.NVIC_IRQChannelSubPriority = 2;
        NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStruction);

        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  //set usart3 interrupt type
        USART_Cmd(USART3, ENABLE);
    } 
    
/**************************** usartbuff struct init**************************/
    memset(UsartBuff.UsartRecBuff, '\0', BUFF_SIZE);
    memset(UsartBuff.UsartSendBuff, '\0', BUFF_SIZE);
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,967评论 19 139
  • 串口操作 串口操作需要的头文件 #include /*标准输入输出定义*/ #include /*标准函数库定...
    旅行家John阅读 1,395评论 0 3
  • 22年12月更新:个人网站关停,如果仍旧对旧教程有兴趣参考 Github 的markdown内容[https://...
    tangyefei阅读 35,231评论 22 257
  • error code(错误代码)=0是操作成功完成。error code(错误代码)=1是功能错误。error c...
    Heikki_阅读 3,456评论 1 9
  • 月亮插入黑夜的人间, 在凌晨到来时, 就变得微弱了 在灯光闪烁的年代里, 有谁还愿意倾听那心之音 白天黑夜,钟声,...
    涯下无冥阅读 208评论 1 1