View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
ADC problem PIC18F4620 |
Posted: Sun Feb 12, 2012 4:03 am |
|
|
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: 19537
|
|
Posted: Sun Feb 12, 2012 4:54 am |
|
|
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
|
|
Posted: Sun Feb 12, 2012 8:28 am |
|
|
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: 19537
|
|
Posted: Sun Feb 12, 2012 9:19 am |
|
|
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
|
|
Posted: Mon Feb 13, 2012 7:11 am |
|
|
Thanks! I`ll correct the errors and see what will happen... |
|
|
|