View previous topic :: View next topic |
Author |
Message |
arrow
Joined: 17 May 2005 Posts: 213
|
ADC should be 10bits gettin 8 bits- please help! |
Posted: Wed Aug 02, 2006 7:15 am |
|
|
Hi
I have the code listed below that does A/D on PIN_A0 on the 18F4550. It appears to be giving me 8 bit resolution (numbers that are printfed are always less than 255)- the data sheet says it should be 10 bits.
Can anyone please tell me how I can get it to my rquired 10bits of resolution?
Thank you in advance
a.
Code: |
#include <18F4550>
#use delay(clock=20000000)
#fuses HS,PROTECT,NOWDT,NOBROWNOUT
#use rs232(baud=19200, xmit=PIN_D1, rcv=PIN_D0,ERRORS)
int8 hi, lo;
int16 Ch0;
int1 newData;
#int_timer2
void clock(){
Ch0 = Read_ADC();
newData = true;
}
void itoX(int16 data){
hi = data>>8;
lo = data&0X00FF;
}
void main() {
//***SETUP A_D
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( AN0 | VSS_VDD );
//***Set the A_D channel and let it stabilize
//*** Should work for 1 channel
set_adc_channel( 0 );
delay_us(10);
//-----------------------------------------------
//***Setup time interrupts
setup_timer_2(T2_DIV_BY_4,249,10); //***500samples/second 20MHz=>20e6/16/4/(249+1)/5
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
set_timer2(0);
//-----------------------------------------------
newData = false;
//***Write data to MMC
while(true){
if(newData){
itoX(Ch0);
// printf("%x%x\n", hi, lo);
printf("%ld\n", Ch0);
newData=false;
}
}
return;
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Wed Aug 02, 2006 7:23 am |
|
|
Try this:
#include <18F4550>
#DEVICE ADC=10
#use delay(clock=20000000)
#fuses HS,PROTECT,NOWDT,NOBROWNOUT _________________ David |
|
![](templates/subSilver/images/spacer.gif) |
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Wed Aug 02, 2006 7:35 am |
|
|
As an aside, you realize your code is going to stall out in your timer ISR waiting for the ADC to finish? This will block the execution of other (maybe important) code later when you add more to your program.
Add arrows #device line and read up on the following:
* setup_adc() and the clock source selection values, look at the chip data sheet to select the appropriate configuration(s) for your oscillator speed.
* the adc interrupt system, again consult the chip datasheet.
Now think about changing your timer ISR to start the acquisition and your adc ISR to set the newData flag. Clear the newData flag in your main after you use the data and/or in your timer isr when you initiate a new acquisition cycle.
If you don't want to use the ADC done interrupt, consider just polling on the done bit.
Also, there are built-in functions to do what your itoX does. Look in the CCS manual for make8().
And last of all, consider the PUT and NOLVP flags for your #fuses statement. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Aug 02, 2006 7:47 am |
|
|
Thank you- that worked!!!
Regards
a. |
|
![](templates/subSilver/images/spacer.gif) |
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Wed Aug 02, 2006 9:48 am |
|
|
Hi Rwyoung
Thank you for your help.
With your suggestion I have used make8() successfully!
I am also trying to implement
#int_ad
void AD(){
newData=true;
}
I am not certain what I should do about:
setup_adc( ADC_CLOCK_INTERNAL );
can the clock speed for ADC be increased? and if so how?
I have looked at the data sheet and I do not quite understand what they are saying.
Once again thank you for all your help.
Regards
a. |
|
![](templates/subSilver/images/spacer.gif) |
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Wed Aug 02, 2006 10:42 am |
|
|
Read it again
Seriously, read it very carefully and pay attention to the timing diagrams. You are trying to determine what internal clock divisor is appropriate. There is a minimum time required to settle and a minimum time required to complete an acquision.
The biggest benefit you have when NOT using the ADC_CLOCK_INTERNAL flag is that you will have a predictible acquistition time. Also, for some (and possibly all) PIC ADCs you get better performance (wrt accuracy).[/b] _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
![](templates/subSilver/images/spacer.gif) |
|