adc1_example_main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ADC1 Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "driver/gpio.h"
  12. #include "driver/adc.h"
  13. #include "esp_adc_cal.h"
  14. #define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
  15. #define NO_OF_SAMPLES 64 //Multisampling
  16. static esp_adc_cal_characteristics_t *adc_chars;
  17. #if CONFIG_IDF_TARGET_ESP32
  18. static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
  19. static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. static const adc_channel_t channel = ADC_CHANNEL_6; // GPIO7 if ADC1, GPIO17 if ADC2
  22. static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
  23. #endif
  24. static const adc_atten_t atten = ADC_ATTEN_DB_0;
  25. static const adc_unit_t unit = ADC_UNIT_1;
  26. static void check_efuse(void)
  27. {
  28. #if CONFIG_IDF_TARGET_ESP32
  29. //Check if TP is burned into eFuse
  30. if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
  31. printf("eFuse Two Point: Supported\n");
  32. } else {
  33. printf("eFuse Two Point: NOT supported\n");
  34. }
  35. //Check Vref is burned into eFuse
  36. if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
  37. printf("eFuse Vref: Supported\n");
  38. } else {
  39. printf("eFuse Vref: NOT supported\n");
  40. }
  41. #elif CONFIG_IDF_TARGET_ESP32S2
  42. if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
  43. printf("eFuse Two Point: Supported\n");
  44. } else {
  45. printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n");
  46. }
  47. #else
  48. #error "This example is configured for ESP32/ESP32S2."
  49. #endif
  50. }
  51. static void print_char_val_type(esp_adc_cal_value_t val_type)
  52. {
  53. if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
  54. printf("Characterized using Two Point Value\n");
  55. } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
  56. printf("Characterized using eFuse Vref\n");
  57. } else {
  58. printf("Characterized using Default Vref\n");
  59. }
  60. }
  61. void app_main(void)
  62. {
  63. //Check if Two Point or Vref are burned into eFuse
  64. check_efuse();
  65. //Configure ADC
  66. if (unit == ADC_UNIT_1) {
  67. adc1_config_width(width);
  68. adc1_config_channel_atten(channel, atten);
  69. } else {
  70. adc2_config_channel_atten((adc2_channel_t)channel, atten);
  71. }
  72. //Characterize ADC
  73. adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
  74. esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
  75. print_char_val_type(val_type);
  76. //Continuously sample ADC1
  77. while (1) {
  78. uint32_t adc_reading = 0;
  79. //Multisampling
  80. for (int i = 0; i < NO_OF_SAMPLES; i++) {
  81. if (unit == ADC_UNIT_1) {
  82. adc_reading += adc1_get_raw((adc1_channel_t)channel);
  83. } else {
  84. int raw;
  85. adc2_get_raw((adc2_channel_t)channel, width, &raw);
  86. adc_reading += raw;
  87. }
  88. }
  89. adc_reading /= NO_OF_SAMPLES;
  90. //Convert adc_reading to voltage in mV
  91. uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
  92. printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
  93. vTaskDelay(pdMS_TO_TICKS(1000));
  94. }
  95. }