Fix ADC prescaler reg config, add prescaler options #142
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So, during my tests I noticed that my ADC readings were a bit off. Basically on a voltage of around 3.27v where I would expect the ADC value to be close to the maximum of 10-bit range I was instead reading values slightly above 900.
So for default ADC configuration, the first thing to note is the macro used to set
ADC_CONFIG_INPSELwas assigning aADC_CONFIG_RESvalue instead of one of the INPSEL ones. This value, happended to be the same as having 1/3 prescaler on the input signal (so in this case my 3.27v input would be scaled to 1/3 of its value before reaching the ADC comparator). TheADC_CONFIG_REFSELvalue was then set to the internal voltage reference, which is 1.2V, so in essence, together with the prescaler the ADC was configured to read voltages from 0 to 3.6V. I guess this is fine once you know it, but maybe it's not an ideal configuration taking in consideration a lot of the boards have been designed for 3.3V general operation.Following one of the PRs for the nRF52 I've added local global to contain a prescaler value to be configured as part of the
analogReference()set-ups. I've fixed some of the invalid comments and added a few more to easily see the values I have set up in this PR, which I do consider be the safest assumption as to what the user would expect, but of course maybe there are better options, so these should be open for discussion.I'd like to also emphasise that this PR, as it currently stands, is not changing the default ADC behaviour, but I do very strongly suggest to change this default configuration from what it is right now (1.2V internal reference, 1/3 input prescaler), to the
AR_SUPPLY_ONE_THIRDconfiguration.This
AR_SUPPLY_ONE_THIRDconfig should set up the reference voltage to be 1/3 VDD and the input voltage to 1/3 as well, therefore having an ADC range from 0 to VDD. In most boards this would be 0 to 3.3V and I do believe that's what most people would expect out of the box.