硬件:
esp32模块
rs232 转ttl 3.3v 电平转换器
3.3v 外接电源
esp32 tx 脚接转换器rx, rx脚接转换器tx
esp32 使用uart2
现在就可以用pc作为上位机通过串口控制esp32,用pc串口调试软件作为esp的输出监控器显示esp的各种运算结果。
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "esp_log.h"
#include <string.h>
#include "driver/gpio.h"
#define UART_NUM UART_NUM_2
#define TXD_PIN 17 //17
#define RXD_PIN 16 //16
#define BUF_SIZE 1024
void init_uart() {
const 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, &uart_config);
uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0);
}
void app_main() {
init_uart();
uint8_t data[128];
int length = 0;
while (1) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM, (size_t*)&length));
length = uart_read_bytes(UART_NUM, data, length, 100); //从pc串口接收
uart_write_bytes(UART_NUM, data,length); //向pc 串口发送
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
上面程序是esp32 接收pc串口数据后,再发送到pc 串口
esp32主要作用是控制各种外设,所以最重要是学习各种外设总线。
先从I2c 开始