adc1_example_main.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
  18. static const adc_atten_t atten = ADC_ATTEN_DB_0;
  19. static const adc_unit_t unit = ADC_UNIT_1;
  20. static void check_efuse()
  21. {
  22. //Check TP is burned into eFuse
  23. if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
  24. printf("eFuse Two Point: Supported\n");
  25. } else {
  26. printf("eFuse Two Point: NOT supported\n");
  27. }
  28. //Check Vref is burned into eFuse
  29. if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
  30. printf("eFuse Vref: Supported\n");
  31. } else {
  32. printf("eFuse Vref: NOT supported\n");
  33. }
  34. }
  35. static void print_char_val_type(esp_adc_cal_value_t val_type)
  36. {
  37. if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
  38. printf("Characterized using Two Point Value\n");
  39. } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
  40. printf("Characterized using eFuse Vref\n");
  41. } else {
  42. printf("Characterized using Default Vref\n");
  43. }
  44. }
  45. void app_main()
  46. {
  47. //Check if Two Point or Vref are burned into eFuse
  48. check_efuse();
  49. //Configure ADC
  50. if (unit == ADC_UNIT_1) {
  51. adc1_config_width(ADC_WIDTH_BIT_12);
  52. adc1_config_channel_atten(channel, atten);
  53. } else {
  54. adc2_config_channel_atten((adc2_channel_t)channel, atten);
  55. }
  56. //Characterize ADC
  57. adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
  58. esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
  59. print_char_val_type(val_type);
  60. //Continuously sample ADC1
  61. while (1) {
  62. uint32_t adc_reading = 0;
  63. //Multisampling
  64. for (int i = 0; i < NO_OF_SAMPLES; i++) {
  65. if (unit == ADC_UNIT_1) {
  66. adc_reading += adc1_get_raw((adc1_channel_t)channel);
  67. } else {
  68. int raw;
  69. adc2_get_raw((adc2_channel_t)channel, ADC_WIDTH_BIT_12, &raw);
  70. adc_reading += raw;
  71. }
  72. }
  73. adc_reading /= NO_OF_SAMPLES;
  74. //Convert adc_reading to voltage in mV
  75. uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
  76. printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
  77. vTaskDelay(pdMS_TO_TICKS(1000));
  78. }
  79. }