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

Problems reading a analog input port RE0 pic18F45j50

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



Joined: 14 Sep 2015
Posts: 7

View user's profile Send private message

Problems reading a analog input port RE0 pic18F45j50
PostPosted: Wed Feb 24, 2016 9:00 am     Reply with quote

Hello everybody not sure what I'm doing wrong. I have a 11k pullup in that port and a connector comes and either sets the port to ground or to a voltage divider 1.7, VDD is 3.3V. The only one that is reading accurate is the gnd.
Code:

#use delay(internal 4MHz);
setup_adc_ports(sAN5|VSS_VREF);
unsigned int16 val;
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
set_adc_channel(5);
val = read_adc;

Problem is all I see is 50048 and I'm looking for a 1.629

Please help pretty new to ccs,
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Feb 24, 2016 9:37 am     Reply with quote

You are not ever going to get/see 1.629.

The adc, returns an integer, not a 'float' value.....

Now you don't show one critical line. #device adc=xx
This specifies the output format used for the ADC. probably defaulting to =16, which will then give a left justified 16bit value.

Then you are telling the ADC to use Vref. What do you have connected to the Vref+ input?. Odds are nothing?. If so, no wonder you get an odd value....
Assuming you don't have an external reference available, and want to use the supply, you need VSS_VDD.

Use #device ADC=10
to get the standard 10bit output from the ADC

Your adc value will then be Vin/(Vdd/1024) (yes, 1024, not 1023).

Since you are setting TAD_MUL_0, there must be at least Tacq, between selecting the ADC channel and actually reading. Does your divider meet the impedance requirements of the ADC (section 21.1 - quote "The maximum recommended impedance for analog sources is 2.5 k)?.
sonny



Joined: 14 Sep 2015
Posts: 7

View user's profile Send private message

PostPosted: Wed Feb 24, 2016 10:02 am     Reply with quote

Ttelmah
thanks for the response

on the VREF I actually want to use vdd as vref+ and vss as vref- not sure if I'm doing it right, when I put ground I do see 0
is when I'm having my voltage divider which is 11k both of them so I guess I'm over the recommended impedance.

if I want to use vdd and vss do I need to set them in the code or is the default value?

thanks Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Feb 24, 2016 10:20 am     Reply with quote

setup_adc_ports(sAN5|VSS_VDD);
sonny



Joined: 14 Sep 2015
Posts: 7

View user's profile Send private message

PostPosted: Wed Feb 24, 2016 10:36 am     Reply with quote

Ttelmah

got it to work thanks my friend Smile
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