ESP32:DAC 输出上无信号 - 对于连续波输出模式(连续/DMA 模式)

问题描述 投票:0回答:1

我想在 ESP32 的 DAC 输出上生成两个信号,即

sine
cosine
信号。但 DAC 输出上没有信号。

请不要提供余弦波输出模式(Cosine Mode)的答案。

我必须使用连续波输出模式(连续/DMA模式)

当然,我阅读了文档peripherals/dac.html,使用了示例代码,但没有得到积极的结果。

我正在使用 PlatformIO(如果有的话):

[env]
; Development platform for Espressif 32 series of microcontrollers
platform = https://github.com/Jason2866/platform-espressif32.git #Arduino/IDF5

framework = arduino

我的代码

main.cpp
文件:

#include <Arduino.h>
#include <ArduinoLog.h>

#include "soc/rtc.h"

#include "DAC_Signal_DMA.h"

void setup()
{

  Serial.begin(921600);
  delay(2000);

  // initialize logging
  Log.begin(LOG_LEVEL_VERBOSE, &Serial);

  Log.noticeln(F("%s() is configured"), __func__);

  DAC_Signal_DMA dac_Re_Im = DAC_Signal_DMA();
  delay(2000);

  dac_Re_Im.Start();
  delay(2000);

  Log.noticeln(F("DAC Signal DMA started"));
}

void loop()
{
  // Just for testing
  Serial.printf("Free heap size: %d\n", esp_get_free_heap_size());
  Serial.printf("%s(), core: %d: Running time [s]: %d\n", __func__, xPortGetCoreID(), millis());

  vTaskDelay(pdMS_TO_TICKS(1000 - 0));
}

DAC_Signal_DMA.h
文件:

一些代码被注释(为第二个 DAC 通道上的

cosine
信号保留)。目前,两个通道都配置为相同的信号。

#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/dac_channel.h"
#include "driver/dac_continuous.h"

// #include "esp_check.h"
// #include "dac_continuous_example.h"

#define EXAMPLE_DAC_CHAN0_IO DAC_CHAN0_GPIO_NUM // DAC channel 0 io number
#define EXAMPLE_DAC_CHAN1_IO DAC_CHAN1_GPIO_NUM // DAC channel 1 io number

#define EXAMPLE_ARRAY_LEN 500     // Length of wave array
#define EXAMPLE_DAC_AMPLITUDE 255 // Amplitude of DAC voltage. If it's more than 256 will causes

#define CONST_2_PI 6.2832 // 2 * PI

_Static_assert(EXAMPLE_DAC_AMPLITUDE < 256, "The DAC accuracy is 8 bit-width, doesn't support the amplitude beyond 255");

class DAC_Signal_DMA
{
public:
    DAC_Signal_DMA()
    {
        generate_waves();

        cont_cfg_0 = {
            .chan_mask = DAC_CHANNEL_MASK_ALL,
            .desc_num = 8,
            .buf_size = 2048,
            .freq_hz = frequency_convert_Hz,
            .offset = 0,
            .clk_src = DAC_DIGI_CLK_SRC_DEFAULT, // If the frequency is out of range, try 'DAC_DIGI_CLK_SRC_APLL'
            .chan_mode = DAC_CHANNEL_MODE_SIMUL,
        };

        // cont_cfg_1 = {
        //     .chan_mask = DAC_CHANNEL_MASK_CH1,
        //     .desc_num = 8,
        //     .buf_size = 2048,
        //     .freq_hz = frequency_convert_Hz,
        //     .offset = 0,
        //     .clk_src = DAC_DIGI_CLK_SRC_DEFAULT, // If the frequency is out of range, try 'DAC_DIGI_CLK_SRC_APLL'
        //     .chan_mode = DAC_CHANNEL_MODE_SIMUL,
        // };

        ESP_ERROR_CHECK(dac_continuous_write_cyclically(handle_0, (uint8_t *)squ_wav, buf_len, NULL));
        // ESP_ERROR_CHECK(dac_continuous_write_cyclically(handle_1, (uint8_t *)squ_wav, buf_len, NULL));

        Serial.println("DAC_Signal_DMA() constructed");
    }
    ~DAC_Signal_DMA()
    {
        Stop();
    }

    void Start()
    {
        /* Allocate continuous channel */
        ESP_ERROR_CHECK(dac_continuous_new_channels(&cont_cfg_0, &handle_0));
        delay(1000);
        /* Enable the channels in the group */
        ESP_ERROR_CHECK(dac_continuous_enable(handle_0));
        delay(1000);

        // /* Allocate continuous channel */
        // ESP_ERROR_CHECK(dac_continuous_new_channels(&cont_cfg_1, &handle_1));
        // delay(1000);
        // /* Enable the channels in the group */
        // ESP_ERROR_CHECK(dac_continuous_enable(handle_1));
        // delay(1000);

        Serial.println("DAC_Signal_DMA() started");
    }

