1139 views|2 replies

1452

Posts

1

Resources
The OP
 

【Portable Environmental Status Detector】UART Serial Communication [Copy link]

 

Serial communication is an important function. It can not only realize the communication between MCUs, but also control the devices that can be controlled by the serial port, such as serial-controlled ultrasonic distance acquisition, serial display, MP3 audio playback module and serial recorder.

It is reported that the UART of the ESP32-S2-Kaluga-1 development board can be defined for use in any port. For this purpose, USB-TTL can be used to connect the development board as follows:

USB-TTL Development Board

RX GPIO4

TX GPIO5

GND GND

To realize the data transmission and reception processing of the serial port, three parts are required:

1) Header files and related variables

The header files involved are as follows:

#include "driver/uart.h"

The variables involved are:

#define UART_TXD (4)

#define UART_RXD (5)

#define UART_RTS (UART_PIN_NO_CHANGE)

#define UART_CTS (UART_PIN_NO_CHANGE)

#define UART_PORT_NUM (1)

#define UART_BAUD_RATE (115200)

#define UART_BUF_SIZE (1024)

2) Test function

In this test function, the initialization and test processing of the serial port are included, and its contents are as follows:

static void Uart_test(void)
{
 uart_config_t uart_config = {
 .baud_rate = UART_BAUD_RATE,
 .data_bits = UART_DATA_8_BITS,
 .parity = UART_PARITY_DISABLE,
 .stop_bits = UART_STOP_BITS_1,
 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
 .source_clk = UART_SCLK_APB,
 };

 int intr_alloc_flags = 0;

 #if CONFIG_UART_ISR_IN_IRAM
 intr_alloc_flags = ESP_INTR_FLAG_IRAM;
 #endif

 ESP_ERROR_CHECK(uart_driver_install(UART_PORT_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
 ESP_ERROR_CHECK(uart_param_config(UART_PORT_NUM, &uart_config));
 ESP_ERROR_CHECK(uart_set_pin(UART_PORT_NUM, UART_TXD, UART_RXD, UART_RTS, UART_CTS));
 uint8_t *data = (uint8_t *) malloc(UART_BUF_SIZE);
 while (1)
 {
 // Read data from the UART
 int len = uart_read_bytes(UART_PORT_NUM, data, UART_BUF_SIZE, 20 / portTICK_RATE_MS);
 // Write data back to the UART
 uart_write_bytes(UART_PORT_NUM, (constchar *) data, len);
 }
}

3) Main program

The main program to implement the test is very simple, and its contents are as follows:

voidapp_main(void)
{
  Uart_test();
}

After the program is compiled and downloaded, the test result of data transmission and reception is shown in Figure 1.

Figure 1 Data transmission and reception test

To test the serial port sending byte data and instructions, you can add the following variables and test statements in the test function:

char udat[5]={0x30,0x31,0x32,0x33,0x34};

uart_write_bytes(UART_PORT_NUM, (constchar *) udat, 5);Uart_test();

The test result is shown in Figure 2. By storing the instructions of the serial port device into an array, the instructions can be controlled in this way.

Figure 2 Send byte data test

This post is from DigiKey Technology Zone

Latest reply

66666, classic!  Details Published on 2022-10-17 07:02
 
 

6820

Posts

11

Resources
2
 
66666, classic!
This post is from DigiKey Technology Zone

Comments

Thanks for your support!!!  Details Published on 2022-10-17 08:38
 
 
 

1452

Posts

1

Resources
3
 

Thanks for your support!!!

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list