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

10 bit ADC basics

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



Joined: 24 Sep 2006
Posts: 262

View user's profile Send private message AIM Address

10 bit ADC basics
PostPosted: Thu Jan 30, 2014 3:53 pm     Reply with quote

Question 1.
Is the default actually 8 bit?
I am using a PIC16F1509 and my program works reading the ADC into an 8 bit variable.

Question 2.
Should I be using #device ADC=8 or is using the default OK (if there is one)?

Question 3.
After searching for answers I notice #device *=16 ADC=10 being used. What exactly is *=16 ? The CCS manual suggests it sets the pointer size. What pointer and why?

Question 4.
What is the default justification for 10 bits returned in a 16 bit variable?

Question 5.
How would I change a 16 bit value into an 8 bit variable?

Thanks in advance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Thu Jan 30, 2014 4:11 pm     Reply with quote

ADC=8, is the default, but it is always safer to specify it yourself.

ADC=10, or ADC=16, take longer to read, larger code, and slower. If you only want an 8bit value, stick with ADC=8.

ADC=10 returns the value right justified (low ten bits). ADC=16 returns the value left justified (to the top of the 16bit result). 10bit value to 8bits, you'd have to divide by 4. To just read the top byte or low byte of a value look at the 'make8' instruction.

*=16, is not connected with the ADC, and can even be on another line. When you 'address' values in RAM, the address is called a 'pointer' (this is the C name for the address). The RAM memory in the PIC16 is 'paged'. So to talk to things bigger than one 'page' in RAM, a multi byte address is needed, and code has to be added to automatically select this. The default is to only handle the RAM in the bottom page of memory (quick). *=16, tells the compiler to generate code to address the whole of the RAM, allowing a maximum of 64KB of RAM to be addressed (doesn't do anything on larger chips without the RAM paging PIC18/24 etc. - often misused on these)

Best Wishes
rovtech



Joined: 24 Sep 2006
Posts: 262

View user's profile Send private message AIM Address

PostPosted: Thu Jan 30, 2014 7:54 pm     Reply with quote

That was fast and informative, Thanks.
rovtech



Joined: 24 Sep 2006
Posts: 262

View user's profile Send private message AIM Address

PostPosted: Fri Jan 31, 2014 2:07 pm     Reply with quote

Here are two examples and some more questions. Only relevant code is shown.
// Example using an 8 bit variable and reading a 10 bit ADC as 8 bit
Code:
#include <16F1509.H>
#device ADC = 8                       // optional but better to use
void main()
{
   setup_oscillator(OSC_8MHZ);   // Set PIC to int osc   
   int volts8;                               // voltage on pot, 8 bit
// setup ADC here including 10 us delay
   volts8 = read_adc();               // reads ADC, stores in volts8
   delay_ms(10);                        // wait 10 ms
}

// Example using a 10 bit ADC but converting to an 8 bit variable
Code:
#include <16F1509.H>
#device ADC = 10
void main()
{
   setup_oscillator(OSC_8MHZ);    // Set PIC to int osc
   long pot_val;                           // voltage on pot, 16 bit
   int volts8;                               // 8 bit value of voltage
// setup ADC here including 10 us delay
   pot_val = read_adc();             // reads ADC, stores in pot_val
   delay_ms(10);                        // wait 10 ms
// Adjust 10 bit to 8 bit
   pot_val = pot_val >>2;           // multiply by 4
   volts8 = make8 (pot_val, 0);   // no byte offset
}

They both work but it is interesting to note that if I use #device ADC=16 in the first example it does not generate a compile error or warning, but the value in volts8 is garbage.
Question:
I can't remember why I use the 10 ms delay after reading the ADC, it is something I used to do several years ago. Is any delay here necessary. I know I need a delay after setting the ADC channel but I see delays all over the place in people's examples. Where should I get the delay value, from the data sheet?
Question:
The PIC16F1509 .h file shows only (I think) one method of setting the internal oscillator using the setup_oscillator(OSC_nMHZ); as shown. Is there another method and why does this statement not compile if I put it before the void main()
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