无法将原始ADC1读数转换为电压

时间:2019-04-12 14:23:16

标签: esp32 esp-idf

我不明白为什么将ESP-IDF电压计算应用于从GPIO39转换原始读数会导致崩溃?

uint32_t电压= esp_adc_cal_raw_to_voltage(read_raw,adc_chars);

下面是简单的源代码

 #include <stdio.h>
    #include <stdlib.h>
    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "freertos/queue.h"
    #include "driver/gpio.h"
    #include "driver/adc.h"
    #include "driver/dac.h"
    #include "esp_system.h"
    #include "esp_adc_cal.h"
    #define BLINK_GPIO 2
    static const adc_atten_t atten = ADC_ATTEN_DB_11;
    static const adc_unit_t unit = ADC_UNIT_1;
    #define DEFAULT_VREF    1100
    static void Read_GPIO39() {
        int read_raw = 0;
        adc1_config_width(ADC_WIDTH_BIT_12);
        adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
        read_raw=adc1_get_raw(ADC1_CHANNEL_3);
       adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));


esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF,adc_chars);
       **//uint32_t voltage = esp_adc_cal_raw_to_voltage(read_raw, adc_chars);**
        printf("Intensity : %d\n", read_raw);

        //printf("Voltage: %d\n", voltage);
        gpio_pad_select_gpio(BLINK_GPIO);
        gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
        gpio_set_level(BLINK_GPIO, 1);
    }


    void app_main() {
        ESP_ERROR_CHECK(nvs_flash_init());
        // initialise_wifi();
        while (1) {
            Read_GPIO39();

            vTaskDelay(3000 / portTICK_PERIOD_MS);
            gpio_set_level(BLINK_GPIO, 0);
        }


    }

1 个答案:

答案 0 :(得分:0)

使用以下两个语句,电压读数可能会与实际电压读数出现+/- 7%的偏差,原始读数最大值= 4095或2 ^ 12位〜= 3.3v。该范围似乎在该区域已经过其他人测试和记录。

static const adc_atten_t atten = ADC_ATTEN_DB_11;
    static const adc_unit_t unit = ADC_UNIT_1;
    #define DEFAULT_VREF    1100
 adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));

 esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF,adc_chars);