adc1_example_main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #if CONFIG_IDF_TARGET_ESP32
  14. #include "esp_adc_cal.h"
  15. #endif
  16. #define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
  17. #define NO_OF_SAMPLES 64 //Multisampling
  18. #if CONFIG_IDF_TARGET_ESP32
  19. static esp_adc_cal_characteristics_t *adc_chars;
  20. static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
  21. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  22. static const adc_channel_t channel = ADC_CHANNEL_6; // GPIO7 if ADC1, GPIO17 if ADC2
  23. #endif
  24. static const adc_atten_t atten = ADC_ATTEN_DB_0;
  25. static const adc_unit_t unit = ADC_UNIT_1;
  26. #if CONFIG_IDF_TARGET_ESP32
  27. static void check_efuse(void)
  28. {
  29. //Check 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. }
  42. static void print_char_val_type(esp_adc_cal_value_t val_type)
  43. {
  44. if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
  45. printf("Characterized using Two Point Value\n");
  46. } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
  47. printf("Characterized using eFuse Vref\n");
  48. } else {
  49. printf("Characterized using Default Vref\n");
  50. }
  51. }
  52. #endif
  53. void app_main(void)
  54. {
  55. #if CONFIG_IDF_TARGET_ESP32
  56. //Check if Two Point or Vref are burned into eFuse
  57. check_efuse();
  58. #endif
  59. //Configure ADC
  60. if (unit == ADC_UNIT_1) {
  61. adc1_config_width(ADC_WIDTH_BIT_12);
  62. adc1_config_channel_atten(channel, atten);
  63. } else {
  64. adc2_config_channel_atten((adc2_channel_t)channel, atten);
  65. }
  66. #if CONFIG_IDF_TARGET_ESP32
  67. //Characterize ADC
  68. adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
  69. esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
  70. print_char_val_type(val_type);
  71. #endif
  72. //Continuously sample ADC1
  73. while (1) {
  74. uint32_t adc_reading = 0;
  75. //Multisampling
  76. for (int i = 0; i < NO_OF_SAMPLES; i++) {
  77. if (unit == ADC_UNIT_1) {
  78. adc_reading += adc1_get_raw((adc1_channel_t)channel);
  79. } else {
  80. int raw;
  81. adc2_get_raw((adc2_channel_t)channel, ADC_WIDTH_BIT_12, &raw);
  82. adc_reading += raw;
  83. }
  84. }
  85. adc_reading /= NO_OF_SAMPLES;
  86. #if CONFIG_IDF_TARGET_ESP32
  87. //Convert adc_reading to voltage in mV
  88. uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
  89. printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
  90. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  91. printf("ADC%d CH%d Raw: %d\t\n", unit, channel, adc_reading);
  92. #endif
  93. vTaskDelay(pdMS_TO_TICKS(1000));
  94. }
  95. }