View previous topic :: View next topic |
Author |
Message |
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
Setting up analog read on PIC18F87J60 |
Posted: Mon Nov 13, 2023 1:03 pm |
|
|
Hi all,
I am trying to read from one channel (AN8) of a PIC18F87J60. I have my code setup as follows:
Setup of ADC:
Code: | setup_adc_ports(AN0_TO_AN8, VREF_VREF);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(8); |
Reading ADC in while loop
Code: | float value = (float)read_adc()/1023.0;
fprintf(DEBUG, "ADC: %1.2f\r\n", value);
|
Setup of the ADC is a little different than I am used to. In the past it seemed that I was able to set the individual pins I wanted to use (sAN8,VREF_VREF) etc. This pic doesnt appear to support this?
When testing this code I am seeing a calculated value of about 0.6V (raw value of about 624 from read_adc()). When measuring the input voltage to the pin, I am at around 2V. Any guidance as to what I am doing wrong here?
PIC schematic here:
![](https://i.imgur.com/yPfdYkq.png) |
|
![](templates/subSilver/images/spacer.gif) |
gaugeguy
Joined: 05 Apr 2011 Posts: 296
|
|
Posted: Mon Nov 13, 2023 1:46 pm |
|
|
If you are getting 624 from the A/D and your vref is 3.3V then 624/1024*3.3 = 2.01V |
|
![](templates/subSilver/images/spacer.gif) |
gaugeguy
Joined: 05 Apr 2011 Posts: 296
|
|
Posted: Mon Nov 13, 2023 1:59 pm |
|
|
I would recommend skipping the floating point and do the result in hundredths volt with integer math
(624 * 330) / 1024 = 201 (2.01V) |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Mon Nov 13, 2023 4:47 pm |
|
|
comment.
when reading analog voltages,always best to read several times, take an average. relying on ONE reading can be bad (think 737-MAX8)
All my temperatures get read 10x,toss high,toss low, avg the remaining 8 ( Olympic averaging). It's a very fast,accurate method though entire books have been written about the 'best' methods.....
Also all ADC inputs should have a 'smoothing' cap on them to stabilize the reading. Value of cap is based upon sensor and 'update' rate.
If AN8 is Vtemp ( temperature) ,0.68mfd is good. Temperature generally is a very slow process compared to a PIC's clock. |
|
![](templates/subSilver/images/spacer.gif) |
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
|
Posted: Mon Nov 13, 2023 5:51 pm |
|
|
gaugeguy wrote: | If you are getting 624 from the A/D and your vref is 3.3V then 624/1024*3.3 = 2.01V |
Doh! Thank you, I shouldn't have posted this minutes before leaving for the day...
It did seem that I was not seeing any change, but I will have to confirm that when I am with the hardware again.
Does it seem that I have the analog configured correctly? |
|
![](templates/subSilver/images/spacer.gif) |
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
|
Posted: Mon Nov 13, 2023 5:57 pm |
|
|
temtronic wrote: | comment.
when reading analog voltages,always best to read several times, take an average. relying on ONE reading can be bad (think 737-MAX8)
All my temperatures get read 10x,toss high,toss low, avg the remaining 8 ( Olympic averaging). It's a very fast,accurate method though entire books have been written about the 'best' methods.....
Also all ADC inputs should have a 'smoothing' cap on them to stabilize the reading. Value of cap is based upon sensor and 'update' rate.
If AN8 is Vtemp ( temperature) ,0.68mfd is good. Temperature generally is a very slow process compared to a PIC's clock. |
Thank you for the info. I just got these boards in, so this is all my first pass at just making sure the board is working and the PIC is configured correctly. I am planning on adding the averaging as you mentioned.
The ouput of the sensor goes through a low pass filter (cutoff @1kHz) and is then amplified with a gain of 3. Adding the larger cap is a good idea though! |
|
![](templates/subSilver/images/spacer.gif) |
|