Changed from ADC_WIDTH_BIT_12 to ADC_WIDTH_BIT_DEFAULT

This commit is contained in:
nopnop2002 2022-05-01 20:14:37 +09:00
parent e03749df3f
commit 8e912d376a
2 changed files with 13 additions and 10 deletions

View File

@ -15,10 +15,10 @@ You will need an analog source to run this demo.
Connect the analog source to the following GPIO.
These are all channel 0 of ADC1.
- Analog input gpio for ESP32 is GPIO36.
- Analog input gpio for ESP32S2 is GPIO01.
- Analog input gpio for ESP32S3 is GPIO01.
- Analog input gpio for ESP32C3 is GPIO00.
- Analog input gpio for ESP32 is GPIO36. 12Bits width.
- Analog input gpio for ESP32S2 is GPIO01. 13Bits width.
- Analog input gpio for ESP32S3 is GPIO01. 12Bits width.
- Analog input gpio for ESP32C3 is GPIO00. 12Bits width.
Schematic I used:
```

View File

@ -143,9 +143,7 @@ void app_main(void)
{
adc_calibration_init();
// ADC1 config
//ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));
//ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_10));
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));
ESP_ERROR_CHECK(adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_EXAMPLE_ATTEN));
SSD1306_t dev;
@ -209,8 +207,13 @@ void app_main(void)
while(1) {
//ssd1306_clear_screen(&dev, false);
int sample = adc1_get_raw(ADC1_CHANNEL_0); // 12 bits sampling
sample = sample / 4; // 12 bits -> 10 bits. Because the original code is for ATMEGA328.
int sample = adc1_get_raw(ADC1_CHANNEL_0);
if (ADC_WIDTH_BIT_DEFAULT == 3) {
sample = sample / 4; // 12 bits -> 10 bits. Because the original code is for ATMEGA328.
}
if (ADC_WIDTH_BIT_DEFAULT == 4) {
sample = sample / 8; // 13 bits -> 10 bits. Because the original code is for ATMEGA328.
}
ESP_LOGD(TAG, "sample=%d", sample);
float MeterValue = sample * 120.079 / 1023;
MeterValue = MeterValue - 60.039;
@ -225,7 +228,7 @@ void app_main(void)
// Display the entire image
ssd1306_show_buffer(&dev);
vTaskDelay(100 / portTICK_PERIOD_MS);
vTaskDelay(1);
// Erase needle
_ssd1306_line(&dev, a1, a2, hMeter, vMeter, true);