0

我不明白为什么 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);
        }


    }
4

1 回答 1

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);
于 2019-04-13T18:03:07.950 回答