新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 UART的使用過程

        STM32 UART的使用過程

        作者: 時間:2016-11-17 來源:網絡 收藏
        1、使用UART前必須啟動相應的外設時鐘,其主要用到固件庫的RCC_APBnPeriphClockCmd函數。
        使能UART1:使用RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE)
        使能UART2:使用RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE)
        2、使用中斷進行UART操作的需要配置NVIC,設置中斷優先級。如:
        /* 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);

        3、配置相應的GPIO口。
        如果系統的UART需要進行重映射,需要使用GPIO_PinRemapConfig函數進行重映射,如:GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//注意:Rx為浮空,Tx為第二功能上拉。
        將Rx配置為:浮空輸入模式,Tx配置為帶上拉的第二功模式。并用GPIO_Init() 函數初始化。如:
        /* Configure USART2 Rx PA3 input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Configure USART1 Tx (PA.09) as alternate 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);
        4、配置UART
        當在conf文件種配置正確的外晶振后,在USART_InitTypeDef定義的結構體種直接寫入UART的波特率、通訊長頓、模式、硬件通訊控制,收發模式。再用USART_Init()進行初始化。如:
        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 USART1 */
        USART_Init(USART1, &USART_InitStructure);

        而后使能收發中斷。如:
        /* Enable USART1 Receive and Transmit interrupts */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
        // USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
        注意:一般不將TXE中斷使能。因為一旦將此中斷使能,如果UART發送緩沖區空,則會立即進入UART中斷中,因此可在程序中需要發送數據處,使能TXE中斷。在UART中斷種用USART_SendData()來發送數據。
        完成中斷使能后,還需要使能UART口:
        如:
        /* Enable the USART1 */
        USART_Cmd(USART1, ENABLE);
        /* Enable the USART2 */
        USART_Cmd(USART2, ENABLE);

        中斷程序(stm32f10x_it.c)可以如下完成發送:注意所有的的串口中斷需要在中斷服務程序中判斷中斷源以分別處理。
        void USART1_IRQHandler(void)
        {
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {
        /* Read one byte from the receive data register */
        RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);
        if(RxCounter1 == NbrOfDataToRead1)
        {
        USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);//發送完成后,將RXNE禁止。
        }
        }

        if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
        {
        USART_SendData(USART1, TxBuffer1[TxCounter1++]);
        if(TxCounter1 == NbrOfDataToTransfer1)
        {
        USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
        }
        }
        }


        關鍵詞: STM32UART使用過

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 榆社县| 浮山县| 志丹县| 平阴县| 延川县| 漠河县| 永年县| 开封县| 临安市| 绥芬河市| 五峰| 高台县| 延寿县| 颍上县| 桂东县| 辽宁省| 和硕县| 衡阳县| 石首市| 潮州市| 麻城市| 定南县| 运城市| 衡阳县| 霍林郭勒市| 凤山市| 东源县| 利川市| 聊城市| 兴城市| 梅河口市| 新巴尔虎右旗| 高平市| 富蕴县| 内乡县| 玉田县| 河南省| 东宁县| 新泰市| 济源市| 冕宁县|