mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-22 18:38:37 +00:00
adc: rename adc examples according to hw feature and usage
This commit is contained in:
6
examples/peripherals/adc/single_read/adc2/CMakeLists.txt
Normal file
6
examples/peripherals/adc/single_read/adc2/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(adc2)
|
8
examples/peripherals/adc/single_read/adc2/Makefile
Normal file
8
examples/peripherals/adc/single_read/adc2/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := adc2
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
96
examples/peripherals/adc/single_read/adc2/README.md
Normal file
96
examples/peripherals/adc/single_read/adc2/README.md
Normal file
@@ -0,0 +1,96 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
# ADC2 Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
In this example, we use ADC2 to measure the output of DAC.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
#### ESP32 platform
|
||||
|
||||
* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
We use ADC1_CHANNEL_7 (GPIO27) and DAC_CHANNEL_1 (GPIO25) by default, you need to short the two GPIOs (if you have changed the ADC2 and DAC channel, please refer to Chapter 4.11 of the `ESP32 Technical Reference Manual` to get the pin number).
|
||||
|
||||
#### ESP32-S2 platform
|
||||
|
||||
* A development board with ESP32S2 SoC
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
We use ADC1_CHANNEL_7 (GPIO18) and DAC_CHANNEL_1 (GPIO17) by default, you need to short the two GPIOs (if you have changed the ADC2 and DAC channel, please refer to the `ESP32S2 Technical Reference Manual` to get the pin number).
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
* Set ADC2 and DAC channel in "Example Configuration"
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Running this example, you will see the following log output on the serial monitor:
|
||||
|
||||
### ESP32 platform
|
||||
|
||||
```
|
||||
ADC channel 7 @ GPIO 27, DAC channel 1 @ GPIO 25.
|
||||
adc2_init...
|
||||
start conversion.
|
||||
1: 150
|
||||
2: 203
|
||||
3: 250
|
||||
4: 300
|
||||
5: 351
|
||||
6: 400
|
||||
7: 441
|
||||
8: 491
|
||||
9: 547
|
||||
10: 595
|
||||
...
|
||||
```
|
||||
|
||||
#### ESP32-S2 platform
|
||||
|
||||
```
|
||||
ADC channel 7 @ GPIO 18, DAC channel 1 @ GPIO 17.
|
||||
adc2_init...
|
||||
start conversion.
|
||||
1: 150
|
||||
2: 203
|
||||
3: 250
|
||||
4: 300
|
||||
5: 351
|
||||
6: 400
|
||||
7: 441
|
||||
8: 491
|
||||
9: 547
|
||||
10: 595
|
||||
...
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* program upload failure
|
||||
|
||||
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "adc2_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
105
examples/peripherals/adc/single_read/adc2/main/Kconfig.projbuild
Normal file
105
examples/peripherals/adc/single_read/adc2/main/Kconfig.projbuild
Normal file
@@ -0,0 +1,105 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
choice EXAMPLE_ADC2_CHANNEL
|
||||
bool "ADC2 Channel Num"
|
||||
depends on IDF_TARGET_ESP32
|
||||
default EXAMPLE_ADC2_CHANNEL_7
|
||||
help
|
||||
The channel of ADC2 used in this example.
|
||||
|
||||
config EXAMPLE_ADC2_CHANNEL_0
|
||||
bool "ADC2 Channel 0 (GPIO 4)"
|
||||
config EXAMPLE_ADC2_CHANNEL_1
|
||||
bool "ADC2 Channel 1 (GPIO 0)"
|
||||
config EXAMPLE_ADC2_CHANNEL_2
|
||||
bool "ADC2 Channel 2 (GPIO 2)"
|
||||
config EXAMPLE_ADC2_CHANNEL_3
|
||||
bool "ADC2 Channel 3 (GPIO 15)"
|
||||
config EXAMPLE_ADC2_CHANNEL_4
|
||||
bool "ADC2 Channel 4 (GPIO 13)"
|
||||
config EXAMPLE_ADC2_CHANNEL_5
|
||||
bool "ADC2 Channel 5 (GPIO 12)"
|
||||
config EXAMPLE_ADC2_CHANNEL_6
|
||||
bool "ADC2 Channel 6 (GPIO 14)"
|
||||
config EXAMPLE_ADC2_CHANNEL_7
|
||||
bool "ADC2 Channel 7 (GPIO 27)"
|
||||
config EXAMPLE_ADC2_CHANNEL_8
|
||||
bool "ADC2 Channel 8 (GPIO 25)"
|
||||
config EXAMPLE_ADC2_CHANNEL_9
|
||||
bool "ADC2 Channel 9 (GPIO 26)"
|
||||
endchoice
|
||||
|
||||
choice EXAMPLE_ADC2_CHANNEL
|
||||
bool "ADC2 Channel Num"
|
||||
depends on IDF_TARGET_ESP32S2
|
||||
default EXAMPLE_ADC2_CHANNEL_7
|
||||
help
|
||||
The channel of ADC2 used in this example.
|
||||
|
||||
config EXAMPLE_ADC2_CHANNEL_0
|
||||
bool "ADC2 Channel 0 (GPIO 11)"
|
||||
config EXAMPLE_ADC2_CHANNEL_1
|
||||
bool "ADC2 Channel 1 (GPIO 12)"
|
||||
config EXAMPLE_ADC2_CHANNEL_2
|
||||
bool "ADC2 Channel 2 (GPIO 13)"
|
||||
config EXAMPLE_ADC2_CHANNEL_3
|
||||
bool "ADC2 Channel 3 (GPIO 14)"
|
||||
config EXAMPLE_ADC2_CHANNEL_4
|
||||
bool "ADC2 Channel 4 (GPIO 15)"
|
||||
config EXAMPLE_ADC2_CHANNEL_5
|
||||
bool "ADC2 Channel 5 (GPIO 16)"
|
||||
config EXAMPLE_ADC2_CHANNEL_6
|
||||
bool "ADC2 Channel 6 (GPIO 17)"
|
||||
config EXAMPLE_ADC2_CHANNEL_7
|
||||
bool "ADC2 Channel 7 (GPIO 18)"
|
||||
config EXAMPLE_ADC2_CHANNEL_8
|
||||
bool "ADC2 Channel 8 (GPIO 19)"
|
||||
config EXAMPLE_ADC2_CHANNEL_9
|
||||
bool "ADC2 Channel 9 (GPIO 20)"
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_ADC2_CHANNEL
|
||||
int
|
||||
default 0 if EXAMPLE_ADC2_CHANNEL_0
|
||||
default 1 if EXAMPLE_ADC2_CHANNEL_1
|
||||
default 2 if EXAMPLE_ADC2_CHANNEL_2
|
||||
default 3 if EXAMPLE_ADC2_CHANNEL_3
|
||||
default 4 if EXAMPLE_ADC2_CHANNEL_4
|
||||
default 5 if EXAMPLE_ADC2_CHANNEL_5
|
||||
default 6 if EXAMPLE_ADC2_CHANNEL_6
|
||||
default 7 if EXAMPLE_ADC2_CHANNEL_7
|
||||
default 8 if EXAMPLE_ADC2_CHANNEL_8
|
||||
default 9 if EXAMPLE_ADC2_CHANNEL_9
|
||||
|
||||
choice EXAMPLE_DAC_CHANNEL
|
||||
bool "DAC Channel Num"
|
||||
depends on IDF_TARGET_ESP32
|
||||
default EXAMPLE_DAC_CHANNEL_1
|
||||
help
|
||||
The channel of DAC used in this example.
|
||||
|
||||
config EXAMPLE_DAC_CHANNEL_1
|
||||
bool "DAC Channel 1 (GPIO25)"
|
||||
config EXAMPLE_DAC_CHANNEL_2
|
||||
bool "DAC Channel 2 (GPIO26)"
|
||||
endchoice
|
||||
|
||||
choice EXAMPLE_DAC_CHANNEL
|
||||
bool "DAC Channel Num"
|
||||
depends on IDF_TARGET_ESP32S2
|
||||
default EXAMPLE_DAC_CHANNEL_1
|
||||
help
|
||||
The channel of DAC used in this example.
|
||||
|
||||
config EXAMPLE_DAC_CHANNEL_1
|
||||
bool "DAC Channel 1 (GPIO17)"
|
||||
config EXAMPLE_DAC_CHANNEL_2
|
||||
bool "DAC Channel 2 (GPIO18)"
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_DAC_CHANNEL
|
||||
int
|
||||
default 0 if EXAMPLE_DAC_CHANNEL_1
|
||||
default 1 if EXAMPLE_DAC_CHANNEL_2
|
||||
|
||||
endmenu
|
@@ -0,0 +1,69 @@
|
||||
/* ADC2 Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#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"
|
||||
|
||||
#define DAC_EXAMPLE_CHANNEL CONFIG_EXAMPLE_DAC_CHANNEL
|
||||
#define ADC2_EXAMPLE_CHANNEL CONFIG_EXAMPLE_ADC2_CHANNEL
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
|
||||
#endif
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
uint8_t output_data=0;
|
||||
int read_raw;
|
||||
esp_err_t r;
|
||||
|
||||
gpio_num_t adc_gpio_num, dac_gpio_num;
|
||||
|
||||
r = adc2_pad_get_io_num( ADC2_EXAMPLE_CHANNEL, &adc_gpio_num );
|
||||
assert( r == ESP_OK );
|
||||
r = dac_pad_get_io_num( DAC_EXAMPLE_CHANNEL, &dac_gpio_num );
|
||||
assert( r == ESP_OK );
|
||||
|
||||
printf("ADC2 channel %d @ GPIO %d, DAC channel %d @ GPIO %d.\n", ADC2_EXAMPLE_CHANNEL, adc_gpio_num,
|
||||
DAC_EXAMPLE_CHANNEL + 1, dac_gpio_num );
|
||||
|
||||
dac_output_enable( DAC_EXAMPLE_CHANNEL );
|
||||
|
||||
//be sure to do the init before using adc2.
|
||||
printf("adc2_init...\n");
|
||||
adc2_config_channel_atten( ADC2_EXAMPLE_CHANNEL, ADC_ATTEN_11db );
|
||||
|
||||
vTaskDelay(2 * portTICK_PERIOD_MS);
|
||||
|
||||
printf("start conversion.\n");
|
||||
while(1) {
|
||||
dac_output_voltage( DAC_EXAMPLE_CHANNEL, output_data++ );
|
||||
r = adc2_get_raw( ADC2_EXAMPLE_CHANNEL, width, &read_raw);
|
||||
if ( r == ESP_OK ) {
|
||||
printf("%d: %d\n", output_data, read_raw );
|
||||
} else if ( r == ESP_ERR_INVALID_STATE ) {
|
||||
printf("%s: ADC2 not initialized yet.\n", esp_err_to_name(r));
|
||||
} else if ( r == ESP_ERR_TIMEOUT ) {
|
||||
//This can not happen in this example. But if WiFi is in use, such error code could be returned.
|
||||
printf("%s: ADC2 is in use by Wi-Fi.\n", esp_err_to_name(r));
|
||||
} else {
|
||||
printf("%s\n", esp_err_to_name(r));
|
||||
}
|
||||
|
||||
vTaskDelay( 2 * portTICK_PERIOD_MS );
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
Reference in New Issue
Block a user