找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1467|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

STM32一種通用串口功能實現(xiàn)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:980286 發(fā)表于 2021-11-30 11:06 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include "uart.h"

void uart_init(uart_def *uart)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

    if (uart->uart_ptr == USART1)// 使能時鐘
    {  
        RCC_APB2PeriphClockCmd(uart->gpio_rx_rcc | uart->gpio_tx_rcc | uart->uart_rcc, ENABLE);
    }
    else // USART2 USART3 UART4 UART5
    {
        RCC_APB2PeriphClockCmd(uart->gpio_rx_rcc | uart->gpio_tx_rcc, ENABLE);
        RCC_APB1PeriphClockCmd(uart->uart_rcc, ENABLE);
    }

    // tx gpio 復(fù)用推挽輸出
    GPIO_InitStructure.GPIO_Pin                         = uart->gpio_tx_pin;
    GPIO_InitStructure.GPIO_Speed                       = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode                        = GPIO_Mode_AF_PP;
    GPIO_Init(uart->gpio_tx, &GPIO_InitStructure);

    //rx gpio 浮動輸入
    GPIO_InitStructure.GPIO_Pin                         = uart->gpio_rx_pin;
    GPIO_InitStructure.GPIO_Speed                       = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode                        = GPIO_Mode_IN_FLOATING;
    GPIO_Init(uart->gpio_rx, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate                  = uart->baudrate;
    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;
    USART_Init(uart->uart_ptr, &USART_InitStructure);

    if (uart->uart_ptr == USART1)                                           //串口1開啟空閑中斷 20190715 nl668模塊通訊
    {
        USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
//        USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);                    //開啟串口DMA接收
    }
    else if(uart->uart_ptr == USART2)                                       //串口2開啟空閑中斷 20190725 cc2530通訊
    {
        USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
    }

//    if(uart->uart_ptr != USART1)                                          //串口1使用的DMA+串口空閑中斷 不需要開啟接收中斷
    USART_ITConfig(uart->uart_ptr, USART_IT_RXNE, ENABLE);                  //使能接收中斷

    USART_Cmd(uart->uart_ptr, ENABLE);                                      //使能USARTx

    NVIC_InitStructure.NVIC_IRQChannel                   = uart->irqn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}





舉例uart1的功能實現(xiàn)
static uart_def uart;

void uart1_init(uint32_t baudrate)
{
    uart.uart_rcc = RCC_APB2Periph_USART1;
    uart.uart_ptr = USART1;

    uart.gpio_rx_rcc = RCC_APB2Periph_GPIOA;
    uart.gpio_rx = GPIOA;
    uart.gpio_rx_pin = GPIO_Pin_10;

    uart.gpio_tx_rcc = RCC_APB2Periph_GPIOA;
    uart.gpio_tx = GPIOA;
    uart.gpio_tx_pin = GPIO_Pin_9;

    uart.irqn = USART1_IRQn;

    uart.baudrate = baudrate;

    uart_init(&uart);
}

void uart1_writeb(uint8_t byte)
{
    while (USART_GetFlagStatus(uart.uart_ptr, USART_FLAG_TC) == RESET);
    USART_SendData(uart.uart_ptr, byte);
    while (USART_GetFlagStatus(uart.uart_ptr, USART_FLAG_TXE) == RESET);
}

評分

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

查看全部評分

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

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表