找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4481|回復: 1
打印 上一主題 下一主題
收起左側

STM32 USART2的初始化方法

[復制鏈接]
跳轉到指定樓層
樓主
ID:650811 發(fā)表于 2019-12-4 16:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
網(wǎng)上找到的都是 USART1的。。我寫了一個USART2的。在103上驗證通過。請參考。。
void USART2_Initialise( u32 bound )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
        USART_ClockInitTypeDef USART_ClockInitStruct;
    USART_InitTypeDef USART_InitStructure;

    /* Enable the USART2 Pins Software Remapping */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //開啟USART2時鐘

    /* Configure USART2 Rx (PA.03) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Configure USART2 Tx (PA.02) as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

        //配置USART2
    USART_InitStructure.USART_BaudRate = bound;                //波特率9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位數(shù)據(jù)
    USART_InitStructure.USART_StopBits = USART_StopBits_1;     //在幀結尾傳輸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;//發(fā)送、接收使能
        //配置USART2時鐘
        USART_ClockInitStruct.USART_Clock = USART_Clock_Disable;  //時鐘低電平活動
        USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low;  //SLCK引腳上時鐘輸出的極性->低電平
        USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge;  //時鐘第二個邊沿進行數(shù)據(jù)捕獲
        USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //最后一位數(shù)據(jù)的時鐘脈沖不從SCLK輸出

    USART_Init(USART2, &USART_InitStructure);
        USART_ClockInit(USART2, &USART_ClockInitStruct);

    /* Enable the USART2 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    /* Enable USART2 */
    USART_Cmd(USART2, ENABLE);
}

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:723746 發(fā)表于 2020-4-24 17:45 | 只看該作者
感謝分享
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表