CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Setting up analog read on PIC18F87J60

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
demedeiros



Joined: 27 Dec 2013
Posts: 71

View user's profile Send private message

Setting up analog read on PIC18F87J60
PostPosted: Mon Nov 13, 2023 1:03 pm     Reply with quote

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:
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Mon Nov 13, 2023 1:46 pm     Reply with quote

If you are getting 624 from the A/D and your vref is 3.3V then 624/1024*3.3 = 2.01V
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Mon Nov 13, 2023 1:59 pm     Reply with quote

I would recommend skipping the floating point and do the result in hundredths volt with integer math

(624 * 330) / 1024 = 201 (2.01V)
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Nov 13, 2023 4:47 pm     Reply with quote

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.
demedeiros



Joined: 27 Dec 2013
Posts: 71

View user's profile Send private message

PostPosted: Mon Nov 13, 2023 5:51 pm     Reply with quote

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?
demedeiros



Joined: 27 Dec 2013
Posts: 71

View user's profile Send private message

PostPosted: Mon Nov 13, 2023 5:57 pm     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group