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

A/D Converter on PIC16F886

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



Joined: 21 Jan 2009
Posts: 6

View user's profile Send private message

A/D Converter on PIC16F886
PostPosted: Tue Feb 03, 2009 2:59 pm     Reply with quote

I am having a problem reading the internal AtoD converter on a PIC16F886. It appears when I look at the value in ADCON0 that the wrong input is being selected when I do a "set_adc_channel( x );"

I have looked at the 16F886.h file and it appears that I am using the correct constants to setup the ports and clock:
setup_adc_ports( sAN2 |sAN3 | sAN4 | VSS_VDD );
setup_adc( ADC_CLOCK_INTERNAL );

When I have selected look at ADCON0 it is set to a value of 0x11101001 (0xE9) which based on the PIC16F88x data sheet would indicate that I have selected AN10 not AN2 as I would expect.

Has anyone elese experienced this problem?

I am using PCM 4.085

Here is my source code:
Code:
// ADC TEST
// Test code for PIC16F886 A to D Converter
// Compiler CCS PCM version 4.085

#include <16F886.H>

#device ICD = TRUE
#device *=16 ADC=10

#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)

#zero_ram


//===============================
void main()
{
long AD_Ch_2 = 0;
long AD_Ch_3 = 0;
long AD_Ch_4 = 0;

// Setup PortA I/O
   Set_Tris_A(0b011110); //sets up port A as an input port

//init A/D converter
   setup_adc_ports( sAN2 |sAN3 | sAN4 | VSS_VDD );
   setup_adc( ADC_CLOCK_INTERNAL );

   while(1)
   {
   
      set_adc_channel( PIN_A2 );      // ADC2 (RA2)
      delay_us(10);               // Delay to allow stabilization
      AD_Ch_2 = read_adc();         // Get the  AtoD value
      
      set_adc_channel( PIN_A3 );      // ADC3 (RA3)
      delay_us(10);               // Delay to allow stabilization
      AD_Ch_2 = read_adc();         // Get the AtoD value
      
      set_adc_channel( PIN_A4 );      // ADC4 (RA4)
      delay_us(10);               // Delay to allow stabilization
      AD_Ch_2 = read_adc();         // Get the AtoD value
      
   
      delay_ms(500);
   
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 3:16 pm     Reply with quote

Quote:
set_adc_channel( PIN_A2 );
set_adc_channel( PIN_A3 );
set_adc_channel( PIN_A4 );


From the manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Quote:
Syntax: set_adc_channel (chan)
Parameters: 'chan' is the channel number to select.
Channel numbers start at 0 and are labeled in the data sheet AN0, AN1

ie., do not use Pin constants for channels. Use 0, 1, 2, 3, etc. for the
channel.
starbuck



Joined: 21 Jan 2009
Posts: 6

View user's profile Send private message

OK - Read the Manual not just look at it!
PostPosted: Tue Feb 03, 2009 3:44 pm     Reply with quote

Works fine, Thanks again PCM

Another case of staring at the manual too long!

Here is my revised code for the record:
Code:

// ADC TEST
// Test code for PIC16F886 A to D Converter
// Compiler CCS PCM version 4.085

#include <16F886.H>

#device ICD = TRUE
#device *=16 ADC=10

#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)

#zero_ram


//===============================
void main()
{
long AD_Ch_2 = 0;
long AD_Ch_3 = 0;
long AD_Ch_4 = 0;

// Setup PortA I/O
   Set_Tris_A(0b011110); //sets up port A as an input port

//init A/D converter
   setup_adc_ports( sAN2 |sAN3 | sAN4 | VSS_VDD );
   setup_adc( ADC_CLOCK_INTERNAL );

   while(1)
   {

   
      set_adc_channel( 2 );      // ADC2 (RA2)
      delay_us(20);               // Delay to allow stabilization
      AD_Ch_2 = read_adc();         // Get the  AtoD value
      
      set_adc_channel( 3 );      // ADC3 (RA3)
      delay_us(20);               // Delay to allow stabilization
      AD_Ch_3 = read_adc();         // Get the AtoD value
      
      set_adc_channel( 4 );      // ADC4 (RA4)
      delay_us(20);               // Delay to allow stabilization
      AD_Ch_4 = read_adc();         // Get the AtoD value

   
      delay_ms(500);
   
  }
}
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