新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32開發(fā)板入門教程 - 串口通訊 UART

        STM32開發(fā)板入門教程 - 串口通訊 UART

        作者: 時間:2016-11-09 來源:網(wǎng)絡 收藏

        通用同步異步收發(fā)器(USART)提供了一種靈活的方法來與使用工業(yè)標準NR 異步串行數(shù)據(jù)格式的外部設備之間進行全雙工數(shù)據(jù)交換。 USART利用分數(shù)波特率發(fā)生器提供寬范圍的波特率選擇。
        它支持同步單向通信和半雙工單線通信。它也支持LIN(局部互連網(wǎng)),智能卡協(xié)議和IrDA(紅外數(shù)據(jù)組織)SIR ENDEC規(guī)范,以及調(diào)制解調(diào)器(CTS/RTS)操作。它還允許多處理器通信。用于多緩沖器配置的DMA方式,可以實現(xiàn)高速數(shù)據(jù)通信。

        主要特性:
        全雙工的,異步通信
        NR 標準格式
        分數(shù)波特率發(fā)生器系統(tǒng)
        -發(fā)送和接收共用的可編程波特率,最高到4.5Mbits/s
        可編程數(shù)據(jù)字長度(8位或9位)
        可配置的停止位 -支持1或2個停止位
        LIN主發(fā)送同步斷開符的能力以及LIN從檢測斷開符的能力
        - 當USART硬件配置成LIN時,生成13位斷開符;檢測10/11位斷開符
        發(fā)送方為同步傳輸提供時鐘
        IRDA SIR 編碼器解碼器
        - 在正常模式下支持3/16位的持續(xù)時間
        智能卡模擬功能
        - 智能卡接口支持ISO7816 -3標準里定義的異步協(xié)議智能卡
        - 智能卡用到的0.5和1.5個停止位
        單線半雙工通信
        使用DMA的可配置的多緩沖器通信
        - 在保留的SRAM里利用集中式DMA緩沖接收/發(fā)送字節(jié)
        單獨的發(fā)送器和接收器使能位
        檢測標志
        - 接收緩沖器滿
        - 發(fā)送緩沖器空
        - 傳輸結(jié)束標志
        校驗控制
        - 發(fā)送校驗位
        - 對接收數(shù)據(jù)進行校驗
        四個錯誤檢測標志
        - 溢出錯誤
        - 噪音錯誤
        - 幀錯誤
        - 校驗錯誤
        10個帶標志的中斷源
        - CTS改變
        - LIN斷開符檢測
        - 發(fā)送數(shù)據(jù)寄存器
        - 發(fā)送完成
        - 接收數(shù)據(jù)寄存器
        - 檢測到總線為空
        - 溢出錯誤
        - 幀錯誤
        - 噪音錯誤
        - 校驗錯誤
        多處理器通信 - - 如果地址不匹配,則進入靜默模式
        從靜默模式中喚醒(通過空閑總線檢測或地址標志檢測)
        兩種喚醒接收器的方式
        - 地址位(MSB)
        - 空閑總線


        本文引用地址:http://www.104case.com/article/201611/317723.htm


        STM32的串口配置 也挺方便的

        首先是配置UART的GPIO口
        /*******************************************************************************
        * Function Name : UART1_GPIO_Configuration
        * Description : Configures the uart1 GPIO ports.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void UART1_GPIO_Configuration(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        // Configure USART1_Tx as alternate function push-pull
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        // Configure USART1_Rx as input floating
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        }

        然后是配置串口參數(shù)


        /* 如果使用查詢的方式發(fā)送和接收數(shù)據(jù) 則不需要使用串口的中斷
        如果需要使用中斷的方式發(fā)送和接收數(shù)據(jù) 則需要使能串口中斷
        函數(shù)原形 void USART_ITConfig(USART_TypeDef* USARTx, u16 USART_IT, FunctionalState NewState)
        功能描述 使能或者失能指定的 USART 中斷

        USART_IT 描述
        USART_IT_PE 奇偶錯誤中斷
        USART_IT_TXE 發(fā)送中斷
        USART_IT_TC 傳輸完成中斷
        USART_IT_RXNE 接收中斷
        USART_IT_IDLE 空閑總線中斷
        USART_IT_LBD LIN中斷檢測中斷
        USART_IT_CTS CTS中斷
        USART_IT_ERR 錯誤中斷

        */


        /*******************************************************************************
        * Function Name : UART1_Configuration
        * Description : Configures the uart1
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void UART1_Configuration(void)
        {

        USART_InitTypeDef USART_InitStructure;
        /* USART1 configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        */
        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;

        /* Configure the USART1*/
        USART_Init(USART1, &USART_InitStructure);

        /* Enable USART1 Receive and Transmit interrupts */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


        /* Enable the USART1 */
        USART_Cmd(USART1, ENABLE);
        }

        發(fā)送一個字符
        /*******************************************************************************
        * Function Name : Uart1_PutChar
        * Description : printf a char to the uart.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        u8 Uart1_PutChar(u8 ch)
        {
        /* Write a character to the USART */
        USART_SendData(USART1, (u8) ch);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
        {
        }
        return ch;
        }


        發(fā)送一個字符串
        /*******************************************************************************
        * Function Name : Uart1_PutString
        * Description : print a string to the uart1
        * Input : buf為發(fā)送數(shù)據(jù)的地址 , len為發(fā)送字符的個數(shù)
        * Output : None
        * Return : None
        *******************************************************************************/
        void Uart1_PutString(u8* buf , u8 len)
        {
        for(u8 i=0;i{
        Uart1_PutChar(*buf++);
        }
        }

        如果UART使用中斷發(fā)送數(shù)據(jù) 則需要修改stm32f10x_it.c 中的串口中斷函數(shù) 并且需要修改void NVIC_Configuration(void)函數(shù)

        在中斷里面的處理 原則上是需要簡短和高效 下面的流程是 如果接收到255個字符或者接收到回車符 則關(guān)閉中斷 并且把標志位UartHaveData 置1

        /*******************************************************************************
        * Function Name : USART1_IRQHandler
        * Description : This function handles USART1 global interrupt request.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void USART1_IRQHandler(void)
        {
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {
        /* Read one byte from the receive data register */
        RxBuffer[ RxCounter ] = USART_ReceiveData(USART1);
        if( RxCounter == 0xfe || /r == RxBuffer[ RxCounter ] )
        {
        /* Disable the USART1 Receive interrupt */
        USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
        RxBuffer[ RxCounter ] = /0;
        UartHaveData = 1;
        }

        RxCounter++;
        }
        }


        修改NVIC_Configuration函數(shù)

        /*******************************************************************************
        * Function Name : NVIC_Configuration
        * Description : Configures NVIC and Vector Table base location.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void NVIC_Configuration(void)
        {
        NVIC_InitTypeDef NVIC_InitStructure;

        #ifdef VECT_TAB_RAM
        /* Set the Vector Table base location at 0x20000000 */
        NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else /* VECT_TAB_FLASH */
        /* Set the Vector Table base location at 0x08000000 */
        NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif

        /* Configure the NVIC Preemption Priority Bits */
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

        /* Enable the USART1 Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

        }




        采用DMA方式進行串口通信

        使用了DMA功能以后,用戶程序中只需配置好DMA,開啟傳輸后,再也不需要操心,10K數(shù)據(jù)完成后會有標志位或中斷產(chǎn)生,期間可以做任何想做的事,非常方便。




        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 泰兴市| 大英县| 宜丰县| 长寿区| 宣武区| 宁远县| 东乌| 光山县| 中西区| 上犹县| 内乡县| 弥勒县| 六枝特区| 司法| 新民市| 龙泉市| 茶陵县| 阳信县| 绥滨县| 桐城市| 政和县| 界首市| 浑源县| 兰州市| 郑州市| 泗洪县| 紫阳县| 广安市| 洪洞县| 申扎县| 宝丰县| 凉山| 汕尾市| 博野县| 当涂县| 资源县| 惠水县| 平顺县| 呼玛县| 龙江县| 德保县|