ESP32 - ESP-IDF - 扫频 DAC

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

我正在尝试使用 ESP32 的 DAC 进行某种频率扫描。

目前我不太确定如何在不先停止 DAC 的情况下更改频率。而且停止 DAC 也会引发一个问题,因为我经常遇到 DAC 在更改和重新启动之前没有停止的问题,就好像停止 DAC 在后台运行一样,但 ESP 已经继续进行下一个操作,同时停止 DAC还没结束

在不停止 DAC 的情况下改变频率的好方法是什么?或者没有吗?有替代方法吗?

#include <math.h>
#include <rom/ets_sys.h>
#include <esp_task_wdt.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/dac_cosine.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_check.h"


dac_cosine_handle_t chan0_handle;

dac_cosine_config_t cos0_cfg = {
    .chan_id = DAC_CHAN_0,                  //  The channel determines the output pin (GPIO 25 OR 17 for channel 0)
    .freq_hz = 1000,                        //  The frequency
    .clk_src = DAC_COSINE_CLK_SRC_DEFAULT,  //  The clock source, only RTC_FAST can be used which is default
    .offset = 0,                            //  DC Offset -128 to 127 (int 8)
    .phase = DAC_COSINE_PHASE_0,            //  Phase of the waveform
    .atten = DAC_COSINE_ATTEN_DEFAULT,      //  attenuation, default = max 3v3pp
    .flags.force_set_freq = false,          
};

void set_frequency(uint32_t frequency) {
    cos0_cfg.freq_hz = frequency;

    ESP_ERROR_CHECK(dac_cosine_stop(chan0_handle));
    ESP_ERROR_CHECK(dac_cosine_del_channel(&chan0_handle));
    ESP_ERROR_CHECK(dac_cosine_new_channel(&cos0_cfg, &chan0_handle));
    ESP_ERROR_CHECK(dac_cosine_start(chan0_handle));
}

void sweep() {
    int freq = 1000;
    while(true) {
        set_frequency(freq);
        freq += 1000;
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        if(freq > 10000) {
            freq = 1000;
        }
    }
    
}

void app_main(void)
{
    ESP_ERROR_CHECK(dac_cosine_new_channel(&cos0_cfg, &chan0_handle));
    ESP_ERROR_CHECK(dac_cosine_start(chan0_handle));

    xTaskCreate(sweep, "sweeping", 4096, NULL, 5, NULL);
}
c++ frequency esp32 esp-idf modulation
© www.soinside.com 2019 - 2024. All rights reserved.