以下示例将每 10ms 生成 100us 脉冲。脉冲使用 RMT 外设生成。

esp32_rmt_pulse.cpp
#include <Arduino.h>
#include <esp_log.h>
#include <driver/rmt.h>
// Output pulse train on D14
constexpr gpio_num_t rmtPin = GPIO_NUM_14;
constexpr rmt_channel_t RMT_TX_CHANNEL = RMT_CHANNEL_0;
static const rmt_item32_t pulseRMT[] = {
{{{
/*pulse duration=*/100, /*pulse level*/1,
// After pulse, output 0
0, 0
}}},
};
void setup() {
Serial.begin(115200);
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(rmtPin, RMT_TX_CHANNEL);
config.clk_div = 80; // input clock 80 MHz => output clk 1 MHz
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
}
void loop() {
ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, pulseRMT, sizeof(pulseRMT) / sizeof(rmt_item32_t), true));
delay(10);
}platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
按类别查看类似文章:
Arduino C/C++ ESP8266/ESP32 PlatformIO
如果这篇文章对您有帮助,请考虑请我喝杯咖啡或通过 PayPal 捐款,以支持 TechOverflow 上新文章的研究与发布