ESP32 WROVER IDF 使用i2s_adc读取电压时,打印数据都是0
Posted: Wed Dec 08, 2021 1:22 am
大家好:
目前我在使用ESP32-WROVER 参考官方例程I2S_ADC做数据采集时一直读取不到数据,打印的数据都是0,配置如下:
void example_i2s_init(void)
{
i2s_config_t i2s_config;
i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN;
i2s_config.sample_rate = I2S_SAMPLE_RATE;
i2s_config.dma_buf_len = I2S_BUFFER;
i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
//i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
i2s_config.use_apll = false;
i2s_config.communication_format = I2S_COMM_FORMAT_PCM;
i2s_config.intr_alloc_flags = 0;
i2s_config.dma_buf_count = 5;
ESP_ERROR_CHECK( adc_gpio_init(ADC_UNIT_1, ADC_CHANNEL_0) );
ESP_ERROR_CHECK( i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) );
ESP_ERROR_CHECK( i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0) );
}
void sample_i2s()
{
int i2s_read_len = I2S_BUFFER;
char* i2s_buff = (char*) calloc(i2s_read_len, sizeof(char));
size_t bytes_read = 0;
//ESP_ERROR_CHECK( i2s_read(I2S_NUM_0, (void*) i2s_buff, i2s_read_len, &bytes_read, portMAX_DELAY) );
int error=i2s_read(I2S_NUM_0, (void*) i2s_buff, i2s_read_len, &bytes_read, portMAX_DELAY);
printf("error=%d \r\n",error); //error 值为0,说明i2s_read读取成功
for(int i=0;i<i2s_read_len;i++)
{
printf("%2x ",i2s_buff); //打印的数据一直为0
}
while(1);
}
打印信息如下:
[0;32mI (308) I2S: DMA Malloc info, datalen=blocksize=2048, dma_buf_count=5[0m
[0;32mI (318) I2S: PLL_D2: Req RATE: 44100, real rate: 2777.000, BITS: 16, CLKM: 30, BCK: 60, MCLK: 30.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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
用ADC单次采集可以采集到电压值
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_TEST_CHANNEL, ADC_ATTEN_11db);
esp_adc_cal_characteristics_t characteristics;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, V_REF, &characteristics);
while(1) {
uint32_t voltage;
esp_adc_cal_get_voltage(ADC1_TEST_CHANNEL, &characteristics, &voltage);
ESP_LOGI(TAG, "%d mV", voltage);
vTaskDelay(200 / portTICK_RATE_MS);
}
请问:用I2S_ADC做数据的时候怎么样设置才能正确的读取到电压值?
目前我在使用ESP32-WROVER 参考官方例程I2S_ADC做数据采集时一直读取不到数据,打印的数据都是0,配置如下:
void example_i2s_init(void)
{
i2s_config_t i2s_config;
i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN;
i2s_config.sample_rate = I2S_SAMPLE_RATE;
i2s_config.dma_buf_len = I2S_BUFFER;
i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
//i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
i2s_config.use_apll = false;
i2s_config.communication_format = I2S_COMM_FORMAT_PCM;
i2s_config.intr_alloc_flags = 0;
i2s_config.dma_buf_count = 5;
ESP_ERROR_CHECK( adc_gpio_init(ADC_UNIT_1, ADC_CHANNEL_0) );
ESP_ERROR_CHECK( i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL) );
ESP_ERROR_CHECK( i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_0) );
}
void sample_i2s()
{
int i2s_read_len = I2S_BUFFER;
char* i2s_buff = (char*) calloc(i2s_read_len, sizeof(char));
size_t bytes_read = 0;
//ESP_ERROR_CHECK( i2s_read(I2S_NUM_0, (void*) i2s_buff, i2s_read_len, &bytes_read, portMAX_DELAY) );
int error=i2s_read(I2S_NUM_0, (void*) i2s_buff, i2s_read_len, &bytes_read, portMAX_DELAY);
printf("error=%d \r\n",error); //error 值为0,说明i2s_read读取成功
for(int i=0;i<i2s_read_len;i++)
{
printf("%2x ",i2s_buff); //打印的数据一直为0
}
while(1);
}
打印信息如下:
[0;32mI (308) I2S: DMA Malloc info, datalen=blocksize=2048, dma_buf_count=5[0m
[0;32mI (318) I2S: PLL_D2: Req RATE: 44100, real rate: 2777.000, BITS: 16, CLKM: 30, BCK: 60, MCLK: 30.234, SCLK: 88864.000000, diva: 64, divb: 14[0m
[0;32mI (328) I2S: PLL_D2: Req RATE: 44100, real rate: 2777.000, BITS: 16, CLKM: 30, BCK: 60, MCLK: 30.234, SCLK: 88864.000000, diva: 64, divb: 14[0m
[0;32mI (338) Scope1:: Task called.[0m
error=0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
用ADC单次采集可以采集到电压值
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_TEST_CHANNEL, ADC_ATTEN_11db);
esp_adc_cal_characteristics_t characteristics;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, V_REF, &characteristics);
while(1) {
uint32_t voltage;
esp_adc_cal_get_voltage(ADC1_TEST_CHANNEL, &characteristics, &voltage);
ESP_LOGI(TAG, "%d mV", voltage);
vTaskDelay(200 / portTICK_RATE_MS);
}
请问:用I2S_ADC做数据的时候怎么样设置才能正确的读取到电压值?