static void check_efuse(void) { #if CONFIG_IDF_TARGET_ESP32 //Check if TP is burned into eFuse if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { printf("eFuse Two Point: Supported\n"); } else { printf("eFuse Two Point: NOT supported\n"); } //Check Vref is burned into eFuse if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) { printf("eFuse Vref: Supported\n"); } else { printf("eFuse Vref: NOT supported\n"); } #elif CONFIG_IDF_TARGET_ESP32S2 if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { printf("eFuse Two Point: Supported\n"); } else { printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n"); } #else #error "This example is configured for ESP32/ESP32S2." #endif } static void print_char_val_type(esp_adc_cal_value_t val_type) { if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { printf("Characterized using Two Point Value\n"); } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { printf("Characterized using eFuse Vref\n"); } else { printf("Characterized using Default Vref\n"); } } void app_main(void) { //Check if Two Point or Vref are burned into eFuse check_efuse(); //Configure ADC if (unit == ADC_UNIT_1) { adc1_config_width(width); adc1_config_channel_atten(channel, atten); } else { adc2_config_channel_atten((adc2_channel_t)channel, atten); } //Characterize ADC adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars); print_char_val_type(val_type); //Continuously sample ADC1 while (1) { uint32_t adc_reading = 0; //Multisampling for (int i = 0; i < NO_OF_SAMPLES; i++) { if (unit == ADC_UNIT_1) { adc_reading += adc1_get_raw((adc1_channel_t)channel); } else { int raw; adc2_get_raw((adc2_channel_t)channel, width, &raw); adc_reading += raw; } } adc_reading /= NO_OF_SAMPLES; //Convert adc_reading to voltage in mV uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage); vTaskDelay(pdMS_TO_TICKS(1000)); } }
时间: 2024-04-06 07:33:41 浏览: 175
efuse_ctrl.rar_TSMC efuse_efuse_efuse ctrl_efuse_ctrl
5星 · 资源好评率100%
这段代码是一个ESP-IDF(Espressif IoT Development Framework)的示例代码,用于演示如何使用ESP32的ADC(模数转换器)功能进行模拟电压读取。
代码中的check_efuse()函数用于检查是否已将TP(两点校准)和Vref(参考电压)烧入eFuse(电子熔断器),以确保ADC的精度和稳定性。
print_char_val_type()函数用于打印ADC的校准方式,包括使用Two Point Value(两点校准)、eFuse Vref和默认Vref。
在app_main()函数中,首先调用check_efuse()函数检查eFuse中是否烧入了TP和Vref。然后使用esp_adc_cal_characterize()函数对ADC进行特性化,以计算出ADC的校准参数。最后进入一个循环,连续采样ADC,并将结果转换为电压输出。
这段代码中还使用了ESP-IDF中的一些API函数,包括adc1_config_width()、adc1_config_channel_atten()、adc1_get_raw()、adc2_config_channel_atten()、adc2_get_raw()、esp_adc_cal_raw_to_voltage()等,这些函数用于配置ADC的参数和进行数据的读取和转换。
阅读全文