美文网首页我爱编程
STM32一个函数配置所有串口的方法

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

作者: machester | 来源:发表于2017-09-03 04:13 被阅读0次

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);
}

相关文章

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

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

  • STM32 - Retarget printf to usart

    STM32 将 printf 重定向至 串口或自定义 printf 函数. 实现串口发送字符串函数。/** * ...

  • Linux串口烧录ST芯片固件

    一、解压安装stm32flash 下载地址 二、使用方法 三、烧录STM32 使stm32进入串口烧录模式(BOO...

  • STM32-printf重映射串口

    在STM32开发过程中必然需要串口打印参与调试,而往往STM32调试分配串口不会固定,可能是在串口1,也有可能为串...

  • 2018-09-13

    # STM32之串口DMA接收不定长数据 ## 引言 在使用stm32或者其他单片机的时候,会经常使用到串口通讯,...

  • STM32F407 DMA USART

    STM32 USART串口DMA接收和发送模式

  • STM32F407串口中断服务程序

      在对串口进行配置时首先需要对对应的引脚进行端口复用映射,因为STM32一个端口有多个用处,另外即使对于同一个功...

  • 【HAL库】STM32上手体验 之 UART 串口应用

    【HAL库】STM32上手体验 之 UART 串口应用 串口是非常方便的调试工具,如果没有串口很多程序只能根据可见...

  • STM32 IO模拟实现软件串口

    最近项目中STM32的串口资源紧张,于是使用IO口进行模拟串口,现进行整理记录。 1. 实现思路 IO口模拟串口的...

  • 循环缓冲区RingBuffer

    stm32和外设通信的时候,需要对外设发来的串行数据做同步。参考过下面这个链接的方法:串口通信帧的同步方法(识别一...

网友评论

    本文标题:STM32一个函数配置所有串口的方法

    本文链接:https://www.haomeiwen.com/subject/gesdjxtx.html