Background:
With a PIC18F25K22 the 10-Bit ADC Result has a positive offset of 2, even when the ANx PIN is tied directly to the PIC Ground pin. With a quality 10K pot the result swings from 2 to 1023. It should swing from 0 to 1023. I am not sure if this is a problem with GCB "ReadAD10" or something else.
How to do it:
In any case, to remove this "offset" I used the following formula borrowed from the Arduino "MAP" command.
Removing Low Range offset from READAD10 Result
Background:
With a PIC18F25K22 the 10-Bit ADC Result has a positive offset of 2, even when the ANx PIN is tied directly to the PIC Ground pin. With a quality 10K pot the result swings from 2 to 1023. It should swing from 0 to 1023. I am not sure if this is a problem with GCB "ReadAD10" or something else.
How to do it:
In any case, to remove this "offset" I used the following formula borrowed from the Arduino "MAP" command.
return (x - inmin) * (outmax - outmin) / (inmax - inmin) + outmin
With the values plugged in it becomes:
adc_val = (adc_val - 2) * (1023 - 0) / (1023 - 2) + 0
Simplified, it becomes:
adc_val = (adc_val -2) * 1023 / 1021
Example Code (Tested to Work):
Last edit: William Roth 2015-12-13
Very useful stuff. Thanks !