打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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个步骤就可以正常通信

四、完整例子

  1. /* UART Echo Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "driver/uart.h"
  11. #include "string.h"
  12. /**
  13. * This is an example which echos any data it receives on UART1 back to the sender,
  14. * with hardware flow control turned off. It does not use UART driver event queue.
  15. *
  16. * - Port: UART1
  17. * - Receive (Rx) buffer: on
  18. * - Transmit (Tx) buffer: off
  19. * - Flow control: off
  20. * - Event queue: off
  21. * - Pin assignment: see defines below
  22. */
  23. //#define ECHO_TEST_TXD (GPIO_NUM_4)
  24. //#define ECHO_TEST_RXD (GPIO_NUM_5)
  25. #define ECHO_TXD0 (GPIO_NUM_1)
  26. #define ECHO_RXD0 (GPIO_NUM_3)
  27. #define ECHO_TXD1 (GPIO_NUM_23)
  28. #define ECHO_RXD1 (GPIO_NUM_22)
  29. #define ECHO_TXD2 (GPIO_NUM_21)
  30. #define ECHO_RXD2 (GPIO_NUM_19)
  31. #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
  32. #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
  33. #define BUF_SIZE (1024)
  34. static void Uart0Recv()
  35. {
  36. /* Configure parameters of an UART driver,
  37. * communication pins and install the driver */
  38. uart_config_t uart_config = {
  39. .baud_rate = 115200,
  40. .data_bits = UART_DATA_8_BITS,
  41. .parity = UART_PARITY_DISABLE,
  42. .stop_bits = UART_STOP_BITS_1,
  43. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  44. };
  45. uart_param_config(UART_NUM_0, &uart_config);
  46. uart_set_pin(UART_NUM_0, ECHO_TXD0, ECHO_RXD0, ECHO_TEST_RTS, ECHO_TEST_CTS);
  47. uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0);
  48. // Configure a temporary buffer for the incoming data
  49. uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
  50. while (1) {
  51. // Read data from the UART
  52. int len = uart_read_bytes(UART_NUM_0, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  53. if(len > 0)
  54. uart_write_bytes(UART_NUM_0, (const char *) "uart0:", strlen("uart1:"));
  55. // Write data back to the UART
  56. uart_write_bytes(UART_NUM_0, (const char *) data, len);
  57. }
  58. }
  59. static void Uart1Recv()
  60. {
  61. /* Configure parameters of an UART driver,
  62. * communication pins and install the driver */
  63. uart_config_t uart_config = {
  64. .baud_rate = 115200,
  65. .data_bits = UART_DATA_8_BITS,
  66. .parity = UART_PARITY_DISABLE,
  67. .stop_bits = UART_STOP_BITS_1,
  68. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  69. };
  70. uart_param_config(UART_NUM_1, &uart_config);
  71. uart_set_pin(UART_NUM_1, ECHO_TXD1, ECHO_RXD1, ECHO_TEST_RTS, ECHO_TEST_CTS);
  72. uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
  73. // Configure a temporary buffer for the incoming data
  74. uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
  75. while (1) {
  76. // Read data from the UART
  77. int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  78. // Write data back to the UART
  79. if(len > 0)
  80. uart_write_bytes(UART_NUM_1, (const char *) "uart1:", strlen("uart1:"));
  81. uart_write_bytes(UART_NUM_1, (const char *) data, len);
  82. }
  83. }
  84. static void Uart2Recv()
  85. {
  86. /* Configure parameters of an UART driver,
  87. * communication pins and install the driver */
  88. uart_config_t uart_config = {
  89. .baud_rate = 115200,
  90. .data_bits = UART_DATA_8_BITS,
  91. .parity = UART_PARITY_DISABLE,
  92. .stop_bits = UART_STOP_BITS_1,
  93. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  94. };
  95. uart_param_config(UART_NUM_2, &uart_config);
  96. uart_set_pin(UART_NUM_2, ECHO_TXD2, ECHO_RXD2, ECHO_TEST_RTS, ECHO_TEST_CTS);
  97. uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);
  98. // Configure a temporary buffer for the incoming data
  99. uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
  100. while (1) {
  101. // Read data from the UART
  102. int len = uart_read_bytes(UART_NUM_2, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  103. // Write data back to the UART
  104. if(len > 0)
  105. uart_write_bytes(UART_NUM_2, (const char *) "uart2:", strlen("uart1:"));
  106. uart_write_bytes(UART_NUM_2, (const char *) data, len);
  107. }
  108. }
  109. void app_main()
  110. {
  111. xTaskCreate(Uart0Recv, "uart_echo_task", 1024, NULL, 10, NULL);
  112. xTaskCreate(Uart1Recv, "Uart1Recv", 1024, NULL, 11, NULL);
  113. xTaskCreate(Uart2Recv, "Uart1Recv", 1024, NULL, 11, NULL);
  114. }

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

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
BLE协议栈——UART DMA工作方式
C语言的一些“骚操作”及其深层理解
esp8266中文资料汇总(esp8266引脚图_与单片机连接_串口wifi实例)
ESP8266 wifi模块开发汇总
TT无人机 Arduino环境探索
early
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服