新聞中心

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

        STM32單片機中UART的使用過程

        作者: 時間:2013-02-22 來源:網絡 收藏

        1、使用前必須啟動相應的外設時鐘,其主要用到固件庫的RCC_APBnPeriphClockCmd函數。

        使能1:使用RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE)

        使能2:使用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);


        上一頁 1 2 下一頁

        關鍵詞: STM32 UART 單片機

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 汤阴县| 厦门市| 武平县| 突泉县| 宽甸| 安龙县| 长垣县| 吴桥县| 辰溪县| 维西| 通化县| 贵港市| 县级市| 浪卡子县| 迁安市| 鹤岗市| 孝义市| 辽宁省| 佳木斯市| 平潭县| 朝阳县| 永靖县| 宽甸| 吴川市| 南昌市| 西城区| 临泽县| 安溪县| 榆林市| 璧山县| 农安县| 广元市| 九台市| 珠海市| 苏尼特左旗| 平塘县| 古交市| 奉贤区| 濮阳县| 宜宾县| 哈巴河县|