View previous topic :: View next topic |
Author |
Message |
sonny
Joined: 14 Sep 2015 Posts: 7
|
Problems reading a analog input port RE0 pic18F45j50 |
Posted: Wed Feb 24, 2016 9:00 am |
|
|
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
|
|
Posted: Wed Feb 24, 2016 9:37 am |
|
|
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
|
|
Posted: Wed Feb 24, 2016 10:02 am |
|
|
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 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Wed Feb 24, 2016 10:20 am |
|
|
setup_adc_ports(sAN5|VSS_VDD); |
|
|
sonny
Joined: 14 Sep 2015 Posts: 7
|
|
Posted: Wed Feb 24, 2016 10:36 am |
|
|
Ttelmah
got it to work thanks my friend |
|
|
|