如何使用 ESP32 RMT 模块生成反向脉冲(Arduino & PlatformIO)

在我们之前的文章 使用 Arduino & PlatformIO 的 ESP32 RMT 脉冲生成最小示例 中使用了 RMT 外设。脉冲的稳态(关闭状态)为 0V,脉冲电压为 3.3V

如果我们想生成反向脉冲,必须反转 pulseRMT 数组中的 level 条目:

pulseRMT_example.cpp
static const rmt_item32_t pulseRMT[] = {
    {{{
      /*pulse duration=*/100, /*pulse level*/0,
      // After pulse, output 1
      0, 1
    }}} ,
};

并额外配置脉冲完成时的 RMT 输出

rmt_idle_level_config.cpp
config.tx_config.idle_level = RMT_IDLE_LEVEL_HIGH;
config.tx_config.idle_output_en = true;

脉冲如下所示:

ESP32 RMT 反向脉冲示波器轨迹,高空闲电平和低脉冲

完整示例:

esp32_rmt_inverted_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*/0,
      // After pulse, output 1
      0, 1
    }}},
};

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
  config.tx_config.idle_level = RMT_IDLE_LEVEL_HIGH;
  config.tx_config.idle_output_en = true;

  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

Check out similar posts by category: Arduino, C/C++, ESP8266/ESP32, PlatformIO