中文字幕理论片,69视频免费在线观看,亚洲成人app,国产1级毛片,刘涛最大尺度戏视频,欧美亚洲美女视频,2021韩国美女仙女屋vip视频

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
ESP32 3個串口使用

一、ESP32總共有3個串口,并且3個 串口管腳都是可以重映射的

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

2、ESP32串口函數(shù)介紹

3、例子代碼

二、ESP32串口使用的基本步驟 官網(wǎng)有詳細串口說明

  1. 設置通信參數(shù)波特率、數(shù)據(jù)位、停止位等   --設置參數(shù)
  2. 設置通訊-其他UART連接到的引腳             --設置具體的管腳及是否選擇流控位
  3. 驅(qū)動器安裝-為UART驅(qū)動程序分配ESP 32的資源  --分配接收發(fā)送空間
  4. 運行UART通信-發(fā)送/接收數(shù)據(jù)                             --串口收發(fā)
  5. 使用中斷-觸發(fā)對特定通信事件的中斷                 --注冊中斷
  6. 刪除驅(qū)動程序-釋放esp 32的資源,如果不再需要uart通信。

使用UART是完成前面4個機可以實現(xiàn)UART的收發(fā),最后兩個是可選的

三、串口函數(shù)的介紹  按照基本步驟介紹函數(shù)說明

1、通信參數(shù)設置

uart_config_t uart_config = {    .baud_rate = 115200,            //波特率    .data_bits = UART_DATA_8_BITS,  //數(shù)據(jù)位數(shù)    .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引腳關聯(lián)

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                  ------串口發(fā)送管腳

rts_io_num                -------流控腳

cts_io_num              --------流控腳

3、驅(qū)動安裝

分配接收發(fā)送空間及函數(shù)調(diào)用參數(shù)

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         ---------發(fā)送緩存大小

queue_size            ------------隊列大小

uart_queue            -------------串口隊列指針

intr_alloc_flags        --------------分配中斷標記

4、UART通信

接收函數(shù):

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                   -------------接收數(shù)據(jù)緩沖地址

uint32_t length             ------------接收緩沖區(qū)長度

TickType_t ticks_to_wait ---------等待時間

發(fā)送函數(shù):

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

uart_port_t uart_num    -------------串口號

const char* src             -------------待發(fā)送數(shù)據(jù)

size_t size                   ---------------發(fā)送數(shù)據(jù)大小

這里只有使用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的管腳可以重映射

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
BLE協(xié)議?!猆ART DMA工作方式
C語言的一些“騷操作”及其深層理解
esp8266中文資料匯總(esp8266引腳圖_與單片機連接_串口wifi實例)
ESP8266 wifi模塊開發(fā)匯總
TT無人機 Arduino環(huán)境探索
early
更多類似文章 >>
生活服務
熱點新聞
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服