    void Stop()
    {
        ESP_ERROR_CHECK(dac_continuous_disable(handle_0));
        ESP_ERROR_CHECK(dac_continuous_del_channels(handle_0));

        // ESP_ERROR_CHECK(dac_continuous_disable(handle_1));
        // ESP_ERROR_CHECK(dac_continuous_del_channels(handle_1));
    }

protected:
    void generate_waves(void)
    {
        uint32_t pnt_num = EXAMPLE_ARRAY_LEN;

        for (int i = 0; i < pnt_num; i++)
        {
            sin_wav[i] = (uint8_t)((sin(i * CONST_2_PI / pnt_num) + 1) * (double)(amplitude) / 2 + 0.5);
            cos_wav[i] = (uint8_t)((cos(i * CONST_2_PI / pnt_num) + 1) * (double)(amplitude) / 2 + 0.5);

            squ_wav[i] = (i < (pnt_num / 2)) ? amplitude : 0;
        }
    }

private:
    uint32_t frequency_Hz = 1000;
    uint32_t frequency_convert_Hz = EXAMPLE_ARRAY_LEN * frequency_Hz;
    size_t buf_len = EXAMPLE_ARRAY_LEN;
    uint8_t amplitude = EXAMPLE_DAC_AMPLITUDE;

    dac_continuous_config_t cont_cfg_0;
    dac_continuous_config_t cont_cfg_1;

    uint8_t sin_wav[EXAMPLE_ARRAY_LEN]; // Used to store sine wave values
    uint8_t cos_wav[EXAMPLE_ARRAY_LEN]; // Used to store cosine wave values
    uint8_t squ_wav[EXAMPLE_ARRAY_LEN]; // Used to store square wave values

    dac_continuous_handle_t handle_0 = NULL;
    dac_continuous_handle_t handle_1 = NULL;
};

问题/疑问摘要:

我需要 ESP32 在连续波输出模式(连续/DMA 模式)下工作 DAC 配置的帮助

感谢您的评论和帮助!

c++ esp32 dma arduino-esp32
1个回答
0
投票
最后,经过一些实验,我发现DAC驱动功能

如果在类中就无法工作

并且必须在通道激活后调用函数

dac_continuous_write_cyclically()

ESP_ERROR_CHECK(dac_continuous_new_channels(&cont_cfg_0, &handle_0)); ESP_ERROR_CHECK(dac_continuous_enable(handle_0)); // After ESP_ERROR_CHECK(dac_continuous_new_channels(&cont_cfg_0, &handle_0));
完整的工作代码:

main.cpp
文件:

#include <Arduino.h> #include "DAC_Continuous_DMA.h" void setup() { Serial.begin(921600); dac_continuous_dma_config(1000); } void loop() { // Just for testing Serial.printf("Free heap size: %d\n", esp_get_free_heap_size()); Serial.printf("%s(), core: %d: Running time [s]: %d\n", __func__, xPortGetCoreID(), millis()); vTaskDelay(pdMS_TO_TICKS(1000)); }

DAC_Continuous_DMA.h
文件:

#pragma once #ifndef DAC_Continuous_DMA_h #define DAC_Continuous_DMA_h #include "Arduino.h" #include <math.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "soc/dac_channel.h" #include "driver/dac_continuous.h" #include "soc/sens_reg.h" #define EXAMPLE_ARRAY_LEN 512 // Length of wave array #define EXAMPLE_DAC_AMPLITUDE 255 // Amplitude of DAC voltage. If it's more than 256 will causes #define CONST_2_PI 6.2832 // 2 * PI _Static_assert(EXAMPLE_DAC_AMPLITUDE < 256, "The DAC accuracy is 8 bit-width, doesn't support the amplitude beyond 255"); dac_continuous_config_t cont_cfg; uint8_t signal_wave[EXAMPLE_ARRAY_LEN]; // Used to store sine wave values uint8_t amplitude = EXAMPLE_DAC_AMPLITUDE; dac_continuous_handle_t cont_handle = NULL; void generate_waves(void) { for (int i = 0; i < EXAMPLE_ARRAY_LEN; i++) { signal_wave[i] = (uint8_t)(amplitude / 2 * (1 + sin(2 * M_PI * i / EXAMPLE_ARRAY_LEN))); } } void dac_continuous_dma_config(uint32_t frequency_Hz = 1000) { generate_waves(); cont_cfg = { .chan_mask = DAC_CHANNEL_MASK_ALL, .desc_num = 2, .buf_size = 2048, .freq_hz = EXAMPLE_ARRAY_LEN * frequency_Hz / 2, .offset = 0, .clk_src = DAC_DIGI_CLK_SRC_DEFAULT, // If the frequency is out of range, try 'DAC_DIGI_CLK_SRC_APLL' .chan_mode = DAC_CHANNEL_MODE_ALTER, }; /* Assume the data in buffer is 'A B C D E F' * DAC_CHANNEL_MODE_SIMUL: * - channel 0: A B C D E F * - channel 1: A B C D E F * DAC_CHANNEL_MODE_ALTER: * - channel 0: A C E * - channel 1: B D F */ ESP_ERROR_CHECK(dac_continuous_new_channels(&cont_cfg, &cont_handle)); ESP_ERROR_CHECK(dac_continuous_enable(cont_handle)); ESP_ERROR_CHECK(dac_continuous_write_cyclically(cont_handle, (uint8_t *)signal_wave, EXAMPLE_ARRAY_LEN, NULL)); } #endif // DAC_Continuous_DMA_h
尽管我在 DAC 输出上收到信号(

当前问题已得到解答),但我的主要目标是在 ESP32 的 DAC 通道上生成两个信号,即 sine
cosine
 信号,因此 
shifted by 90 deg
 ,没有实现。

但这又是另一个问题了。如果有人对此感兴趣并且可以帮助我,我创建了新问题

here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.