zoukankan      html  css  js  c++  java
  • stm32 USART串口通信

     

    UART初始化函数:

    HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)

    参数结构体:

    typedef struct
    {
      USART_TypeDef            *Instance;        /*!< UART registers base address        */
    
      UART_InitTypeDef         Init;             /*!< UART communication parameters      */
    
      UART_AdvFeatureInitTypeDef AdvancedInit;   /*!< UART Advanced Features initialization parameters */
    
      uint8_t                  *pTxBuffPtr;      /*!< Pointer to UART Tx transfer Buffer */
    
      uint16_t                 TxXferSize;       /*!< UART Tx Transfer size              */
    
      uint16_t                 TxXferCount;      /*!< UART Tx Transfer Counter           */
    
      uint8_t                  *pRxBuffPtr;      /*!< Pointer to UART Rx transfer Buffer */
    
      uint16_t                 RxXferSize;       /*!< UART Rx Transfer size              */
    
      uint16_t                 RxXferCount;      /*!< UART Rx Transfer Counter           */
    
      uint16_t                 Mask;             /*!< UART Rx RDR register mask          */
    
      DMA_HandleTypeDef        *hdmatx;          /*!< UART Tx DMA Handle parameters      */
    
      DMA_HandleTypeDef        *hdmarx;          /*!< UART Rx DMA Handle parameters      */
    
      HAL_LockTypeDef           Lock;            /*!< Locking object                     */
    
      __IO HAL_UART_StateTypeDef    gState;      /*!< UART state information related to global Handle management 
                                                      and also related to Tx operations.
                                                      This parameter can be a value of @ref HAL_UART_StateTypeDef */
    
      __IO HAL_UART_StateTypeDef    RxState;     /*!< UART state information related to Rx operations.
                                                      This parameter can be a value of @ref HAL_UART_StateTypeDef */
    
      __IO uint32_t             ErrorCode;   /*!< UART Error code                    */
    
    }UART_HandleTypeDef;

    第二个成员的结构体:

    typedef struct
    {
      uint32_t BaudRate;                  /*!< This member configures the UART communication baud rate.
                                               The baud rate register is computed using the following formula:
                                               - If oversampling is 16 or in LIN mode,
                                                  Baud Rate Register = ((PCLKx) / ((huart->Init.BaudRate)))
                                               - If oversampling is 8,
                                                  Baud Rate Register[15:4] = ((2 * PCLKx) / ((huart->Init.BaudRate)))[15:4]
                                                  Baud Rate Register[3] =  0
                                                  Baud Rate Register[2:0] =  (((2 * PCLKx) / ((huart->Init.BaudRate)))[3:0]) >> 1      */
    
      uint32_t WordLength;                /*!< Specifies the number of data bits transmitted or received in a frame.
                                               This parameter can be a value of @ref UARTEx_Word_Length */
    
      uint32_t StopBits;                  /*!< Specifies the number of stop bits transmitted.
                                               This parameter can be a value of @ref UART_Stop_Bits */
    
      uint32_t Parity;                    /*!< Specifies the parity mode.
                                               This parameter can be a value of @ref UART_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). */
    
      uint32_t Mode;                      /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
                                               This parameter can be a value of @ref UART_Mode */
    
      uint32_t HwFlowCtl;                 /*!< Specifies whether the hardware flow control mode is enabled
                                               or disabled.
                                               This parameter can be a value of @ref UART_Hardware_Flow_Control */
    
      uint32_t OverSampling;              /*!< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
                                               This parameter can be a value of @ref UART_Over_Sampling */
    
      uint32_t OneBitSampling;            /*!< Specifies whether a single sample or three samples' majority vote is selected.
                                               Selecting the single sample method increases the receiver tolerance to clock
                                               deviations. This parameter can be a value of @ref UART_OneBit_Sampling */
    }UART_InitTypeDef;

    示例:

    u8 aRxBuffer[RXBUFFERSIZE];//HAL库使用的串口接收缓冲
    UART_HandleTypeDef UART1_Handler; //UART句柄
    
    //初始化IO 串口1 
    //bound:波特率
    void uart_init(u32 bound)
    {    
        //UART 初始化设置
        UART1_Handler.Instance=USART1;                        //USART1
        UART1_Handler.Init.BaudRate=bound;                    //波特率
        UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B;   //字长为8位数据格式
        UART1_Handler.Init.StopBits=UART_STOPBITS_1;        //一个停止位
        UART1_Handler.Init.Parity=UART_PARITY_NONE;            //无奇偶校验位
        UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;   //无硬件流控
        UART1_Handler.Init.Mode=UART_MODE_TX_RX;            //收发模式
        HAL_UART_Init(&UART1_Handler);                        //HAL_UART_Init()会使能UART1
        
        HAL_UART_Receive_IT(&UART1_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
      
    }

    发送数据:

    HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)

    第一个参数:句柄 (和HAL_UART_Init的参数相同)

    第二个参数:发送数据的数组指针

    第三个参数:数组长度

    第四个参数:超时时间

  • 相关阅读:
    Ubuntu18.04 一条命令安装caffe问题
    ubuntu18 + caffe+cpu+anaconda3
    ubuntu18+caffe+cuda
    opencv-python常用接口
    tensorflow-gpu安装
    【LabVIEW】如何给输入控件赋值
    【LabVIEW】控件初始化
    【LabVIEW】程序结构
    【LabVIEW】程序框图中错误输出后status如何调出
    【总线】ARINC 429总线协议
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/12057786.html
Copyright © 2011-2022 走看看