|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 08, 2011 3:38 pm |
|
|
I don't have your exact PIC, but I have one in the same family, and
with the test program shown below, it displays the following results
in the TeraTerm window. This was tested with vs. 4.119 on a PicDem2-
Plus board. I jumpered in a 100 nF cap for the VCAP pin for this test.
The .LST files have the same generated code for #device adc=10 or 12.
This result comes from turning the 5K trimpot on sAN0 through its
complete range:
Quote: |
9
9
10
93
212
528
713
1156
1524
2015
2695
3518
4075
4079
4079
4079
|
Code: |
#include <18F46K80.h>
#device adc=12
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main(void)
{
int16 result;
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_4);
set_adc_channel(0);
delay_us(5);
while(1)
{
result = read_adc();
printf("%lu \n\r", result);
delay_ms(500);
}
} |
|
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Thu Sep 08, 2011 4:16 pm |
|
|
Oh really, hey?
Now that's confusing, how would the adc=10 return values higher than 1024?
I'm confused....
My setup is the following:
I have a 10k NTC to ground and a 10k pullup to +5V, in the middle, i go to the adc and the NTC is specified to have a resistance of 598793Ohm at -55C, 10k at 25C, it isn't linear but i got the values from the datasheet and calculated my values.
At room temperature, i get about a value of ~2048-ish which seems about right, but at -8.03C I was already maxed out at 4095 and it wouldn't go higher when i went lower with the temperature.
I got this with adc=16 as well as adc=12, I don't get to "try" adc=10 until next Wednesday and thus would like to avoid "trying" and failing.... so suggestions are appreciated!
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 08, 2011 4:55 pm |
|
|
Quote: | Now that's confusing, how would the adc=10 return values higher than 1024? |
The compiler doesn't mask the ADC result down to 10 bits. It just reads
the ADRES registers with read_adc() and returns the result. It does the
same exact thing for "adc=12". |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Thu Sep 08, 2011 4:59 pm |
|
|
Interesting so i'll need to come up with some way of measuring the voltage on the adc then...that'll be difficult....
I might be able to measure the voltage on the ntc.... |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Thu Sep 08, 2011 5:50 pm |
|
|
Or anyone another idea how to troubleshoot this?
It really shouldn't be that the NTC would be the problem, i don't see why... :( |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Sep 08, 2011 8:05 pm |
|
|
you said:
Quote: |
I have tried adc=16 as well but that doesn't work, the maximum value the adc ever reaches is 4095 (12bit) which makes sense from a hardware point of view... so it would exhaust its range before reaching all the way up to it's maximum value....
|
last time i checked - thats a 12 bit result dude ;-))
what are you EXPECTING to get ??? |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Thu Sep 08, 2011 8:11 pm |
|
|
yes but if you have adc=16, you'd expect a 16bit value, wouldn't you? |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri Sep 09, 2011 2:21 am |
|
|
cerr wrote: | yes but if you have adc=16, you'd expect a 16bit value, wouldn't you? |
No, I'd expect the result to be consistent with the hardware: a 12 bit result padded with 4 bits at the least siginifcant end just as the CCS documentation for read_adc() says.
This makes sense. If you always use adc=16 then it won't matter (much) what the actual hardware gives: you'll always get the best that it can give aligned to the top of the 16 bit word. That allows simpler upwards migration of code to better equipped processors. The fly in the ointment is that the range of the values, i.e. full scale, changes. With 8 bit hardware the maximum ADC count will be 65280, for 10 bit its 65472 and for 12 bit its 65520, and all of these represent different voltages. But if you treat them all as 16 bits then those voltages will be correct. If we assume a 5V reference voltage then all those full ranges will convert to voltage consistently:
Vmeasured = ADC value/65536 * 5.0V
8 bit, 65280 = 4.98046875V
10 bit, 65472 = 4.9951171875V
12 bit, 65280 = 4.998779296875V
16 bit, 65536 = 4.9999237060546875V (but no PIC does this... yet)
Note that, of course, none can convert 5V - its overrange.
So yes, its a 16 bit value, but NOT 16 bit resolution.
Incidentally I can't find any mention of #device adc=12 in PCWH. So, I'm going to assume that adc=12 is invalid and instead you'll have to use adc=16 and that the alignment of the result is dependant on the hardware. That can be changed by setting the ADFM bit in ADCON2. Set is right justified, which is what you seem to be getting, whereas the default setting, clear, is left justified as I've described above. Presumably there's an part specific setting in set_adc() as something must be setting it away from the default.
RF Developer |
|
|
|
|
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
|