ESP32开发(四)——UART发送数据(不带中断)

本文介绍了ESP32如何配置和使用UART进行串口通信,包括串口参数配置、引脚设置、驱动安装、数据发送与接收。同时强调了在编写代码时避免与库文件中的函数名冲突的重要性。
摘要由CSDN通过智能技术生成

一.相关函数

(1)串口配置

//参数:uart_num:串口号
//     uart_config:串口配置结构体,与GPIO结构体类似
//作用:配置串口参数
esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)

 (2)设置引脚

//参数:uart_num:串口好
//     tx_io_num:tx管脚
//     rx_io_num:rx管脚
//     rts_io_num:
//     cts_io_num:
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)

(3)安装驱动

//参数:uart_num:串口号
//     rx_buffer_size:接收缓存大小
//     tx_buffer_size:发送缓存大小
//     event_queue_size:队列大小
//     uart_queue:事件队列首地址
//     intr_alloc_flags:中断标志
esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int event_queue_size, QueueHandle_t *uart_queue, int intr_alloc_flags)

(4)发送数据和接收数据

// Write data to UART.
char* test_str = "This is a test string.\n";
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));


// Read data from UART.
//调用 uart_get_buffered_data_len() 能够查看 Rx FIFO 缓冲区中可用的字节数
const uart_port_t uart_num = UART_NUM_2;
uint8_t data[128];
int length = 0;
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
length = uart_read_bytes(uart_num, data, length, 100);

二.串口配置结构体

typedef struct {
    int baud_rate;                      /*!< UART baud rate*/
    uart_word_length_t data_bits;       /*!< UART byte size*/
    uart_parity_t parity;               /*!< UART parity mode*/
    uart_stop_bits_t stop_bits;         /*!< UART stop bits*/
    uart_hw_flowcontrol_t flow_ctrl;    /*!< UART HW flow control mode (cts/rts)*/
    uint8_t rx_flow_ctrl_thresh;        /*!< UART HW RTS threshold*/
    uart_sclk_t source_clk;             /*!< UART source clock selection */
} uart_config_t;

三.代码

  1. 设置通信参数 - 设置波特率、数据位、停止位等

  2. 设置通信管脚 - 分配连接设备的管脚

  3. 安装驱动程序 - 为 UART 驱动程序分配 ESP32 资源

  4. 运行 UART 通信 - 发送/接收数据

  5. 使用中断 - 触发特定通信事件的中断

  6. 删除驱动程序 - 如无需 UART 通信,则释放已分配的资源

步骤 1 到 3 为配置阶段,步骤 4 为 UART 运行阶段,步骤 5 和 6 为可选步骤。

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "driver/gpio.h"
#include "driver/uart.h"


#define RX1_BUF_SIZE (1024)
#define TX1_BUF_SIZE (512)
#define TXD1_PIN (GPIO_NUM_36)
#define RXD1_PIN (GPIO_NUM_37)

const uart_port_t UART = UART_NUM_0;

void uart1init(void)
{ 
//串口配置结构体

uart_config_t uart1_config;

//串口参数配置->uart1

uart1_config.baud_rate = 115200; //波特率

uart1_config.data_bits = UART_DATA_8_BITS; //数据位

uart1_config.parity = UART_PARITY_DISABLE; //校验位

uart1_config.stop_bits = UART_STOP_BITS_1; //停止位

uart1_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; //硬件流控

uart_param_config(UART, &uart1_config); //设置串口

//IO映射-> T:IO36  R:IO37

uart_set_pin(UART, TXD1_PIN, RXD1_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

//注册串口服务即使能+设置缓存区大小

uart_driver_install(UART, RX1_BUF_SIZE * 2, TX1_BUF_SIZE * 2, 0, NULL, 0);


}


void app_main(void)
{
    uart1init();
    while (1)
    {
        vTaskDelay(1000/portTICK_PERIOD_MS);  //延迟1s
        uart_write_bytes(UART, "uart1 test OK\r\n ", strlen("uart1 test OK\r\n "));
    }

}

四.现象

需要注意的是:在引入的"driver/uart.h"库中,也有些串口的定义,所以我们在定义自己的一些串口函数或者参数时,不要与"driver/uart.h"里面的参数重名了,否者会报错的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Crystal(mercy)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值