【问题标题】:Sampling analog signals on the ESP32 at different rates using I2S使用 I2S 以不同速率对 ESP32 上的模拟信号进行采样
【发布时间】:2021-01-14 19:21:41
【问题描述】:

我需要阅读高频。来自一个 ADC1 通道的模拟信号数据并读取低频。来自其他 ADC1 引脚的数据。
我使用 I2S 的高频。数据读取,运行完美,但一旦配置 I2S,所有其他 ADC1 引脚仅读取 4095。
我的要求的正确处理方式是什么?
由于wifi,无法使用ADC2。

代码摘录:

  void readerTask(void *param) {

    size_t bytesRead = 0;
    while(true) {
      // Get ADC data from DMA buffer
      i2s_read(I2S_NUM_0, buf, sizeof(buf), &bytesRead, portMAX_DELAY);

      // prevent the data getting corrupted
      i2s_adc_disable(I2S_NUM_0);
    
      /* data processing */

      delay(30);
      i2s_adc_enable(I2S_NUM_0);
    }
  }

  void setup() {
    i2s_config_t i2s_config = {
        .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
        .sample_rate = 8000,
        .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
        .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
        .communication_format = I2S_COMM_FORMAT_I2S_LSB,
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
        .dma_buf_count = 2,
        .dma_buf_len = 1024,
        .use_apll = false,
        .tx_desc_auto_clear = false,
        .fixed_mclk = 0};
    
    //install and start i2s driver
    i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
    
    //init ADC pad
    i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
    
    // enable the ADC
    i2s_adc_enable(I2S_NUM_0);
    
    // start a task to read samples from I2S
    xTaskCreatePinnedToCore(readerTask, "Reader Task", 2048, NULL, 1, NULL, 0);
  }

  void loop()
  {
    EVERY_N_MILLISECONDS( 100 ) { 
      // read low freq. data
      int test = analogRead(34);   // will always read 4095
    }
  }

【问题讨论】:

    标签: sampling esp32 adc i2s


    【解决方案1】:

    你需要:

      i2s_adc_disable(I2S_NUM_0);
      i2s_driver_uninstall(I2S_NUM_0);
      analogRead(34);
      i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
      i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0);
      i2s_adc_enable(I2S_NUM_0);
    

    【讨论】:

    • 禁用/卸载 i2s 不会与 i2s 阅读器任务冲突吗?这些是 2 个并行线程,所以我担心在并行运行的线程中禁用/卸载 i2s 时 i2s 阅读器任务会受到损害。
    • 在所有任务中禁用/卸载 i2s。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多