ADC clock division in RTC mode
Posted: Fri Dec 11, 2020 5:55 pm
Hi, in ADC RTC mode, is it possible to change the ADC speed using adc_set_clk_div() function?
My code:
In the function adc_set_clk_div(), setting the division factor doesn't change the acquisition time.
My code:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "driver/timer.h"
#include "esp_timer.h"
#define DEFAULT_VREF 1060 //Use adc2_vref_to_gpio() to obtain a better estimate
#define NO_OF_SAMPLES 1000 //Multisampling
extern "C" {
void app_main(void);
}
static unsigned long millis() {
return esp_timer_get_time() / 1000;
}
float adc_reading_2, adc_reading_3 = 0;
int read_raw_1, read_raw_2, adc_reading_4, adc_reading_1 = 0;
void ADC_config(void)
{
adc_set_clk_div(2); // ADC clock is divided from the APB Clock
adc1_config_width(ADC_WIDTH_BIT_12); // select bit width for adc1
adc1_config_channel_atten(ADC1_CHANNEL_5, ADC_ATTEN_11db); // select channel (GPIO 33) and attenuation
adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_11db); // select channel (GPIO 32) and attenuation
adc2_config_channel_atten(ADC2_CHANNEL_6, ADC_ATTEN_11db); // GPIO 14
adc2_config_channel_atten(ADC2_CHANNEL_3, ADC_ATTEN_11db); // GPIO 15
}
void adc_task (void *pvParameter)
{
while (1) {
unsigned long t1 = millis();
for (uint32_t i = 0; i < NO_OF_SAMPLES; i++) {
//adc_reading_1 = adc1_get_raw(ADC1_CHANNEL_6);
//adc_reading_2 = adc1_get_raw(ADC1_CHANNEL_5);
adc_reading_3 = adc1_get_raw(ADC1_CHANNEL_4);
//adc_reading_1 = adc2_get_raw(ADC2_CHANNEL_3, ADC_WIDTH_BIT_12, &read_raw_1);
//adc_reading_4 = adc2_get_raw(ADC2_CHANNEL_6, ADC_WIDTH_BIT_12, &read_raw_2);
//adc_reading_4 = adc2_get_raw(ADC2_CHANNEL_3, ADC_WIDTH_BIT_12, &read_raw_2);
}
unsigned long t2 = millis();
unsigned long diff_ms = (t2-t1);
printf("Conversion time: %lums\n", diff_ms);
printf("Mean time for each ADC reading in us: %0.4f\n", static_cast<double>(diff_ms)/NO_OF_SAMPLES*1000);
printf("ADC speed in kSPS: %0.4f\n",static_cast<double>(NO_OF_SAMPLES/diff_ms));
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void app_main(void)
{
esp_timer_init();
ADC_config();
xTaskCreatePinnedToCore(adc_task, "adc_task", 2048, NULL, 5, NULL, tskNO_AFFINITY);
}