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

ADC problem PIC18F4620

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



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

ADC problem PIC18F4620
PostPosted: Sun Feb 12, 2012 4:03 am     Reply with quote

I'm using 18F4620. The ADC is running in 8 bits. I want to use 10 or more bits ADC, but directives as
#define ADC=10
or
#define ADC=16

I'm using only channel 0.
How to increase the bits of the ADC???
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Sun Feb 12, 2012 4:54 am     Reply with quote

You already have the directives you need:
#device ADC=10
Since the 4620, only has a 10bit ADC, you are not going to get 'or more'.

Are you saying you are still seeing an 8bit result with the device line specified?. If so, think 'how large is the variable I'm putting the result into'?.

You need to be using an int16 variable to hold the value, or it'll be trimmed to 8bits, and using 16bit output formats (%ld, rather than %d if printing for example).

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Sun Feb 12, 2012 8:28 am     Reply with quote

I'm using int16 for the result.
Example:
Code:

#include "18F4620.h"
#FUSES HS,NOWDT
#use delay(clock=20000000)
#define ADC=10

void main(){
int16 t;

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );

while(1){
setup_adc(ADC_start_only);
while(!adc_done())
{
}
delay_us(10);
t=read_adc();
writeint(t,0x80);
delay_ms(500);
}
}


And again the result is in 8 bit format. Where is the problem???
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Sun Feb 12, 2012 9:19 am     Reply with quote

As a comment, there are several things 'wrong' with the way you are reading the ADC. Read the data sheet. Should you be using ADC_CLOCK_INTERNAL, if you leave the main CPU clock running while reading the ADC?. If you are not stopping the ADC clock, then just use t=read_ADC();. Either select the right clock, or use sleep.
Then, supposedly having waited for the ADC to actually perform the acquisition, you wait 10uSec, and throw away the result (read_adc, without a value, automatically starts the ADC again, waits for it to acquire and reads the result.
Then you are using the wrong value - setup_adc, does _not_ accept the value 'ADC_START_ONLY'. In fact this will set the ADC clock to FOSC/8, which at 20MHz, runs the ADC nearly twice as fast as it supports.

What does 'writeint' do?. I'd guess (you don't tell us what it is meant to do), that it is only accepting an 8bit int....

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 7:11 am     Reply with quote

Thanks! I`ll correct the errors and see what will happen...
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