PIC18F2580 ADC can't convert High Resolution

32 views
Skip to first unread message

flyway38

unread,
Aug 30, 2022, 12:58:49 PM8/30/22
to jallib
Hello,

Cannot make my PIC18F2580 make ADC conversions at 10bits (High Resolution).
It is converting at 8bit (Low Resolution).
Here's my code.
Any help would be great.

-- Measure using high resolution
const bit ADC_HIGH_RESOLUTION = ON
-- Step 1: ADC analog input pin setup
const byte ADC_CHANNEL = 0                       -- port 0 is analog input (default)
const byte ANSEL_ANS0 = TRUE                     -- potmeter connected to pin_AN0
pin_AN0_direction = input
-- Step 2: Set VDD and VSS as Vref
const byte ADCON0_VCFG = FALSE
-- Step 3: Use Frc as ADC clock
const byte ANSEL_ADCS = 0b011
const ADC_RSOURCE = 500                          -- Input Resistance
const ADC_TEMP = 20                              -- PIC Temperature
-- We want the fastest possible ADC timing, so we specify here the
--const ADC_MIN_TAD = 1
const ADC_MAX_TAD = 10
-- Now we can include the library
include adc
-- And initialize the whole with our parameters
adc_init()
--
var byte AdcChnn = 0

Inside Forever Loop

AdcData = adc_read_high_res(AdcChnn)

Using latest (I think) compiler;
jal jalv25r6 (compiled Oct 29 2021)

Thankyou.

Kind regards,
Filipe Santos.

Rob CJ

unread,
Aug 31, 2022, 4:07:20 AM8/31/22
to jal...@googlegroups.com
Hi Filip,

That is strange. There is a sample program for the 18f2580 (not 18f2580) that does both high resolution and low resolution. The sample file is 18f2520_adc_depedent.jal.

As far as I can remember I tested this sample file when I made a modification to the ADC libraries (remove JANSEL).

I think this setting is no longer used: const bit ADC_HIGH_RESOLUTION = ON

If I look at the datasheets both ADC's look the same. I am not sure if I have a 18F2580 to test it, will check later but it should work.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens flyway38 <fsfo...@gmail.com>
Verzonden: dinsdag 30 augustus 2022 18:58
Aan: jallib <jal...@googlegroups.com>
Onderwerp: [jallib] PIC18F2580 ADC can't convert High Resolution
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/beb04e53-0d62-43bc-95dc-9680c4ef175an%40googlegroups.com.

Rob Hamerling

unread,
Aug 31, 2022, 4:21:04 AM8/31/22
to jal...@googlegroups.com


Hello Filipe,


On 30/08/2022 18.58, flyway38 wrote:

var byte AdcChnn = 0

Inside Forever Loop

AdcData = adc_read_high_res(AdcChnn)


adc_read_high_res() returns a word, but your AdcChnn is a byte...


Regards, Rob.


--
Rob Hamerling, Vianen, NL

Rob Hamerling

unread,
Aug 31, 2022, 4:26:23 AM8/31/22
to jal...@googlegroups.com


On 31/08/2022 10.21, Rob Hamerling wrote:
adc_read_high_res() returns a word, but your AdcChnn is a byte...

Sorry, that 'AdcChnn' is a byte isn't the relevant here. Maybe AdcData is a byte, but I cannot find that in the code you showed.

flyway38

unread,
Aug 31, 2022, 8:13:02 AM8/31/22
to jallib
Hey Guys,

My bad. Sorry to make this mistaken post.
10bit ADC is working fine.
Problem is my code. Am trying to smooth the sinal that is read by the ADC.
Its a fast (100KHz) oscillating signal (with vpp around 1V) that am trying to read it like DC... Not easy I know.
My code is currently reading some hundreds times the signal, then finds the median of all readings and then discard the final reading (make it equal to last median value) if it changes by a small divergence (like 2 bits) from last median value.
This is what lead me to think (wrongly) that ADC was converting at 8bit.
Sorry again for posting a wrong question.

But now this raises new question;
How to smooth an oscillating signal and make it DC reading just like a DC-Voltmeter would do?
(Using only code... cannot change my design)
Thank you once again.

Best regards,
Filipe Santos.

vsurducan

unread,
Aug 31, 2022, 11:22:43 AM8/31/22
to jal...@googlegroups.com
Felipe, in my opinion you have two variants:
1. the easiest one: use a zero precision rectifier with operational amplifiers, there are several versions, then your problem will be measuring a DC signal. Beware of using adequate bandwidth OA and use precise resistors for positive and negative slopes ( if your signal is alternately).


2. the complicated one which will not work with jal library: sense the zero cross of the signal (if there is any), add a precise delay until you'll sample the top of the rectangular signal or the Vmax of the sinusoidal shape and sample it. You'll have to process data later. You can not sample, format and display in a loop since the time consumed by last two is much longer than the signal period.

best wishes


--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.

vsurducan

unread,
Aug 31, 2022, 11:26:29 AM8/31/22
to jal...@googlegroups.com
:) "precision rectifier" or "zero voltage drop rectifier" not "zero precision rectifier" :P

flyway38

unread,
Aug 31, 2022, 12:32:20 PM8/31/22
to jallib
Hi Vasile,

Yes understand your point and thank you for your input.
But, my problem is cannot change the hardware design. Maybe in near future...
Also, it's not AC signal what I need to smooth. Its a positive oscillating signal, like 3V, +/-1V.
Thanks anyways.

Cheers,
Filipe Santos.

vsurducan

unread,
Aug 31, 2022, 10:53:54 PM8/31/22
to jal...@googlegroups.com
I do not think you can do a proper average on a +3V +/-1V whichever "software tricks" you'll do.
Which amplitude do you want to measure on that signal, V+, V-, Vpp? Perhaps an oscilloscope capture would help for a better answer.

flyway38

unread,
Sep 13, 2022, 10:48:14 AM9/13/22
to jallib
Hello Vasile,

Sorry but only today had a chance to check here.
Yes, not easy to smooth that signal but managed to do it good enough using median values and then filtering the readings if they change more than some threshold... I can publish that code, here, if needed.
And yes, reading values inside VCC/GND amplitude.
Thank you for the input.

Cheers,
FS.
Reply all
Reply to author
Forward
0 new messages