ESP32 3个串口使用

一、ESP32总共有3个串口,并且3个 串口管脚都是可以重映射的

1、ESP32串口使用的基本步骤

2、ESP32串口函数介绍

3、例子代码

二、ESP32串口使用的基本步骤 官网有详细串口说明

  1. 设置通信参数波特率、数据位、停止位等   --设置参数
  2. 设置通讯-其他UART连接到的引脚             --设置具体的管脚及是否选择流控位
  3. 驱动器安装-为UART驱动程序分配ESP 32的资源  --分配接收发送空间
  4. 运行UART通信-发送/接收数据                             --串口收发
  5. 使用中断-触发对特定通信事件的中断                 --注册中断
  6. 删除驱动程序-释放esp 32的资源,如果不再需要uart通信。

使用UART是完成前面4个机可以实现UART的收发,最后两个是可选的

三、串口函数的介绍  按照基本步骤介绍函数说明

1、通信参数设置

uart_config_t uart_config = {
    .baud_rate = 115200,            //波特率
    .data_bits = UART_DATA_8_BITS,  //数据位数
    .parity = UART_PARITY_DISABLE,  //奇偶控制
    .stop_bits = UART_STOP_BITS_1,  //停止位
    .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS, //流控位
    .rx_flow_ctrl_thresh = UART_HW_FLOWCTRL_DISABLE,//控制模式
};

esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)

uart_port_t uart_num                       -----串口号 UART0    UART1  UART2

const uart_config_t *uart_config      -----串口配置信息

2、设置通信

设置UART和具体的物理GPIO引脚关联

esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)

uart_port_t uart_num  ------串口号 UART0    UART1  UART2

rx_io_num                   ------串口接收管脚

tx_io_num                  ------串口发送管脚

rts_io_num                -------流控脚

cts_io_num              --------流控脚

3、驱动安装

分配接收发送空间及函数调用参数

esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t *uart_queue, int intr_alloc_flags)
uart_num                -------串口号

rx_buffer_size         --------接收缓存大小

tx_buffer_size         ---------发送缓存大小

queue_size            ------------队列大小

uart_queue            -------------串口队列指针

intr_alloc_flags        --------------分配中断标记

4、UART通信

接收函数:

int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait)

uart_port_t uart_num    -------------串口号

uint8_t* buf                   -------------接收数据缓冲地址

uint32_t length             ------------接收缓冲区长度

TickType_t ticks_to_wait ---------等待时间

发送函数:

int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size)

uart_port_t uart_num    -------------串口号

const char* src             -------------待发送数据

size_t size                   ---------------发送数据大小

这里只有使用4个步骤就可以正常通信

四、完整例子

/* UART Echo Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "string.h"
/**
 * This is an example which echos any data it receives on UART1 back to the sender,
 * with hardware flow control turned off. It does not use UART driver event queue.
 *
 * - Port: UART1
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: off
 * - Pin assignment: see defines below
 */

//#define ECHO_TEST_TXD  (GPIO_NUM_4)
//#define ECHO_TEST_RXD  (GPIO_NUM_5)
#define ECHO_TXD0  (GPIO_NUM_1)
#define ECHO_RXD0  (GPIO_NUM_3)

#define ECHO_TXD1  (GPIO_NUM_23)
#define ECHO_RXD1  (GPIO_NUM_22)


#define ECHO_TXD2  (GPIO_NUM_21)
#define ECHO_RXD2  (GPIO_NUM_19)
#define ECHO_TEST_RTS  (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS  (UART_PIN_NO_CHANGE)

#define BUF_SIZE (1024)

static void Uart0Recv()
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_0, &uart_config);
    uart_set_pin(UART_NUM_0, ECHO_TXD0, ECHO_RXD0, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0);

    // Configure a temporary buffer for the incoming data
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);

    while (1) {
        // Read data from the UART
        int len = uart_read_bytes(UART_NUM_0, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        if(len > 0)
          uart_write_bytes(UART_NUM_0, (const char *) "uart0:", strlen("uart1:"));
        // Write data back to the UART
        uart_write_bytes(UART_NUM_0, (const char *) data, len);
    }
}


static void Uart1Recv()
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_1, &uart_config);
    uart_set_pin(UART_NUM_1, ECHO_TXD1, ECHO_RXD1, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);

    // Configure a temporary buffer for the incoming data
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);

    while (1) {
        // Read data from the UART
        int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        // Write data back to the UART
        if(len > 0)
          uart_write_bytes(UART_NUM_1, (const char *) "uart1:", strlen("uart1:"));
        uart_write_bytes(UART_NUM_1, (const char *) data, len);
    }
}


static void Uart2Recv()
{
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_2, &uart_config);
    uart_set_pin(UART_NUM_2, ECHO_TXD2, ECHO_RXD2, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);

    // Configure a temporary buffer for the incoming data
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);

    while (1) {
        // Read data from the UART
        int len = uart_read_bytes(UART_NUM_2, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        // Write data back to the UART
        if(len > 0)
          uart_write_bytes(UART_NUM_2, (const char *) "uart2:", strlen("uart1:"));
        uart_write_bytes(UART_NUM_2, (const char *) data, len);
    }
}
void app_main()
{
    xTaskCreate(Uart0Recv, "uart_echo_task", 1024, NULL, 10, NULL);
    xTaskCreate(Uart1Recv, "Uart1Recv", 1024, NULL, 11, NULL);
    xTaskCreate(Uart2Recv, "Uart1Recv", 1024, NULL, 11, NULL);
}

到这里基本就完成了3个串口的程序编写

我使用的是红色框哪里面的6个脚,ESP32的管脚可以重映射

  • 9
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做了不一定能实现但不做一定不会实现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值