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 support@ccsinfo.com

A/D Conversion - Changing Resolution

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



Joined: 23 Apr 2005
Posts: 73

View user's profile Send private message Send e-mail AIM Address

A/D Conversion - Changing Resolution
PostPosted: Sat Apr 23, 2005 4:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 23, 2005 6:21 pm     Reply with quote

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

View user's profile Send private message Send e-mail AIM Address

PostPosted: Sat Apr 23, 2005 6:57 pm     Reply with quote

Thanks for the help, worked like a charm!
UFAnders



Joined: 13 Apr 2005
Posts: 36
Location: Michigan

View user's profile Send private message Send e-mail Visit poster's website AIM Address

Problem with #device adc=10, PIC18F2520
PostPosted: Tue Jun 07, 2005 11:13 pm     Reply with quote

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! Evil or Very Mad

Any ideas? I'm using PCH 3.217.

Thank you!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 08, 2005 12:10 am     Reply with quote

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:
Code:
int16 result;


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

View user's profile Send private message Send e-mail Visit poster's website AIM Address

PostPosted: Thu Jun 09, 2005 1:15 am     Reply with quote

Haha, no way! Embarassed 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! Very Happy
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