-
Notifications
You must be signed in to change notification settings - Fork 7.6k
AnalogReadResolution and AnalogRead not handling low and high resolutions correctly #5163
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
Comments
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
nudge so stale bot does not close this |
[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future. |
bump, not stale |
me-no-dev
pushed a commit
that referenced
this issue
Oct 24, 2021
…ulotion() (#5776) Function analogReadResolution set how many bits will analogRead return. Find out that this functionality was added back 2017 by @me-no-dev in #161. Related issues: #5163
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hardware:
Board: Adafruit ESP32 Feather
Core Installation version: 1.0.6
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Fedora 33 5.11.17-200.fc33.x86_64
Description:
AnalogReadResolution was implemented, but testing shows that the full range was not. The code comments say the range is 1 to 16.
arduino-esp32/cores/esp32/esp32-hal-adc.h
Lines 41 to 48 in eb46978
However, only bit counts in the range of 9 to 12 give the expected results. Values less than 9 act as if 9 was set, and values greater than 12 act as if 12 was set. What is supposed to happen, is that values between 1 and 32 should work. Values outside of the 9 to 12 directly supported by the hardware should cause
analogRead()
results to be "shifted" left or right to match the range for the specified number of bits. So a full range value would return 1 for a resolution of 1 bit, and a little less than 2 ** 32 for 32 bits. The "little less" because the shifting zero fills. With the 12 bits available from the hardware adc, with a real value of 0xfff, being turned into 0xfff00000. Implementing that is also going to need analogRead() to return auint32_t
, instead auint16_t
. Without that, the resolution could still be implement for up to 16 bits.It is not sufficent to "force" the resolution bits into the 9 to 12 range. The actual specified resolution needs to be use by
analogRead()
(all return paths) to shift the hardware result into the correct range.arduino-esp32/cores/esp32/esp32-hal-adc.c
Lines 136 to 144 in 7856de7
arduino-esp32/cores/esp32/esp32-hal-adc.c
Lines 67 to 77 in 7856de7
The logic needed is similar to how the debug sketch generates the expected values.
return value >> (9 - bits);
return value << (bits - 12);
return value';
where the bits value needs to be carried over from when
analogReadResolution()
was called, or defaulted (I assume) to 12. Not the adjusted value calculated in__analogSetWidth()
Sketch: esp32_analog_resolution
Debug Messages:
output from test sketch
The text was updated successfully, but these errors were encountered: