View previous topic :: View next topic |
Author |
Message |
Bryan
Joined: 23 Apr 2005 Posts: 73
|
A/D Conversion - Changing Resolution |
Posted: Sat Apr 23, 2005 4:32 pm |
|
|
I am new to PICs and have been experimenting with the A/D convert function on my PIC18F1320. Here is the code I use to turn on an LED when the analog input voltage goes above a certain threshold:
#include <18F1320.h>
#fuses NOWDT,NOPROTECT,NOLVP,NOMCLR,INTRC_IO// CCPB0
#use delay(clock=20000000)
void main ()
{
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(1);
output_a(0x00);
output_b(0x00);
set_tris_a (0xFF);
set_tris_b (0x00);
while (1)
{
if(read_adc() >= 128)
{
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}
}
}
Now, since I am comparing the read_adc() with 128, I expect this to be 1/8 of the reference voltage (I use 5V Vdd so I was expecting the LED to turn on at around 0.625V) since the resolution is supposed to be 10-bit or 1024 possible discrete levels. The strange thing is that it is acting like there is an 8-bit resolution since the LED turns on when I reach half of the reference voltage (Vdd is 5V and the LED comes on at 2.5V exactly) - I used a multimeter to make sure that my voltage readouts were correct. Anyone know how I can change a setting to make the A/D the full 10-bit that is possible with the PIC18? Any help would be appreciated! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 23, 2005 6:21 pm |
|
|
You could look in the manual or Help file in the read_adc() section.
But the answer is to add the line shown in bold below, immediately
after the #include line for your PIC.
#include <18F1320.h>
#device adc=10 |
|
|
Bryan
Joined: 23 Apr 2005 Posts: 73
|
|
Posted: Sat Apr 23, 2005 6:57 pm |
|
|
Thanks for the help, worked like a charm! |
|
|
UFAnders
Joined: 13 Apr 2005 Posts: 36 Location: Michigan
|
Problem with #device adc=10, PIC18F2520 |
Posted: Tue Jun 07, 2005 11:13 pm |
|
|
I have scoured the forum for reasons as to why I keep getting an 8-bit maximum value from my PIC 18F2520's ADC. I included the #device adc=10 statement directly after my chip's include file directive, and I have the read_adc() result flowing into an integer. I am viewing the value through HyperTerminal with fprintf() shooting out a zero-padded 4-digit result, but it still never goes higher than 255!
Any ideas? I'm using PCH 3.217.
Thank you! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 08, 2005 12:10 am |
|
|
Quote: | I have the read_adc() result flowing into an integer. |
Does that mean you're doing this ?
Code: | int result;
// A/D setup code not shown.
result = read_adc(); |
If so, you need to know that in CCS, an "int" is an unsigned 8-bit integer.
If that's what you're doing, then you're only getting the lower 8 bits of
the A/D value. To fix this problem, you need to declare "result" like this:
Also in printf(), you need to use "%LX" or "%LU" to display a 16-bit
or 32-bit value in CCS. |
|
|
UFAnders
Joined: 13 Apr 2005 Posts: 36 Location: Michigan
|
|
Posted: Thu Jun 09, 2005 1:15 am |
|
|
Haha, no way! You're absolutely correct - I declared this:
int ADCResult;
Then, just before I checked the forum again I tried:
long int ADCResult;
And ran it through:
fprintf(swrs232, "%04lu\r", ADCResult);
Works like a charm! Thanks for the super-quick reply, I imagined it would take weeks! |
|
|
|