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的管脚可以重映射

  • 8
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
上一期免费开源项目中,我最新发布了:最简单DIY的51蓝牙遥控小车设计方案,地址是:https://www.cirmall.com/circuit/20328当时测试用的是手机蓝牙调试助手来遥控智能小车的,那么这次开源免费项目中,我要用ESP32来实现串口蓝牙的功能来完全替代手机蓝牙调试助手来无线控制蓝牙智能小车。 优酷视频演示地址: 友情提示:蓝牙控制的设备是智能小车,想下载的买家可以到上面的网址免费下载,配套使用的。本次设计采用Arduino开发环境编写ESP32 C++程序,不会搭建开发环境的买家可以到:https://www.cirmall.com/circuit/19141自行按照说明搭建,这次为了改变口味,我用的是ESP32DEVKITV1开发板,价格更低十几块在某宝就能买到,这次下载程序可以直接使用手机数据线跟ESP32连接,在arduino里面直接点击下载就可以把程序下载进去了。 彩图如下: 科普一下:串口蓝牙就是模块设定为master模式,自动连上蓝牙设备-智能小车(client模式),不需要使用AT指令(我这个方案的优势),连上蓝牙设备之后将串口的数据通过蓝牙方式转发出去(注意我用的不是串口蓝牙模块),最后达到遥控蓝牙智能小车的效果,我现在用arduino自带的串口调试助手来测试。 下面是全家福照片: 下面是本次项目的主角: 源码代码截图: 注意了:我用的蓝牙设备-智能小车上安装的是串口蓝牙模块的型号是HC-05,所以我的串口蓝牙遥控器编写的程序里面写死了适配蓝牙的名字和密码,买家根据自己的情况来修改了。 下面是串口调试截图: 按照图上的配置信息配置,烧录源码,重启,输入FFF,点击“发送”按钮,小车就前进,串口调试窗口就看到小车回复了FFF,输入BBB就是倒退,左转是LLL,右转是RRR。 稍微说一下程序原理:电脑串口传输字符串给ESP32ESP32将字符串通过蓝牙天线发送给智能小车,智能小车的串口蓝牙接收到信号之后触发串口中断,在中断服务程序执行控制小车行动的逻辑,然后再返回一个相同的字符串给ESP32ESP32接收到字符串之后打印到电脑串口显示端上显示出来。 下一期我将会用ESP32DEVKITV1开发板做一个脱离电脑的串口蓝牙遥控器,通过摇杆AD转换成蓝牙控制信号来控制智能小车,敬请期待。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值