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

ADC should be 10bits gettin 8 bits- please help!

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



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

ADC should be 10bits gettin 8 bits- please help!
PostPosted: Wed Aug 02, 2006 7:15 am     Reply with quote

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;
}
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Wed Aug 02, 2006 7:23 am     Reply with quote

Try this:

#include <18F4550>

#DEVICE ADC=10

#use delay(clock=20000000)
#fuses HS,PROTECT,NOWDT,NOBROWNOUT
_________________
David
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Wed Aug 02, 2006 7:35 am     Reply with quote

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!
Guest








PostPosted: Wed Aug 02, 2006 7:47 am     Reply with quote

Thank you- that worked!!!

Regards
a.
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Wed Aug 02, 2006 9:48 am     Reply with quote

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.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Wed Aug 02, 2006 10:42 am     Reply with quote

Read it again Twisted Evil

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!
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