Skip to content

analogRead() return value according to value set in analogReadResolution() #5776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions cores/esp32/esp32-hal-adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ static uint8_t __analogVRefPin = 0;
#endif

static uint8_t __analogAttenuation = 3;//11db
#if CONFIG_IDF_TARGET_ESP32S2
static uint8_t __analogWidth = 4; // 13 bits
#else
static uint8_t __analogWidth = 3; // 12 bits
#endif
static uint8_t __analogWidth = ADC_WIDTH_MAX - 1; //3 for ESP32/ESP32C3; 4 for ESP32S2
static uint8_t __analogReturnedWidth = SOC_ADC_MAX_BITWIDTH; //12 for ESP32/ESP32C3; 13 for ESP32S2
static uint8_t __analogClockDiv = 1;
static adc_attenuation_t __pin_attenuation[SOC_GPIO_PIN_COUNT];

static inline uint16_t mapResolution(uint16_t value)
{
uint8_t from = __analogWidth + 9;
if (from == __analogReturnedWidth) {
return value;
}
if (from > __analogReturnedWidth) {
return value >> (from - __analogReturnedWidth);
}
return value << (__analogReturnedWidth - from);
}

void __analogSetClockDiv(uint8_t clockDiv){
if(!clockDiv){
clockDiv = 1;
Expand Down Expand Up @@ -150,6 +159,7 @@ void __analogReadResolution(uint8_t bits)
if(!bits || bits > 16){
return;
}
__analogReturnedWidth = bits;
#if CONFIG_IDF_TARGET_ESP32
__analogSetWidth(bits); // hadware from 9 to 12
#endif
Expand All @@ -169,7 +179,7 @@ uint16_t __analogRead(uint8_t pin)
channel -= 10;
r = adc2_get_raw( channel, __analogWidth, &value);
if ( r == ESP_OK ) {
return value;
return mapResolution(value);
} else if ( r == ESP_ERR_INVALID_STATE ) {
log_e("GPIO%u: %s: ADC2 not initialized yet.", pin, esp_err_to_name(r));
} else if ( r == ESP_ERR_TIMEOUT ) {
Expand All @@ -178,9 +188,10 @@ uint16_t __analogRead(uint8_t pin)
log_e("GPIO%u: %s", pin, esp_err_to_name(r));
}
} else {
return adc1_get_raw(channel);
value = adc1_get_raw(channel);
return mapResolution(value);
}
return value;
return mapResolution(value);
}

uint32_t __analogReadMilliVolts(uint8_t pin){
Expand Down